independent-analytics
/
vendor
/
doctrine
/
cache
/
lib
/
Doctrine
/
Common
/
Cache
/
CacheProvider.php
independent-analytics
/
vendor
/
doctrine
/
cache
/
lib
/
Doctrine
/
Common
/
Cache
Last commit date
Psr6
2 years ago
ApcCache.php
2 years ago
ApcuCache.php
2 years ago
ArrayCache.php
2 years ago
Cache.php
2 years ago
CacheProvider.php
2 years ago
ChainCache.php
2 years ago
ClearableCache.php
2 years ago
CouchbaseBucketCache.php
2 years ago
CouchbaseCache.php
2 years ago
ExtMongoDBCache.php
2 years ago
FileCache.php
2 years ago
FilesystemCache.php
2 years ago
FlushableCache.php
2 years ago
InvalidCacheId.php
2 years ago
LegacyMongoDBCache.php
2 years ago
MemcacheCache.php
2 years ago
MemcachedCache.php
2 years ago
MongoDBCache.php
2 years ago
MultiDeleteCache.php
2 years ago
MultiGetCache.php
2 years ago
MultiOperationCache.php
2 years ago
MultiPutCache.php
2 years ago
PhpFileCache.php
2 years ago
PredisCache.php
2 years ago
RedisCache.php
2 years ago
SQLite3Cache.php
2 years ago
Version.php
2 years ago
VoidCache.php
2 years ago
WinCacheCache.php
2 years ago
XcacheCache.php
2 years ago
ZendDataCache.php
2 years ago
CacheProvider.php
280 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Doctrine\Common\Cache; |
| 4 | |
| 5 | use function array_combine; |
| 6 | use function array_key_exists; |
| 7 | use function array_map; |
| 8 | use function sprintf; |
| 9 | /** |
| 10 | * Base class for cache provider implementations. |
| 11 | * @internal |
| 12 | */ |
| 13 | abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiOperationCache |
| 14 | { |
| 15 | public const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]'; |
| 16 | /** |
| 17 | * The namespace to prefix all cache ids with. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | private $namespace = ''; |
| 22 | /** |
| 23 | * The namespace version. |
| 24 | * |
| 25 | * @var int|null |
| 26 | */ |
| 27 | private $namespaceVersion; |
| 28 | /** |
| 29 | * Sets the namespace to prefix all cache ids with. |
| 30 | * |
| 31 | * @param string $namespace |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function setNamespace($namespace) |
| 36 | { |
| 37 | $this->namespace = (string) $namespace; |
| 38 | $this->namespaceVersion = null; |
| 39 | } |
| 40 | /** |
| 41 | * Retrieves the namespace that prefixes all cache ids. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | public function getNamespace() |
| 46 | { |
| 47 | return $this->namespace; |
| 48 | } |
| 49 | /** |
| 50 | * {@inheritdoc} |
| 51 | */ |
| 52 | public function fetch($id) |
| 53 | { |
| 54 | return $this->doFetch($this->getNamespacedId($id)); |
| 55 | } |
| 56 | /** |
| 57 | * {@inheritdoc} |
| 58 | */ |
| 59 | public function fetchMultiple(array $keys) |
| 60 | { |
| 61 | if (empty($keys)) { |
| 62 | return []; |
| 63 | } |
| 64 | // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys |
| 65 | $namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys)); |
| 66 | $items = $this->doFetchMultiple($namespacedKeys); |
| 67 | $foundItems = []; |
| 68 | // no internal array function supports this sort of mapping: needs to be iterative |
| 69 | // this filters and combines keys in one pass |
| 70 | foreach ($namespacedKeys as $requestedKey => $namespacedKey) { |
| 71 | if (!isset($items[$namespacedKey]) && !array_key_exists($namespacedKey, $items)) { |
| 72 | continue; |
| 73 | } |
| 74 | $foundItems[$requestedKey] = $items[$namespacedKey]; |
| 75 | } |
| 76 | return $foundItems; |
| 77 | } |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function saveMultiple(array $keysAndValues, $lifetime = 0) |
| 82 | { |
| 83 | $namespacedKeysAndValues = []; |
| 84 | foreach ($keysAndValues as $key => $value) { |
| 85 | $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value; |
| 86 | } |
| 87 | return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime); |
| 88 | } |
| 89 | /** |
| 90 | * {@inheritdoc} |
| 91 | */ |
| 92 | public function contains($id) |
| 93 | { |
| 94 | return $this->doContains($this->getNamespacedId($id)); |
| 95 | } |
| 96 | /** |
| 97 | * {@inheritdoc} |
| 98 | */ |
| 99 | public function save($id, $data, $lifeTime = 0) |
| 100 | { |
| 101 | return $this->doSave($this->getNamespacedId($id), $data, $lifeTime); |
| 102 | } |
| 103 | /** |
| 104 | * {@inheritdoc} |
| 105 | */ |
| 106 | public function deleteMultiple(array $keys) |
| 107 | { |
| 108 | return $this->doDeleteMultiple(array_map([$this, 'getNamespacedId'], $keys)); |
| 109 | } |
| 110 | /** |
| 111 | * {@inheritdoc} |
| 112 | */ |
| 113 | public function delete($id) |
| 114 | { |
| 115 | return $this->doDelete($this->getNamespacedId($id)); |
| 116 | } |
| 117 | /** |
| 118 | * {@inheritdoc} |
| 119 | */ |
| 120 | public function getStats() |
| 121 | { |
| 122 | return $this->doGetStats(); |
| 123 | } |
| 124 | /** |
| 125 | * {@inheritDoc} |
| 126 | */ |
| 127 | public function flushAll() |
| 128 | { |
| 129 | return $this->doFlush(); |
| 130 | } |
| 131 | /** |
| 132 | * {@inheritDoc} |
| 133 | */ |
| 134 | public function deleteAll() |
| 135 | { |
| 136 | $namespaceCacheKey = $this->getNamespaceCacheKey(); |
| 137 | $namespaceVersion = $this->getNamespaceVersion() + 1; |
| 138 | if ($this->doSave($namespaceCacheKey, $namespaceVersion)) { |
| 139 | $this->namespaceVersion = $namespaceVersion; |
| 140 | return \true; |
| 141 | } |
| 142 | return \false; |
| 143 | } |
| 144 | /** |
| 145 | * Prefixes the passed id with the configured namespace value. |
| 146 | * |
| 147 | * @param string $id The id to namespace. |
| 148 | * |
| 149 | * @return string The namespaced id. |
| 150 | */ |
| 151 | private function getNamespacedId(string $id) : string |
| 152 | { |
| 153 | $namespaceVersion = $this->getNamespaceVersion(); |
| 154 | return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion); |
| 155 | } |
| 156 | /** |
| 157 | * Returns the namespace cache key. |
| 158 | */ |
| 159 | private function getNamespaceCacheKey() : string |
| 160 | { |
| 161 | return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); |
| 162 | } |
| 163 | /** |
| 164 | * Returns the namespace version. |
| 165 | */ |
| 166 | private function getNamespaceVersion() : int |
| 167 | { |
| 168 | if ($this->namespaceVersion !== null) { |
| 169 | return $this->namespaceVersion; |
| 170 | } |
| 171 | $namespaceCacheKey = $this->getNamespaceCacheKey(); |
| 172 | $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1; |
| 173 | return $this->namespaceVersion; |
| 174 | } |
| 175 | /** |
| 176 | * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it. |
| 177 | * |
| 178 | * @param string[] $keys Array of keys to retrieve from cache |
| 179 | * |
| 180 | * @return mixed[] Array of values retrieved for the given keys. |
| 181 | */ |
| 182 | protected function doFetchMultiple(array $keys) |
| 183 | { |
| 184 | $returnValues = []; |
| 185 | foreach ($keys as $key) { |
| 186 | $item = $this->doFetch($key); |
| 187 | if ($item === \false && !$this->doContains($key)) { |
| 188 | continue; |
| 189 | } |
| 190 | $returnValues[$key] = $item; |
| 191 | } |
| 192 | return $returnValues; |
| 193 | } |
| 194 | /** |
| 195 | * Fetches an entry from the cache. |
| 196 | * |
| 197 | * @param string $id The id of the cache entry to fetch. |
| 198 | * |
| 199 | * @return mixed|false The cached data or FALSE, if no cache entry exists for the given id. |
| 200 | */ |
| 201 | protected abstract function doFetch($id); |
| 202 | /** |
| 203 | * Tests if an entry exists in the cache. |
| 204 | * |
| 205 | * @param string $id The cache id of the entry to check for. |
| 206 | * |
| 207 | * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise. |
| 208 | */ |
| 209 | protected abstract function doContains($id); |
| 210 | /** |
| 211 | * Default implementation of doSaveMultiple. Each driver that supports multi-put should override it. |
| 212 | * |
| 213 | * @param mixed[] $keysAndValues Array of keys and values to save in cache |
| 214 | * @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these |
| 215 | * cache entries (0 => infinite lifeTime). |
| 216 | * |
| 217 | * @return bool TRUE if the operation was successful, FALSE if it wasn't. |
| 218 | */ |
| 219 | protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
| 220 | { |
| 221 | $success = \true; |
| 222 | foreach ($keysAndValues as $key => $value) { |
| 223 | if ($this->doSave($key, $value, $lifetime)) { |
| 224 | continue; |
| 225 | } |
| 226 | $success = \false; |
| 227 | } |
| 228 | return $success; |
| 229 | } |
| 230 | /** |
| 231 | * Puts data into the cache. |
| 232 | * |
| 233 | * @param string $id The cache id. |
| 234 | * @param string $data The cache entry/data. |
| 235 | * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this |
| 236 | * cache entry (0 => infinite lifeTime). |
| 237 | * |
| 238 | * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise. |
| 239 | */ |
| 240 | protected abstract function doSave($id, $data, $lifeTime = 0); |
| 241 | /** |
| 242 | * Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it. |
| 243 | * |
| 244 | * @param string[] $keys Array of keys to delete from cache |
| 245 | * |
| 246 | * @return bool TRUE if the operation was successful, FALSE if it wasn't |
| 247 | */ |
| 248 | protected function doDeleteMultiple(array $keys) |
| 249 | { |
| 250 | $success = \true; |
| 251 | foreach ($keys as $key) { |
| 252 | if ($this->doDelete($key)) { |
| 253 | continue; |
| 254 | } |
| 255 | $success = \false; |
| 256 | } |
| 257 | return $success; |
| 258 | } |
| 259 | /** |
| 260 | * Deletes a cache entry. |
| 261 | * |
| 262 | * @param string $id The cache id. |
| 263 | * |
| 264 | * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
| 265 | */ |
| 266 | protected abstract function doDelete($id); |
| 267 | /** |
| 268 | * Flushes all cache entries. |
| 269 | * |
| 270 | * @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise. |
| 271 | */ |
| 272 | protected abstract function doFlush(); |
| 273 | /** |
| 274 | * Retrieves cached information from the data store. |
| 275 | * |
| 276 | * @return mixed[]|null An associative array with server's statistics if available, NULL otherwise. |
| 277 | */ |
| 278 | protected abstract function doGetStats(); |
| 279 | } |
| 280 |