QueryCacheProfile.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Doctrine\DBAL\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. use function hash;
  5. use function serialize;
  6. use function sha1;
  7. /**
  8. * Query Cache Profile handles the data relevant for query caching.
  9. *
  10. * It is a value object, setter methods return NEW instances.
  11. */
  12. class QueryCacheProfile
  13. {
  14. /** @var Cache|null */
  15. private $resultCacheDriver;
  16. /** @var int */
  17. private $lifetime = 0;
  18. /** @var string|null */
  19. private $cacheKey;
  20. /**
  21. * @param int $lifetime
  22. * @param string|null $cacheKey
  23. */
  24. public function __construct($lifetime = 0, $cacheKey = null, ?Cache $resultCache = null)
  25. {
  26. $this->lifetime = $lifetime;
  27. $this->cacheKey = $cacheKey;
  28. $this->resultCacheDriver = $resultCache;
  29. }
  30. /**
  31. * @return Cache|null
  32. */
  33. public function getResultCacheDriver()
  34. {
  35. return $this->resultCacheDriver;
  36. }
  37. /**
  38. * @return int
  39. */
  40. public function getLifetime()
  41. {
  42. return $this->lifetime;
  43. }
  44. /**
  45. * @return string
  46. *
  47. * @throws CacheException
  48. */
  49. public function getCacheKey()
  50. {
  51. if ($this->cacheKey === null) {
  52. throw CacheException::noCacheKey();
  53. }
  54. return $this->cacheKey;
  55. }
  56. /**
  57. * Generates the real cache key from query, params, types and connection parameters.
  58. *
  59. * @param string $query
  60. * @param mixed[] $params
  61. * @param int[]|string[] $types
  62. * @param mixed[] $connectionParams
  63. *
  64. * @return string[]
  65. */
  66. public function generateCacheKeys($query, $params, $types, array $connectionParams = [])
  67. {
  68. $realCacheKey = 'query=' . $query .
  69. '&params=' . serialize($params) .
  70. '&types=' . serialize($types) .
  71. '&connectionParams=' . hash('sha256', serialize($connectionParams));
  72. // should the key be automatically generated using the inputs or is the cache key set?
  73. if ($this->cacheKey === null) {
  74. $cacheKey = sha1($realCacheKey);
  75. } else {
  76. $cacheKey = $this->cacheKey;
  77. }
  78. return [$cacheKey, $realCacheKey];
  79. }
  80. /**
  81. * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  82. */
  83. public function setResultCacheDriver(Cache $cache)
  84. {
  85. return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
  86. }
  87. /**
  88. * @param string|null $cacheKey
  89. *
  90. * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  91. */
  92. public function setCacheKey($cacheKey)
  93. {
  94. return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
  95. }
  96. /**
  97. * @param int $lifetime
  98. *
  99. * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  100. */
  101. public function setLifetime($lifetime)
  102. {
  103. return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
  104. }
  105. }