QueryCacheProfile.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 MIT license. 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 \Doctrine\Common\Cache\Cache|null
  32. */
  33. private $resultCacheDriver;
  34. /**
  35. * @var integer
  36. */
  37. private $lifetime = 0;
  38. /**
  39. * @var string|null
  40. */
  41. private $cacheKey;
  42. /**
  43. * @param integer $lifetime
  44. * @param string|null $cacheKey
  45. * @param \Doctrine\Common\Cache\Cache|null $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 \Doctrine\Common\Cache\Cache|null
  55. */
  56. public function getResultCacheDriver()
  57. {
  58. return $this->resultCacheDriver;
  59. }
  60. /**
  61. * @return integer
  62. */
  63. public function getLifetime()
  64. {
  65. return $this->lifetime;
  66. }
  67. /**
  68. * @return string
  69. *
  70. * @throws \Doctrine\DBAL\Cache\CacheException
  71. */
  72. public function getCacheKey()
  73. {
  74. if ($this->cacheKey === null) {
  75. throw CacheException::noCacheKey();
  76. }
  77. return $this->cacheKey;
  78. }
  79. /**
  80. * Generates the real cache key from query, params and types.
  81. *
  82. * @param string $query
  83. * @param array $params
  84. * @param array $types
  85. *
  86. * @return array
  87. */
  88. public function generateCacheKeys($query, $params, $types)
  89. {
  90. $realCacheKey = $query . "-" . serialize($params) . "-" . serialize($types);
  91. // should the key be automatically generated using the inputs or is the cache key set?
  92. if ($this->cacheKey === null) {
  93. $cacheKey = sha1($realCacheKey);
  94. } else {
  95. $cacheKey = $this->cacheKey;
  96. }
  97. return array($cacheKey, $realCacheKey);
  98. }
  99. /**
  100. * @param \Doctrine\Common\Cache\Cache $cache
  101. *
  102. * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  103. */
  104. public function setResultCacheDriver(Cache $cache)
  105. {
  106. return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
  107. }
  108. /**
  109. * @param string|null $cacheKey
  110. *
  111. * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  112. */
  113. public function setCacheKey($cacheKey)
  114. {
  115. return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
  116. }
  117. /**
  118. * @param integer $lifetime
  119. *
  120. * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  121. */
  122. public function setLifetime($lifetime)
  123. {
  124. return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
  125. }
  126. }