Annotations
3 years ago
EntityTraits
2 years ago
EventListeners
2 months ago
Middlewares
1 month ago
Types
2 months ago
Validator
2 months ago
WPDB
2 months ago
ArrayCache.php
2 months ago
CacheOnlyMappingDriver.php
3 years ago
ConfigurationFactory.php
3 years ago
ConnectionFactory.php
1 year ago
EntityManagerFactory.php
2 years ago
MetadataCache.php
2 months ago
PSRArrayCache.php
2 months ago
PSRCacheInvalidArgumentException.php
3 years ago
PSRCacheItem.php
2 months ago
PSRMetadataCache.php
3 years ago
ProxyClassNameResolver.php
3 years ago
Repository.php
8 months ago
TablePrefixMetadataFactory.php
6 months ago
index.php
3 years ago
ArrayCache.php
100 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Doctrine; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoetVendor\Doctrine\Common\Cache\CacheProvider; |
| 9 | |
| 10 | /** |
| 11 | * Array cache |
| 12 | * Based on https://github.com/doctrine/cache/blob/1.11.x/lib/Doctrine/Common/Cache/ArrayCache.php |
| 13 | * The cache implementation was removed from the doctrine/cache v2.0 so we need to provide own implementation. |
| 14 | */ |
| 15 | class ArrayCache extends CacheProvider { |
| 16 | |
| 17 | /** @var array<string, array{0: mixed, 1: int|false}> */ |
| 18 | private $data = []; |
| 19 | |
| 20 | /** @var int */ |
| 21 | private $hitsCount = 0; |
| 22 | |
| 23 | /** @var int */ |
| 24 | private $missesCount = 0; |
| 25 | |
| 26 | /** @var int */ |
| 27 | private $upTime; |
| 28 | |
| 29 | /** |
| 30 | * {@inheritdoc} |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | $this->upTime = time(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * {@inheritdoc} |
| 38 | */ |
| 39 | protected function doFetch($id) { |
| 40 | if (!$this->doContains($id)) { |
| 41 | $this->missesCount += 1; |
| 42 | return false; |
| 43 | } |
| 44 | $this->hitsCount += 1; |
| 45 | return $this->data[$id][0]; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * {@inheritdoc} |
| 50 | */ |
| 51 | protected function doContains($id) { |
| 52 | if (!isset($this->data[$id])) { |
| 53 | return false; |
| 54 | } |
| 55 | $expiration = $this->data[$id][1]; |
| 56 | if ($expiration && $expiration < \time()) { |
| 57 | $this->doDelete($id); |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | protected function doSave($id, $data, $lifeTime = 0) { |
| 67 | $this->data[$id] = [$data, $lifeTime ? \time() + $lifeTime : false]; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | protected function doDelete($id) { |
| 75 | unset($this->data[$id]); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * {@inheritdoc} |
| 81 | */ |
| 82 | protected function doFlush() { |
| 83 | $this->data = []; |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | protected function doGetStats() { |
| 91 | return [ |
| 92 | CacheProvider::STATS_HITS => $this->hitsCount, |
| 93 | CacheProvider::STATS_MISSES => $this->missesCount, |
| 94 | CacheProvider::STATS_UPTIME => $this->upTime, |
| 95 | CacheProvider::STATS_MEMORY_USAGE => null, |
| 96 | CacheProvider::STATS_MEMORY_AVAILABLE => null, |
| 97 | ]; |
| 98 | } |
| 99 | } |
| 100 |