QueryCacheProfile.php 3.1 KB

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