@@ -34,6 +34,20 @@ abstract class AbstractPersistentObjectCache implements PersistentObjectCacheInt
3434 */
3535 protected const MODE_REPLACE = 2 ;
3636
37+ /**
38+ * The amount of requests made to the persistent cache.
39+ *
40+ * @var int
41+ */
42+ protected $ requests ;
43+
44+ /**
45+ * The total time spent making requests to the persistent cache.
46+ *
47+ * @var int
48+ */
49+ protected $ requestTime ;
50+
3751 /**
3852 * The current blog ID when multisite is active.
3953 *
@@ -53,7 +67,14 @@ abstract class AbstractPersistentObjectCache implements PersistentObjectCacheInt
5367 *
5468 * @var array
5569 */
56- private $ globalGroups = [];
70+ private $ globalGroups ;
71+
72+ /**
73+ * The amount of times the cache data was already stored in the cache.
74+ *
75+ * @var int
76+ */
77+ private $ hits ;
5778
5879 /**
5980 * Flag whether this is a multisite installation or not.
@@ -62,12 +83,19 @@ abstract class AbstractPersistentObjectCache implements PersistentObjectCacheInt
6283 */
6384 private $ isMultisite ;
6485
86+ /**
87+ * Amount of times the cache didn't have the request in cache.
88+ *
89+ * @var int
90+ */
91+ private $ misses ;
92+
6593 /**
6694 * List of non-persistent groups.
6795 *
6896 * @var array
6997 */
70- private $ nonPersistentGroups = [] ;
98+ private $ nonPersistentGroups ;
7199
72100 /**
73101 * Prefix used for all cache keys.
@@ -89,9 +117,15 @@ abstract class AbstractPersistentObjectCache implements PersistentObjectCacheInt
89117 public function __construct (bool $ isMultisite , string $ prefix = '' )
90118 {
91119 $ this ->cache = [];
120+ $ this ->globalGroups = [];
121+ $ this ->hits = 0 ;
92122 $ this ->isMultisite = $ isMultisite ;
123+ $ this ->misses = 0 ;
124+ $ this ->nonPersistentGroups = [];
93125 $ this ->prefix = trim ($ prefix );
94126 $ this ->requestedKeys = [];
127+ $ this ->requests = 0 ;
128+ $ this ->requestTime = 0 ;
95129
96130 if (!empty ($ this ->prefix )) {
97131 $ this ->prefix = $ this ->sanitizeCacheKeyPart ($ this ->prefix );
@@ -207,10 +241,14 @@ public function get(string $group, string $key, bool $force = false, &$found = n
207241 if ($ this ->isNonPersistentGroup ($ group ) && !$ this ->hasInMemory ($ cacheKey )) {
208242 $ found = false ;
209243
244+ ++$ this ->misses ;
245+
210246 return false ;
211247 } elseif ((!$ force || $ this ->isNonPersistentGroup ($ group )) && $ this ->hasInMemory ($ cacheKey )) {
212248 $ found = true ;
213249
250+ ++$ this ->hits ;
251+
214252 return $ this ->getFromMemory ($ cacheKey );
215253 }
216254
@@ -222,6 +260,8 @@ public function get(string $group, string $key, bool $force = false, &$found = n
222260
223261 $ found = false !== $ value ;
224262
263+ $ found ? $ this ->hits ++ : $ this ->misses ++;
264+
225265 if (false !== $ value ) {
226266 $ this ->requestedKeys [$ cacheKey ] = true ;
227267 $ this ->storeInMemory ($ cacheKey , $ value );
@@ -230,6 +270,21 @@ public function get(string $group, string $key, bool $force = false, &$found = n
230270 return $ value ;
231271 }
232272
273+ /**
274+ * {@inheritdoc}
275+ */
276+ public function getInfo (): array
277+ {
278+ return [
279+ 'hits ' => $ this ->hits ,
280+ 'misses ' => $ this ->misses ,
281+ 'ratio ' => round (($ this ->hits / ($ this ->hits + $ this ->misses )) * 100 , 1 ),
282+ 'requests ' => $ this ->requests ,
283+ 'request_time ' => $ this ->requestTime ,
284+ 'type ' => str_replace ('ObjectCache ' , '' , (new \ReflectionClass ($ this ))->getShortName ()),
285+ ];
286+ }
287+
233288 /**
234289 * {@inheritdoc}
235290 */
@@ -251,7 +306,11 @@ public function getMultiple(string $group, array $keys, bool $force = false): ar
251306 $ value = false ;
252307
253308 if ((!$ force || $ this ->isNonPersistentGroup ($ group )) && $ this ->hasInMemory ($ cacheKey )) {
309+ ++$ this ->hits ;
310+
254311 $ value = $ this ->getFromMemory ($ cacheKey );
312+ } elseif ($ this ->isNonPersistentGroup ($ group ) && !$ this ->hasInMemory ($ cacheKey )) {
313+ ++$ this ->misses ;
255314 }
256315
257316 return [$ key => $ value ];
@@ -269,10 +328,6 @@ public function getMultiple(string $group, array $keys, bool $force = false): ar
269328 return $ cacheKeys [$ key ] ?? '' ;
270329 })->filter ()->all ()));
271330
272- $ valuesFromPersistentCache ->each (function ($ value , string $ key ) {
273- $ this ->storeInMemory ($ key , $ value );
274- });
275-
276331 $ keysWithMissingValues ->each (function (string $ key ) use ($ cacheKeys , $ values , $ valuesFromPersistentCache ) {
277332 $ cacheKey = $ cacheKeys [$ key ] ?? '' ;
278333
@@ -281,6 +336,16 @@ public function getMultiple(string $group, array $keys, bool $force = false): ar
281336 }
282337
283338 $ values [$ key ] = $ valuesFromPersistentCache [$ cacheKey ] ?? false ;
339+
340+ if (false === $ values [$ key ]) {
341+ ++$ this ->misses ;
342+
343+ return ;
344+ }
345+
346+ ++$ this ->hits ;
347+
348+ $ this ->storeInMemory ($ cacheKey , $ values [$ key ]);
284349 });
285350
286351 $ order = $ keys ->flip ()->all ();
0 commit comments