QueryCacheProfile.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Cache;
  20. use Doctrine\Common\Cache\Cache;
  21. /**
  22. * Query Cache Profile handles the data relevant for query caching.
  23. *
  24. * It is a value object, setter methods return NEW instances.
  25. *
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class QueryCacheProfile
  29. {
  30. /**
  31. * @var Cache
  32. */
  33. private $resultCacheDriver;
  34. /**
  35. * @var int
  36. */
  37. private $lifetime = 0;
  38. /**
  39. * @var string
  40. */
  41. private $cacheKey;
  42. /**
  43. * @param int $lifetime
  44. * @param string $cacheKey
  45. * @param Cache $resultCache
  46. */
  47. public function __construct($lifetime = 0, $cacheKey = null, Cache $resultCache = null)
  48. {
  49. $this->lifetime = $lifetime;
  50. $this->cacheKey = $cacheKey;
  51. $this->resultCacheDriver = $resultCache;
  52. }
  53. /**
  54. * @return Cache
  55. */
  56. public function getResultCacheDriver()
  57. {
  58. return $this->resultCacheDriver;
  59. }
  60. /**
  61. * @return int
  62. */
  63. public function getLifetime()
  64. {
  65. return $this->lifetime;
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getCacheKey()
  71. {
  72. if ($this->cacheKey === null) {
  73. throw CacheException::noCacheKey();
  74. }
  75. return $this->cacheKey;
  76. }
  77. /**
  78. * Generate the real cache key from query, params and types.
  79. *
  80. * @param string $query
  81. * @param array $params
  82. * @param array $types
  83. * @return array
  84. */
  85. public function generateCacheKeys($query, $params, $types)
  86. {
  87. $realCacheKey = $query . "-" . serialize($params) . "-" . serialize($types);
  88. // should the key be automatically generated using the inputs or is the cache key set?
  89. if ($this->cacheKey === null) {
  90. $cacheKey = sha1($realCacheKey);
  91. } else {
  92. $cacheKey = $this->cacheKey;
  93. }
  94. return array($cacheKey, $realCacheKey);
  95. }
  96. /**
  97. * @param Cache $cache
  98. * @return QueryCacheProfile
  99. */
  100. public function setResultCacheDriver(Cache $cache)
  101. {
  102. return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
  103. }
  104. /**
  105. * @param string|null $cacheKey
  106. * @return QueryCacheProfile
  107. */
  108. public function setCacheKey($cacheKey)
  109. {
  110. return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
  111. }
  112. /**
  113. * @param int $lifetime
  114. * @return QueryCacheProfile
  115. */
  116. public function setLifetime($lifetime)
  117. {
  118. return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
  119. }
  120. }