|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of php-cache\array-adapter package. |
| 5 | + * |
| 6 | + * (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Cache\Adapter\PHPArray; |
| 13 | + |
| 14 | +use Cache\Adapter\Common\AbstractCachePool; |
| 15 | +use Cache\Adapter\Common\CacheItem; |
| 16 | +use Cache\Hierarchy\HierarchicalCachePoolTrait; |
| 17 | +use Cache\Hierarchy\HierarchicalPoolInterface; |
| 18 | +use Psr\Cache\CacheItemInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Array cache pool. You could set a limit of how many items you wantt to be stored to avoid memory leaks. |
| 22 | + * |
| 23 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 24 | + */ |
| 25 | +class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface |
| 26 | +{ |
| 27 | + use HierarchicalCachePoolTrait; |
| 28 | + |
| 29 | + /** |
| 30 | + * @type array |
| 31 | + */ |
| 32 | + private $cache; |
| 33 | + |
| 34 | + /** |
| 35 | + * @type array |
| 36 | + * A map to hold keys |
| 37 | + */ |
| 38 | + private $keyMap = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * @type int |
| 42 | + * The maximum number of keys in the map |
| 43 | + */ |
| 44 | + private $limit; |
| 45 | + |
| 46 | + /** |
| 47 | + * @type int |
| 48 | + * The next key that we should remove from the cache |
| 49 | + */ |
| 50 | + private $currentPosition = 0; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param int $limit the amount if items stored in the cache. Using a limit will reduce memory leaks. |
| 54 | + * @param array $cache |
| 55 | + */ |
| 56 | + public function __construct($limit = null, array &$cache = []) |
| 57 | + { |
| 58 | + $this->cache = &$cache; |
| 59 | + $this->limit = $limit; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritdoc} |
| 64 | + */ |
| 65 | + protected function getItemWithoutGenerateCacheKey($key) |
| 66 | + { |
| 67 | + if (isset($this->deferred[$key])) { |
| 68 | + $item = $this->deferred[$key]; |
| 69 | + |
| 70 | + return is_object($item) ? clone $item : $item; |
| 71 | + } |
| 72 | + |
| 73 | + return $this->fetchObjectFromCache($key); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritdoc} |
| 78 | + */ |
| 79 | + protected function fetchObjectFromCache($key) |
| 80 | + { |
| 81 | + $storageKey = $this->getHierarchyKey($key); |
| 82 | + if (isset($this->cache[$storageKey])) { |
| 83 | + return $this->cache[$storageKey]; |
| 84 | + } |
| 85 | + |
| 86 | + return new CacheItem($key, false); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * {@inheritdoc} |
| 91 | + */ |
| 92 | + protected function clearAllObjectsFromCache() |
| 93 | + { |
| 94 | + $this->cache = []; |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * {@inheritdoc} |
| 101 | + */ |
| 102 | + protected function clearOneObjectFromCache($key) |
| 103 | + { |
| 104 | + $this->commit(); |
| 105 | + $keyString = $this->getHierarchyKey($key, $path); |
| 106 | + if (isset($this->cache[$path])) { |
| 107 | + $this->cache[$path]++; |
| 108 | + } else { |
| 109 | + $this->cache[$path] = 0; |
| 110 | + } |
| 111 | + $this->clearHierarchyKeyCache(); |
| 112 | + |
| 113 | + unset($this->cache[$keyString]); |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * {@inheritdoc} |
| 120 | + */ |
| 121 | + protected function storeItemInCache($key, CacheItemInterface $item, $ttl) |
| 122 | + { |
| 123 | + $key = $this->getHierarchyKey($key); |
| 124 | + $this->cache[$key] = $item; |
| 125 | + |
| 126 | + if ($this->limit !== null) { |
| 127 | + // Remove the oldest value |
| 128 | + if (isset($this->keyMap[$this->currentPosition])) { |
| 129 | + unset($this->cache[$this->keyMap[$this->currentPosition]]); |
| 130 | + } |
| 131 | + |
| 132 | + // Add the new key to the current position |
| 133 | + $this->keyMap[$this->currentPosition] = $key; |
| 134 | + |
| 135 | + // Increase the current position |
| 136 | + $this->currentPosition = ($this->currentPosition + 1) % $this->limit; |
| 137 | + } |
| 138 | + |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * {@inheritdoc} |
| 144 | + */ |
| 145 | + protected function getValueFormStore($key) |
| 146 | + { |
| 147 | + if (isset($this->cache[$key])) { |
| 148 | + return $this->cache[$key]; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments