| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Cache;
|
|
|
4 |
|
|
|
5 |
use Doctrine\Common\Cache\Cache;
|
|
|
6 |
use Doctrine\Common\Cache\Psr6\CacheAdapter;
|
|
|
7 |
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
|
|
|
8 |
use Doctrine\DBAL\Types\Type;
|
|
|
9 |
use Doctrine\Deprecations\Deprecation;
|
|
|
10 |
use Psr\Cache\CacheItemPoolInterface;
|
|
|
11 |
use TypeError;
|
|
|
12 |
|
|
|
13 |
use function get_class;
|
|
|
14 |
use function hash;
|
|
|
15 |
use function serialize;
|
|
|
16 |
use function sha1;
|
|
|
17 |
use function sprintf;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Query Cache Profile handles the data relevant for query caching.
|
|
|
21 |
*
|
|
|
22 |
* It is a value object, setter methods return NEW instances.
|
|
|
23 |
*/
|
|
|
24 |
class QueryCacheProfile
|
|
|
25 |
{
|
|
|
26 |
private ?CacheItemPoolInterface $resultCache = null;
|
|
|
27 |
|
|
|
28 |
/** @var int */
|
|
|
29 |
private $lifetime;
|
|
|
30 |
|
|
|
31 |
/** @var string|null */
|
|
|
32 |
private $cacheKey;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @param int $lifetime
|
|
|
36 |
* @param string|null $cacheKey
|
|
|
37 |
* @param CacheItemPoolInterface|Cache|null $resultCache
|
|
|
38 |
*/
|
|
|
39 |
public function __construct($lifetime = 0, $cacheKey = null, ?object $resultCache = null)
|
|
|
40 |
{
|
|
|
41 |
$this->lifetime = $lifetime;
|
|
|
42 |
$this->cacheKey = $cacheKey;
|
|
|
43 |
if ($resultCache instanceof CacheItemPoolInterface) {
|
|
|
44 |
$this->resultCache = $resultCache;
|
|
|
45 |
} elseif ($resultCache instanceof Cache) {
|
|
|
46 |
Deprecation::trigger(
|
|
|
47 |
'doctrine/dbal',
|
|
|
48 |
'https://github.com/doctrine/dbal/pull/4620',
|
|
|
49 |
'Passing an instance of %s to %s as $resultCache is deprecated. Pass an instance of %s instead.',
|
|
|
50 |
Cache::class,
|
|
|
51 |
__METHOD__,
|
|
|
52 |
CacheItemPoolInterface::class,
|
|
|
53 |
);
|
|
|
54 |
|
|
|
55 |
$this->resultCache = CacheAdapter::wrap($resultCache);
|
|
|
56 |
} elseif ($resultCache !== null) {
|
|
|
57 |
throw new TypeError(sprintf(
|
|
|
58 |
'$resultCache: Expected either null or an instance of %s or %s, got %s.',
|
|
|
59 |
CacheItemPoolInterface::class,
|
|
|
60 |
Cache::class,
|
|
|
61 |
get_class($resultCache),
|
|
|
62 |
));
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function getResultCache(): ?CacheItemPoolInterface
|
|
|
67 |
{
|
|
|
68 |
return $this->resultCache;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @deprecated Use {@see getResultCache()} instead.
|
|
|
73 |
*
|
|
|
74 |
* @return Cache|null
|
|
|
75 |
*/
|
|
|
76 |
public function getResultCacheDriver()
|
|
|
77 |
{
|
|
|
78 |
Deprecation::trigger(
|
|
|
79 |
'doctrine/dbal',
|
|
|
80 |
'https://github.com/doctrine/dbal/pull/4620',
|
|
|
81 |
'%s is deprecated, call getResultCache() instead.',
|
|
|
82 |
__METHOD__,
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
return $this->resultCache !== null ? DoctrineProvider::wrap($this->resultCache) : null;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/** @return int */
|
|
|
89 |
public function getLifetime()
|
|
|
90 |
{
|
|
|
91 |
return $this->lifetime;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @return string
|
|
|
96 |
*
|
|
|
97 |
* @throws CacheException
|
|
|
98 |
*/
|
|
|
99 |
public function getCacheKey()
|
|
|
100 |
{
|
|
|
101 |
if ($this->cacheKey === null) {
|
|
|
102 |
throw CacheException::noCacheKey();
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return $this->cacheKey;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Generates the real cache key from query, params, types and connection parameters.
|
|
|
110 |
*
|
|
|
111 |
* @param string $sql
|
|
|
112 |
* @param list<mixed>|array<string, mixed> $params
|
|
|
113 |
* @param array<int, Type|int|string|null>|array<string, Type|int|string|null> $types
|
|
|
114 |
* @param array<string, mixed> $connectionParams
|
|
|
115 |
*
|
|
|
116 |
* @return array{string, string}
|
|
|
117 |
*/
|
|
|
118 |
public function generateCacheKeys($sql, $params, $types, array $connectionParams = [])
|
|
|
119 |
{
|
|
|
120 |
if (isset($connectionParams['password'])) {
|
|
|
121 |
unset($connectionParams['password']);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$realCacheKey = 'query=' . $sql .
|
|
|
125 |
'¶ms=' . serialize($params) .
|
|
|
126 |
'&types=' . serialize($types) .
|
|
|
127 |
'&connectionParams=' . hash('sha256', serialize($connectionParams));
|
|
|
128 |
|
|
|
129 |
// should the key be automatically generated using the inputs or is the cache key set?
|
|
|
130 |
$cacheKey = $this->cacheKey ?? sha1($realCacheKey);
|
|
|
131 |
|
|
|
132 |
return [$cacheKey, $realCacheKey];
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public function setResultCache(CacheItemPoolInterface $cache): QueryCacheProfile
|
|
|
136 |
{
|
|
|
137 |
return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* @deprecated Use {@see setResultCache()} instead.
|
|
|
142 |
*
|
|
|
143 |
* @return QueryCacheProfile
|
|
|
144 |
*/
|
|
|
145 |
public function setResultCacheDriver(Cache $cache)
|
|
|
146 |
{
|
|
|
147 |
Deprecation::trigger(
|
|
|
148 |
'doctrine/dbal',
|
|
|
149 |
'https://github.com/doctrine/dbal/pull/4620',
|
|
|
150 |
'%s is deprecated, call setResultCache() instead.',
|
|
|
151 |
__METHOD__,
|
|
|
152 |
);
|
|
|
153 |
|
|
|
154 |
return new QueryCacheProfile($this->lifetime, $this->cacheKey, CacheAdapter::wrap($cache));
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* @param string|null $cacheKey
|
|
|
159 |
*
|
|
|
160 |
* @return QueryCacheProfile
|
|
|
161 |
*/
|
|
|
162 |
public function setCacheKey($cacheKey)
|
|
|
163 |
{
|
|
|
164 |
return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCache);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* @param int $lifetime
|
|
|
169 |
*
|
|
|
170 |
* @return QueryCacheProfile
|
|
|
171 |
*/
|
|
|
172 |
public function setLifetime($lifetime)
|
|
|
173 |
{
|
|
|
174 |
return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCache);
|
|
|
175 |
}
|
|
|
176 |
}
|