1 | <?php |
||
29 | class InMemoryCache |
||
30 | { |
||
31 | /** |
||
32 | * @var float Cache Time to Live, in seconds. This is only for how long we keep cache object around in-memory. |
||
33 | */ |
||
34 | private $ttl; |
||
35 | |||
36 | /** |
||
37 | * @var int The limit of objects in cache pool at a given time |
||
38 | */ |
||
39 | private $limit; |
||
40 | |||
41 | /** |
||
42 | * @var bool Switch for enabeling/disabling in-memory cache |
||
43 | */ |
||
44 | private $enabled; |
||
45 | |||
46 | /** |
||
47 | * Cache objects by primary key. |
||
48 | * |
||
49 | * @var object[] |
||
50 | */ |
||
51 | private $cache = []; |
||
52 | |||
53 | /** |
||
54 | * @var float[] Expiry timestamp (float microtime) for individual cache (by primary key). |
||
55 | */ |
||
56 | private $cacheExpiryTime = []; |
||
57 | |||
58 | /** |
||
59 | * @var int[] Access counter for individual cache (by primary key), to order by by popularity on vacuum(). |
||
60 | */ |
||
61 | private $cacheAccessCount = []; |
||
62 | |||
63 | /** |
||
64 | * Mapping of secondary index to primary key. |
||
65 | * |
||
66 | * @var string[] |
||
67 | */ |
||
68 | private $cacheIndex = []; |
||
69 | |||
70 | /** |
||
71 | * In Memory Cache constructor. |
||
72 | * |
||
73 | * @param int $ttl Seconds for the cache to live, by default 300 milliseconds |
||
74 | * @param int $limit Limit for values to keep in cache, by default 100 cache values. |
||
75 | * @param bool $enabled For use by configuration to be able to disable or enable depending on needs. |
||
76 | */ |
||
77 | public function __construct(int $ttl = 300, int $limit = 100, bool $enabled = true) |
||
83 | |||
84 | /** |
||
85 | * @param bool $enabled |
||
86 | * |
||
87 | * @return bool Prior value |
||
88 | */ |
||
89 | public function setEnabled(bool $enabled = true): bool |
||
96 | |||
97 | /** |
||
98 | * Returns a cache objects. |
||
99 | * |
||
100 | * @param string $key Primary or secondary index to look for cache on. |
||
101 | * |
||
102 | * @return object|null Object if found, null if not. |
||
103 | */ |
||
104 | public function get(string $key) |
||
119 | |||
120 | /** |
||
121 | * Set object in in-memory cache. |
||
122 | * |
||
123 | * Should only set Cache hits here! |
||
124 | * |
||
125 | * @param object[] $objects |
||
126 | * @param callable $objectIndexes Return array of indexes per object (first argument), must return at least 1 primary index |
||
127 | * @param string|null $listIndex Optional index for list of items |
||
128 | */ |
||
129 | public function setMulti(array $objects, callable $objectIndexes, string $listIndex = null): void |
||
165 | |||
166 | /** |
||
167 | * Removes multiple in-memory cache from the pool. |
||
168 | * |
||
169 | * @param string[] $keys An array of keys that should be removed from the pool. |
||
170 | */ |
||
171 | public function deleteMulti(array $keys): void |
||
187 | |||
188 | /** |
||
189 | * Deletes all cache in the in-memory pool. |
||
190 | */ |
||
191 | public function clear(): void |
||
196 | |||
197 | /** |
||
198 | * Call to reduce cache items when $limit has been reached. |
||
199 | * |
||
200 | * Deletes items using LFU approach where the least frequently used items are evicted from bottom and up. |
||
201 | * Within groups of cache used equal amount of times, the oldest keys will be deleted first (FIFO). |
||
202 | */ |
||
203 | private function vacuum(): void |
||
232 | } |
||
233 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.