]> git.mxchange.org Git - friendica.git/commitdiff
Add getStats() method for MemoryCaches
authorPhilipp <admin@philipp.info>
Mon, 21 Apr 2025 17:37:37 +0000 (19:37 +0200)
committerPhilipp <admin@philipp.info>
Sun, 27 Apr 2025 19:26:28 +0000 (21:26 +0200)
12 files changed:
src/Core/Cache/Capability/ICanCacheInMemory.php
src/Core/Cache/Type/APCuCache.php
src/Core/Cache/Type/ArrayCache.php
src/Core/Cache/Type/MemcacheCache.php
src/Core/Cache/Type/MemcachedCache.php
src/Core/Cache/Type/ProfilerCacheDecorator.php
src/Core/Cache/Type/RedisCache.php
src/Core/Lock/Type/CacheLock.php
tests/src/Core/Cache/APCuCacheTest.php
tests/src/Core/Cache/MemcacheCacheTest.php
tests/src/Core/Cache/MemcachedCacheTest.php
tests/src/Core/Cache/RedisCacheTest.php

index 82492d368a2db4c1d4731d2a19c93fdfee37d8f2..550273f5f066c0ee205a7cefcb1755491cf57ca9 100644 (file)
@@ -53,4 +53,11 @@ interface ICanCacheInMemory extends ICanCache
         * @throws CachePersistenceException In case the underlying cache driver has errors during persistence
         */
        public function compareDelete(string $key, $value): bool;
+
+       /**
+        * Returns some basic statistics of the used Cache instance
+        *
+        * @return array Returns an associative array of statistics
+        */
+       public function getStats(): array;
 }
index f1dde2462c021dd86b4e4dd95441d48e32b10c92..3c72e76c983304a1f402d424d1478a7b398e3a7b 100644 (file)
@@ -147,4 +147,19 @@ class APCuCache extends AbstractCache implements ICanCacheInMemory
 
                return true;
        }
+
+       /** {@inheritDoc} */
+       public function getStats(): array
+       {
+               $apcu = apcu_cache_info();
+               $sma  = apcu_sma_info();
+
+               return [
+                       'entries'     => $apcu['num_entries'] ?? null,
+                       'used_memory'    => $apcu['mem_size'] ?? null,
+                       'hits'        => $apcu['num_hits'] ?? null,
+                       'misses'      => $apcu['num_misses'] ?? null,
+                       'avail_mem'   => $sma['avail_mem'] ?? null,
+               ];
+       }
 }
index 7fd44deb0a92b623044c611ae8eb9cb1ed389d66..c6a20d627b62745038ba52df1b84fcf1ad95ae14 100644 (file)
@@ -96,4 +96,10 @@ class ArrayCache extends AbstractCache implements ICanCacheInMemory
                        return false;
                }
        }
+
+       /** {@inheritDoc} */
+       public function getStats(): array
+       {
+               return [];
+       }
 }
index 14bd5e310b4bbe89081243980e61b0f66ff3f345..b2142ffd3670513b0aceba79cf7a6aeee54418c2 100644 (file)
@@ -156,4 +156,21 @@ class MemcacheCache extends AbstractCache implements ICanCacheInMemory
                $cacheKey = $this->getCacheKey($key);
                return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
        }
+
+       /** {@inheritDoc} */
+       public function getStats(): array
+       {
+               $stats = $this->memcache->getStats();
+
+               return [
+                       'version'           => $stats['version'] ?? null,
+                       'entries'           => $stats['curr_items'] ?? null,
+                       'used_memory'       => $stats['bytes'] ?? null,
+                       'uptime'            => $stats['uptime'] ?? null,
+                       'connected_clients' => $stats['curr_connections'] ?? null,
+                       'hits'              => $stats['get_hits'] ?? null,
+                       'misses'            => $stats['get_misses'] ?? null,
+                       'evictions'         => $stats['evictions'] ?? null,
+               ];
+       }
 }
index 2e970e6078aaa535f76a5b1226ca747f37ef30c2..53f959fd2b035cf98b96f2c3fe6c1431d3d43345 100644 (file)
@@ -172,4 +172,27 @@ class MemcachedCache extends AbstractCache implements ICanCacheInMemory
                $cacheKey = $this->getCacheKey($key);
                return $this->memcached->add($cacheKey, $value, $ttl);
        }
+
+       /** {@inheritDoc} */
+       public function getStats(): array
+       {
+               $stats = $this->memcached->getStats();
+
+               // get statistics of the first instance
+               foreach ($stats as $value) {
+                       $stats = $value;
+                       break;
+               }
+
+               return [
+                       'version'           => $stats['version'] ?? null,
+                       'entries'     =>       $stats['curr_items'] ?? null,
+                       'used_memory'       => $stats['bytes'] ?? null,
+                       'uptime'            => $stats['uptime'] ?? null,
+                       'connected_clients' => $stats['curr_connections'] ?? null,
+                       'hits'              => $stats['get_hits'] ?? null,
+                       'misses'            => $stats['get_misses'] ?? null,
+                       'evictions'         => $stats['evictions'] ?? null,
+               ];
+       }
 }
index 8071b79c5e3a03855c68c7f3167b4aa6409b00d3..113aa766883d6f89af358547c9d8065250fbac08 100644 (file)
@@ -166,4 +166,14 @@ class ProfilerCacheDecorator implements ICanCache, ICanCacheInMemory
        {
                return $this->cache->getName() . ' (with profiler)';
        }
+
+       /** {@inheritDoc} */
+       public function getStats(): array
+       {
+               if ($this->cache instanceof ICanCacheInMemory) {
+                       return $this->cache->getStats();
+               } else {
+                       return [];
+               }
+       }
 }
index cf78d362bbd3e8a4a1d7735a33fa7000641e4a58..d00562464136304816753dc9675e8a33da918ca4 100644 (file)
@@ -207,4 +207,21 @@ class RedisCache extends AbstractCache implements ICanCacheInMemory
                $this->redis->unwatch();
                return false;
        }
+
+       /** {@inheritDoc} */
+       public function getStats(): array
+       {
+               $info = $this->redis->info();
+
+               return [
+                       'version'           => $info['redis_version'] ?? null,
+                       'entries'           => $this->redis->dbSize() ?? null,
+                       'used_memory'       => $info['used_memory'] ?? null,
+                       'connected_clients' => $info['connected_clients'] ?? null,
+                       'uptime'            => $info['uptime_in_seconds'] ?? null,
+                       'hits'              => $info['keyspace_hits'] ?? null,
+                       'misses'            => $info['keyspace_misses'] ?? null,
+                       'evictions'         => $info['evicted_keys'] ?? null,
+               ];
+       }
 }
index c3794d06a7e9004f7da3182b2158cc0689c4bfa4..9fc9fad8f8462284b3742a006e40e42de5888b99 100644 (file)
@@ -156,6 +156,16 @@ class CacheLock extends AbstractLock
                return $success;
        }
 
+       /**
+        * Returns stats about the cache provider
+        *
+        * @return array
+        */
+       public function getCacheStats(): array
+       {
+               return $this->cache->getStats();
+       }
+
        /**
         * @param string $key The original key
         *
index 117c211b04140001373ef23712e4f15082c3191a..47e660b26ad2ef7754e6b6053b482d67b307d202 100644 (file)
@@ -35,4 +35,18 @@ class APCuCacheTest extends MemoryCacheTestCase
                $this->cache->clear(false);
                parent::tearDown();
        }
+
+       /**
+        * @small
+        */
+       public function testStats()
+       {
+               $stats = $this->instance->getStats();
+
+               self::assertNotNull($stats['entries']);
+               self::assertNotNull($stats['used_memory']);
+               self::assertNotNull($stats['hits']);
+               self::assertNotNull($stats['misses']);
+               self::assertNotNull($stats['avail_mem']);
+       }
 }
index abd073f4838abc0b6a120c99e3ab6554742c90aa..c622f22216c6060b06065afd0e6a5f5fe15876ba 100644 (file)
@@ -59,4 +59,21 @@ class MemcacheCacheTest extends MemoryCacheTestCase
        {
                static::markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
        }
+
+       /**
+        * @small
+        */
+       public function testStats()
+       {
+               $stats = $this->instance->getStats();
+
+               self::assertNotNull($stats['version']);
+               self::assertIsNumeric($stats['hits']);
+               self::assertIsNumeric($stats['misses']);
+               self::assertIsNumeric($stats['evictions']);
+               self::assertIsNumeric($stats['entries']);
+               self::assertIsNumeric($stats['used_memory']);
+               self::assertGreaterThan(0, $stats['connected_clients']);
+               self::assertGreaterThan(0, $stats['uptime']);
+       }
 }
index f3b6107b5be6f8ed56cc6195ad8698bd19c4234d..a1c3653f1b360109b32c3ecd28ca4dd99a03b004 100644 (file)
@@ -58,4 +58,21 @@ class MemcachedCacheTest extends MemoryCacheTestCase
        {
                static::markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
        }
+
+       /**
+        * @small
+        */
+       public function testStats()
+       {
+               $stats = $this->instance->getStats();
+
+               self::assertNotNull($stats['version']);
+               self::assertIsNumeric($stats['hits']);
+               self::assertIsNumeric($stats['misses']);
+               self::assertIsNumeric($stats['evictions']);
+               self::assertIsNumeric($stats['entries']);
+               self::assertIsNumeric($stats['used_memory']);
+               self::assertGreaterThan(0, $stats['connected_clients']);
+               self::assertGreaterThan(0, $stats['uptime']);
+       }
 }
index 6169171f40eaeace3bdda24aefb80a86c85753fc..d16bf5a64cea7865003f70b7fcffe5c4412f6725 100644 (file)
@@ -57,4 +57,21 @@ class RedisCacheTest extends MemoryCacheTestCase
                $this->cache->clear(false);
                parent::tearDown();
        }
+
+       /**
+        * @small
+        */
+       public function testStats()
+       {
+               $stats = $this->instance->getStats();
+
+               self::assertNotNull($stats['version']);
+               self::assertIsNumeric($stats['hits']);
+               self::assertIsNumeric($stats['misses']);
+               self::assertIsNumeric($stats['evictions']);
+               self::assertIsNumeric($stats['entries']);
+               self::assertIsNumeric($stats['used_memory']);
+               self::assertGreaterThan(0, $stats['connected_clients']);
+               self::assertGreaterThan(0, $stats['uptime']);
+       }
 }