diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php b/vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php new file mode 100644 index 0000000..b085f6c --- /dev/null +++ b/vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php | |||
@@ -0,0 +1,91 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Cache; | ||
6 | |||
7 | use Doctrine\DBAL\Cache\Exception\NoCacheKey; | ||
8 | use Doctrine\DBAL\Connection; | ||
9 | use Psr\Cache\CacheItemPoolInterface; | ||
10 | |||
11 | use function hash; | ||
12 | use function serialize; | ||
13 | use function sha1; | ||
14 | |||
15 | /** | ||
16 | * Query Cache Profile handles the data relevant for query caching. | ||
17 | * | ||
18 | * It is a value object, setter methods return NEW instances. | ||
19 | * | ||
20 | * @psalm-import-type WrapperParameterType from Connection | ||
21 | */ | ||
22 | class QueryCacheProfile | ||
23 | { | ||
24 | public function __construct( | ||
25 | private readonly int $lifetime = 0, | ||
26 | private readonly ?string $cacheKey = null, | ||
27 | private readonly ?CacheItemPoolInterface $resultCache = null, | ||
28 | ) { | ||
29 | } | ||
30 | |||
31 | public function getResultCache(): ?CacheItemPoolInterface | ||
32 | { | ||
33 | return $this->resultCache; | ||
34 | } | ||
35 | |||
36 | public function getLifetime(): int | ||
37 | { | ||
38 | return $this->lifetime; | ||
39 | } | ||
40 | |||
41 | /** @throws CacheException */ | ||
42 | public function getCacheKey(): string | ||
43 | { | ||
44 | if ($this->cacheKey === null) { | ||
45 | throw NoCacheKey::new(); | ||
46 | } | ||
47 | |||
48 | return $this->cacheKey; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Generates the real cache key from query, params, types and connection parameters. | ||
53 | * | ||
54 | * @param list<mixed>|array<string, mixed> $params | ||
55 | * @param array<string, mixed> $connectionParams | ||
56 | * @psalm-param array<int, WrapperParameterType>|array<string, WrapperParameterType> $types | ||
57 | * | ||
58 | * @return array{string, string} | ||
59 | */ | ||
60 | public function generateCacheKeys(string $sql, array $params, array $types, array $connectionParams = []): array | ||
61 | { | ||
62 | if (isset($connectionParams['password'])) { | ||
63 | unset($connectionParams['password']); | ||
64 | } | ||
65 | |||
66 | $realCacheKey = 'query=' . $sql . | ||
67 | '¶ms=' . serialize($params) . | ||
68 | '&types=' . serialize($types) . | ||
69 | '&connectionParams=' . hash('sha256', serialize($connectionParams)); | ||
70 | |||
71 | // should the key be automatically generated using the inputs or is the cache key set? | ||
72 | $cacheKey = $this->cacheKey ?? sha1($realCacheKey); | ||
73 | |||
74 | return [$cacheKey, $realCacheKey]; | ||
75 | } | ||
76 | |||
77 | public function setResultCache(CacheItemPoolInterface $cache): QueryCacheProfile | ||
78 | { | ||
79 | return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache); | ||
80 | } | ||
81 | |||
82 | public function setCacheKey(?string $cacheKey): self | ||
83 | { | ||
84 | return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCache); | ||
85 | } | ||
86 | |||
87 | public function setLifetime(int $lifetime): self | ||
88 | { | ||
89 | return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCache); | ||
90 | } | ||
91 | } | ||