Psr6
2 years ago
Cache.php
4 years ago
CacheProvider.php
4 years ago
ClearableCache.php
4 years ago
FlushableCache.php
4 years ago
MultiDeleteCache.php
4 years ago
MultiGetCache.php
4 years ago
MultiOperationCache.php
4 years ago
MultiPutCache.php
4 years ago
index.php
3 years ago
CacheProvider.php
146 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\Common\Cache; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use function array_combine; |
| 5 | use function array_key_exists; |
| 6 | use function array_map; |
| 7 | use function sprintf; |
| 8 | abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiOperationCache |
| 9 | { |
| 10 | public const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]'; |
| 11 | private $namespace = ''; |
| 12 | private $namespaceVersion; |
| 13 | public function setNamespace($namespace) |
| 14 | { |
| 15 | $this->namespace = (string) $namespace; |
| 16 | $this->namespaceVersion = null; |
| 17 | } |
| 18 | public function getNamespace() |
| 19 | { |
| 20 | return $this->namespace; |
| 21 | } |
| 22 | public function fetch($id) |
| 23 | { |
| 24 | return $this->doFetch($this->getNamespacedId($id)); |
| 25 | } |
| 26 | public function fetchMultiple(array $keys) |
| 27 | { |
| 28 | if (empty($keys)) { |
| 29 | return []; |
| 30 | } |
| 31 | // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys |
| 32 | $namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys)); |
| 33 | $items = $this->doFetchMultiple($namespacedKeys); |
| 34 | $foundItems = []; |
| 35 | // no internal array function supports this sort of mapping: needs to be iterative |
| 36 | // this filters and combines keys in one pass |
| 37 | foreach ($namespacedKeys as $requestedKey => $namespacedKey) { |
| 38 | if (!isset($items[$namespacedKey]) && !array_key_exists($namespacedKey, $items)) { |
| 39 | continue; |
| 40 | } |
| 41 | $foundItems[$requestedKey] = $items[$namespacedKey]; |
| 42 | } |
| 43 | return $foundItems; |
| 44 | } |
| 45 | public function saveMultiple(array $keysAndValues, $lifetime = 0) |
| 46 | { |
| 47 | $namespacedKeysAndValues = []; |
| 48 | foreach ($keysAndValues as $key => $value) { |
| 49 | $namespacedKeysAndValues[$this->getNamespacedId($key)] = $value; |
| 50 | } |
| 51 | return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime); |
| 52 | } |
| 53 | public function contains($id) |
| 54 | { |
| 55 | return $this->doContains($this->getNamespacedId($id)); |
| 56 | } |
| 57 | public function save($id, $data, $lifeTime = 0) |
| 58 | { |
| 59 | return $this->doSave($this->getNamespacedId($id), $data, $lifeTime); |
| 60 | } |
| 61 | public function deleteMultiple(array $keys) |
| 62 | { |
| 63 | return $this->doDeleteMultiple(array_map([$this, 'getNamespacedId'], $keys)); |
| 64 | } |
| 65 | public function delete($id) |
| 66 | { |
| 67 | return $this->doDelete($this->getNamespacedId($id)); |
| 68 | } |
| 69 | public function getStats() |
| 70 | { |
| 71 | return $this->doGetStats(); |
| 72 | } |
| 73 | public function flushAll() |
| 74 | { |
| 75 | return $this->doFlush(); |
| 76 | } |
| 77 | public function deleteAll() |
| 78 | { |
| 79 | $namespaceCacheKey = $this->getNamespaceCacheKey(); |
| 80 | $namespaceVersion = $this->getNamespaceVersion() + 1; |
| 81 | if ($this->doSave($namespaceCacheKey, $namespaceVersion)) { |
| 82 | $this->namespaceVersion = $namespaceVersion; |
| 83 | return \true; |
| 84 | } |
| 85 | return \false; |
| 86 | } |
| 87 | private function getNamespacedId(string $id) : string |
| 88 | { |
| 89 | $namespaceVersion = $this->getNamespaceVersion(); |
| 90 | return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion); |
| 91 | } |
| 92 | private function getNamespaceCacheKey() : string |
| 93 | { |
| 94 | return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); |
| 95 | } |
| 96 | private function getNamespaceVersion() : int |
| 97 | { |
| 98 | if ($this->namespaceVersion !== null) { |
| 99 | return $this->namespaceVersion; |
| 100 | } |
| 101 | $namespaceCacheKey = $this->getNamespaceCacheKey(); |
| 102 | $this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1; |
| 103 | return $this->namespaceVersion; |
| 104 | } |
| 105 | protected function doFetchMultiple(array $keys) |
| 106 | { |
| 107 | $returnValues = []; |
| 108 | foreach ($keys as $key) { |
| 109 | $item = $this->doFetch($key); |
| 110 | if ($item === \false && !$this->doContains($key)) { |
| 111 | continue; |
| 112 | } |
| 113 | $returnValues[$key] = $item; |
| 114 | } |
| 115 | return $returnValues; |
| 116 | } |
| 117 | protected abstract function doFetch($id); |
| 118 | protected abstract function doContains($id); |
| 119 | protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
| 120 | { |
| 121 | $success = \true; |
| 122 | foreach ($keysAndValues as $key => $value) { |
| 123 | if ($this->doSave($key, $value, $lifetime)) { |
| 124 | continue; |
| 125 | } |
| 126 | $success = \false; |
| 127 | } |
| 128 | return $success; |
| 129 | } |
| 130 | protected abstract function doSave($id, $data, $lifeTime = 0); |
| 131 | protected function doDeleteMultiple(array $keys) |
| 132 | { |
| 133 | $success = \true; |
| 134 | foreach ($keys as $key) { |
| 135 | if ($this->doDelete($key)) { |
| 136 | continue; |
| 137 | } |
| 138 | $success = \false; |
| 139 | } |
| 140 | return $success; |
| 141 | } |
| 142 | protected abstract function doDelete($id); |
| 143 | protected abstract function doFlush(); |
| 144 | protected abstract function doGetStats(); |
| 145 | } |
| 146 |