DefaultMultiGetRegion.php
9 months ago
DefaultRegion.php
9 months ago
FileLockRegion.php
9 months ago
UpdateTimestampCache.php
9 months ago
index.php
9 months ago
DefaultRegion.php
112 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Cache\Region; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Closure; |
| 6 | use MailPoetVendor\Doctrine\Common\Cache\Cache as LegacyCache; |
| 7 | use MailPoetVendor\Doctrine\Common\Cache\CacheProvider; |
| 8 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\CacheAdapter; |
| 9 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\DoctrineProvider; |
| 10 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 11 | use MailPoetVendor\Doctrine\ORM\Cache\CacheEntry; |
| 12 | use MailPoetVendor\Doctrine\ORM\Cache\CacheKey; |
| 13 | use MailPoetVendor\Doctrine\ORM\Cache\CollectionCacheEntry; |
| 14 | use MailPoetVendor\Doctrine\ORM\Cache\Lock; |
| 15 | use MailPoetVendor\Doctrine\ORM\Cache\Region; |
| 16 | use MailPoetVendor\Psr\Cache\CacheItemInterface; |
| 17 | use MailPoetVendor\Psr\Cache\CacheItemPoolInterface; |
| 18 | use Traversable; |
| 19 | use TypeError; |
| 20 | use function array_map; |
| 21 | use function get_debug_type; |
| 22 | use function iterator_to_array; |
| 23 | use function sprintf; |
| 24 | use function strtr; |
| 25 | class DefaultRegion implements Region |
| 26 | { |
| 27 | public const REGION_KEY_SEPARATOR = '_'; |
| 28 | private const REGION_PREFIX = 'DC2_REGION_'; |
| 29 | protected $cache; |
| 30 | protected $name; |
| 31 | protected $lifetime = 0; |
| 32 | private $cacheItemPool; |
| 33 | public function __construct(string $name, $cacheItemPool, int $lifetime = 0) |
| 34 | { |
| 35 | if ($cacheItemPool instanceof LegacyCache) { |
| 36 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9322', 'Passing an instance of %s to %s is deprecated, pass a %s instead.', get_debug_type($cacheItemPool), __METHOD__, CacheItemPoolInterface::class); |
| 37 | // @phpstan-ignore property.deprecated |
| 38 | $this->cache = $cacheItemPool; |
| 39 | $this->cacheItemPool = CacheAdapter::wrap($cacheItemPool); |
| 40 | } elseif (!$cacheItemPool instanceof CacheItemPoolInterface) { |
| 41 | throw new TypeError(sprintf('%s: Parameter #2 is expected to be an instance of %s, got %s.', __METHOD__, CacheItemPoolInterface::class, get_debug_type($cacheItemPool))); |
| 42 | } else { |
| 43 | // @phpstan-ignore property.deprecated |
| 44 | $this->cache = DoctrineProvider::wrap($cacheItemPool); |
| 45 | $this->cacheItemPool = $cacheItemPool; |
| 46 | } |
| 47 | $this->name = $name; |
| 48 | $this->lifetime = $lifetime; |
| 49 | } |
| 50 | public function getName() |
| 51 | { |
| 52 | return $this->name; |
| 53 | } |
| 54 | public function getCache() |
| 55 | { |
| 56 | return $this->cache; |
| 57 | } |
| 58 | public function contains(CacheKey $key) |
| 59 | { |
| 60 | return $this->cacheItemPool->hasItem($this->getCacheEntryKey($key)); |
| 61 | } |
| 62 | public function get(CacheKey $key) |
| 63 | { |
| 64 | $item = $this->cacheItemPool->getItem($this->getCacheEntryKey($key)); |
| 65 | $entry = $item->isHit() ? $item->get() : null; |
| 66 | if (!$entry instanceof CacheEntry) { |
| 67 | return null; |
| 68 | } |
| 69 | return $entry; |
| 70 | } |
| 71 | public function getMultiple(CollectionCacheEntry $collection) |
| 72 | { |
| 73 | $keys = array_map(Closure::fromCallable([$this, 'getCacheEntryKey']), $collection->identifiers); |
| 74 | $items = $this->cacheItemPool->getItems($keys); |
| 75 | if ($items instanceof Traversable) { |
| 76 | $items = iterator_to_array($items); |
| 77 | } |
| 78 | $result = []; |
| 79 | foreach ($keys as $arrayKey => $cacheKey) { |
| 80 | if (!isset($items[$cacheKey]) || !$items[$cacheKey]->isHit()) { |
| 81 | return null; |
| 82 | } |
| 83 | $entry = $items[$cacheKey]->get(); |
| 84 | if (!$entry instanceof CacheEntry) { |
| 85 | return null; |
| 86 | } |
| 87 | $result[$arrayKey] = $entry; |
| 88 | } |
| 89 | return $result; |
| 90 | } |
| 91 | public function put(CacheKey $key, CacheEntry $entry, ?Lock $lock = null) |
| 92 | { |
| 93 | $item = $this->cacheItemPool->getItem($this->getCacheEntryKey($key))->set($entry); |
| 94 | if ($this->lifetime > 0) { |
| 95 | $item->expiresAfter($this->lifetime); |
| 96 | } |
| 97 | return $this->cacheItemPool->save($item); |
| 98 | } |
| 99 | public function evict(CacheKey $key) |
| 100 | { |
| 101 | return $this->cacheItemPool->deleteItem($this->getCacheEntryKey($key)); |
| 102 | } |
| 103 | public function evictAll() |
| 104 | { |
| 105 | return $this->cacheItemPool->clear(self::REGION_PREFIX . $this->name); |
| 106 | } |
| 107 | protected function getCacheEntryKey(CacheKey $key) |
| 108 | { |
| 109 | return self::REGION_PREFIX . $this->name . self::REGION_KEY_SEPARATOR . strtr($key->hash, '{}()/\\@:', '________'); |
| 110 | } |
| 111 | } |
| 112 |