ArrayResult.php
2 years ago
CacheException.php
2 years ago
QueryCacheProfile.php
9 months ago
index.php
2 years ago
QueryCacheProfile.php
90 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL\Cache; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\Common\Cache\Cache; |
| 5 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\CacheAdapter; |
| 6 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\DoctrineProvider; |
| 7 | use MailPoetVendor\Doctrine\DBAL\Types\Type; |
| 8 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 9 | use MailPoetVendor\Psr\Cache\CacheItemPoolInterface; |
| 10 | use RuntimeException; |
| 11 | use TypeError; |
| 12 | use function class_exists; |
| 13 | use function get_class; |
| 14 | use function hash; |
| 15 | use function serialize; |
| 16 | use function sha1; |
| 17 | use function sprintf; |
| 18 | class QueryCacheProfile |
| 19 | { |
| 20 | private ?CacheItemPoolInterface $resultCache = null; |
| 21 | private $lifetime; |
| 22 | private $cacheKey; |
| 23 | public function __construct($lifetime = 0, $cacheKey = null, ?object $resultCache = null) |
| 24 | { |
| 25 | $this->lifetime = $lifetime; |
| 26 | $this->cacheKey = $cacheKey; |
| 27 | if ($resultCache instanceof CacheItemPoolInterface) { |
| 28 | $this->resultCache = $resultCache; |
| 29 | } elseif ($resultCache instanceof Cache) { |
| 30 | Deprecation::trigger('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4620', 'Passing an instance of %s to %s as $resultCache is deprecated. Pass an instance of %s instead.', Cache::class, __METHOD__, CacheItemPoolInterface::class); |
| 31 | $this->resultCache = CacheAdapter::wrap($resultCache); |
| 32 | } elseif ($resultCache !== null) { |
| 33 | throw new TypeError(sprintf('$resultCache: Expected either null or an instance of %s or %s, got %s.', CacheItemPoolInterface::class, Cache::class, get_class($resultCache))); |
| 34 | } |
| 35 | } |
| 36 | public function getResultCache() : ?CacheItemPoolInterface |
| 37 | { |
| 38 | return $this->resultCache; |
| 39 | } |
| 40 | public function getResultCacheDriver() |
| 41 | { |
| 42 | Deprecation::trigger('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4620', '%s is deprecated, call getResultCache() instead.', __METHOD__); |
| 43 | if ($this->resultCache === null) { |
| 44 | return null; |
| 45 | } |
| 46 | if (!class_exists(DoctrineProvider::class)) { |
| 47 | throw new RuntimeException(sprintf('Calling %s() is not supported if the doctrine/cache package is not installed. ' . 'Try running "composer require doctrine/cache" or migrate cache access to PSR-6.', __METHOD__)); |
| 48 | } |
| 49 | return DoctrineProvider::wrap($this->resultCache); |
| 50 | } |
| 51 | public function getLifetime() |
| 52 | { |
| 53 | return $this->lifetime; |
| 54 | } |
| 55 | public function getCacheKey() |
| 56 | { |
| 57 | if ($this->cacheKey === null) { |
| 58 | throw CacheException::noCacheKey(); |
| 59 | } |
| 60 | return $this->cacheKey; |
| 61 | } |
| 62 | public function generateCacheKeys($sql, $params, $types, array $connectionParams = []) |
| 63 | { |
| 64 | if (isset($connectionParams['password'])) { |
| 65 | unset($connectionParams['password']); |
| 66 | } |
| 67 | $realCacheKey = 'query=' . $sql . '¶ms=' . serialize($params) . '&types=' . serialize($types) . '&connectionParams=' . hash('sha256', serialize($connectionParams)); |
| 68 | // should the key be automatically generated using the inputs or is the cache key set? |
| 69 | $cacheKey = $this->cacheKey ?? sha1($realCacheKey); |
| 70 | return [$cacheKey, $realCacheKey]; |
| 71 | } |
| 72 | public function setResultCache(CacheItemPoolInterface $cache) : QueryCacheProfile |
| 73 | { |
| 74 | return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache); |
| 75 | } |
| 76 | public function setResultCacheDriver(Cache $cache) |
| 77 | { |
| 78 | Deprecation::trigger('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/4620', '%s is deprecated, call setResultCache() instead.', __METHOD__); |
| 79 | return new QueryCacheProfile($this->lifetime, $this->cacheKey, CacheAdapter::wrap($cache)); |
| 80 | } |
| 81 | public function setCacheKey($cacheKey) |
| 82 | { |
| 83 | return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCache); |
| 84 | } |
| 85 | public function setLifetime($lifetime) |
| 86 | { |
| 87 | return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCache); |
| 88 | } |
| 89 | } |
| 90 |