1 | <?php |
||
18 | class InMemoryCache |
||
19 | { |
||
20 | /** |
||
21 | * @var float Cache Time to Live, in seconds. This is only for how long we keep cache object around in-memory. |
||
22 | */ |
||
23 | private $ttl; |
||
24 | |||
25 | /** |
||
26 | * @var int The limit of objects in cache pool at a given time |
||
27 | */ |
||
28 | private $limit; |
||
29 | |||
30 | /** |
||
31 | * @var bool Switch for enabeling/disabling in-memory cache |
||
32 | */ |
||
33 | private $enabled; |
||
34 | |||
35 | /** |
||
36 | * Cache objects by primary key. |
||
37 | * |
||
38 | * @var object[] |
||
39 | */ |
||
40 | private $cache = []; |
||
41 | |||
42 | /** |
||
43 | * @var float[] Timestamp (float microtime) for individual cache by primary key. |
||
44 | */ |
||
45 | private $cacheTime = []; |
||
46 | |||
47 | /** |
||
48 | * Mapping of secondary index to primary key. |
||
49 | * |
||
50 | * @var string[] |
||
51 | */ |
||
52 | private $cacheIndex = []; |
||
53 | |||
54 | /** |
||
55 | * In Memory Cache constructor. |
||
56 | * |
||
57 | * @param int $ttl Seconds for the cache to live, by default 300 milliseconds |
||
58 | * @param int $limit Limit for values to keep in cache, by default 100 cache values (per pool instance). |
||
59 | * @param bool $enabled For use by configuration to be able to disable or enable depending on needs. |
||
60 | */ |
||
61 | public function __construct(int $ttl = 300, int $limit = 100, bool $enabled = true) |
||
67 | |||
68 | /** |
||
69 | * @param bool $enabled |
||
70 | * |
||
71 | * @return bool Prior value |
||
72 | */ |
||
73 | public function setEnabled(bool $enabled = true): bool |
||
80 | |||
81 | /** |
||
82 | * Returns a cache objects. |
||
83 | * |
||
84 | * @param string $key Primary or secondary index to look for cache on. |
||
85 | * |
||
86 | * @return object|null Object if found, null if not. |
||
87 | */ |
||
88 | public function get(string $key) |
||
101 | |||
102 | /** |
||
103 | * Set object in in-memory cache. |
||
104 | * |
||
105 | * Should only set Cache hits here! |
||
106 | * |
||
107 | * @param object[] $objects |
||
108 | * @param callable $objectIndexes Return array of indexes per object (first argument), must return at least 1 primary index |
||
109 | * @param string|null $listIndex Optional index for list of items |
||
110 | */ |
||
111 | public function setMulti(array $objects, callable $objectIndexes, string $listIndex = null): void |
||
145 | |||
146 | /** |
||
147 | * Removes multiple in-memory cache from the pool. |
||
148 | * |
||
149 | * @param string[] $keys An array of keys that should be removed from the pool. |
||
150 | */ |
||
151 | public function deleteMulti(array $keys): void |
||
165 | |||
166 | /** |
||
167 | * Deletes all cache in the in-memory pool. |
||
168 | */ |
||
169 | public function clear(): void |
||
174 | |||
175 | /** |
||
176 | * Call to reduce cache items when $limit has been reached. |
||
177 | * |
||
178 | * Deletes expired first, then oldest(or least used?). |
||
179 | */ |
||
180 | private function vacuum(): void |
||
192 | } |
||
193 |
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.