From 71c08a044f8317c92b3b282a70f55406ae9deb0a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 22:52:32 -0400 Subject: [PATCH] Add ICacheDriver->getAllKeys method --- src/Core/Cache.php | 23 +++++++++++++++++++++++ src/Core/Cache/ArrayCache.php | 8 ++++++++ src/Core/Cache/DatabaseCacheDriver.php | 10 ++++++++++ src/Core/Cache/ICacheDriver.php | 7 +++++++ src/Core/Cache/MemcacheCacheDriver.php | 22 ++++++++++++++++++++++ src/Core/Cache/MemcachedCacheDriver.php | 8 ++++++++ src/Core/Cache/RedisCacheDriver.php | 8 ++++++++ 7 files changed, 86 insertions(+) diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 81eacc80ff..68537c5249 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -46,6 +46,29 @@ class Cache extends \Friendica\BaseObject return self::$driver; } + /** + * @brief Returns all the cache keys sorted alphabetically + * + * @return array|null Null if the driver doesn't support this feature + */ + public static function getAllKeys() + { + $time = microtime(true); + + $return = self::getDriver()->getAllKeys(); + + // Keys are prefixed with the node hostname, let's remove it + array_walk($return, function (&$value) { + $value = preg_replace('/^' . self::getApp()->get_hostname() . ':/', '', $value); + }); + + sort($return); + + self::getApp()->save_timestamp($time, 'cache'); + + return $return; + } + /** * @brief Fetch cached data according to the key * diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index d1302c1d6e..47c9c16680 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -19,6 +19,14 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver /** @var array Array with the cached data */ protected $cachedData = array(); + /** + * (@inheritdoc) + */ + public function getAllKeys() + { + return array_keys($this->cachedData); + } + /** * (@inheritdoc) */ diff --git a/src/Core/Cache/DatabaseCacheDriver.php b/src/Core/Cache/DatabaseCacheDriver.php index 58769165d1..74dfe3991e 100644 --- a/src/Core/Cache/DatabaseCacheDriver.php +++ b/src/Core/Cache/DatabaseCacheDriver.php @@ -13,6 +13,16 @@ use Friendica\Util\DateTimeFormat; */ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver { + /** + * (@inheritdoc) + */ + public function getAllKeys() + { + $stmt = DBA::select('cache', ['k'], ['`expires` >= ?', DateTimeFormat::utcNow()]); + + return DBA::toArray($stmt); + } + /** * (@inheritdoc) */ diff --git a/src/Core/Cache/ICacheDriver.php b/src/Core/Cache/ICacheDriver.php index 9ddcf5ad1f..b77aa03c16 100644 --- a/src/Core/Cache/ICacheDriver.php +++ b/src/Core/Cache/ICacheDriver.php @@ -11,6 +11,13 @@ use Friendica\Core\Cache; */ interface ICacheDriver { + /** + * Lists all cache keys + * + * @return array|null Null if it isn't supported by the cache driver + */ + public function getAllKeys(); + /** * Fetches cached data according to the key * diff --git a/src/Core/Cache/MemcacheCacheDriver.php b/src/Core/Cache/MemcacheCacheDriver.php index 37327ebba9..9e5246f5b6 100644 --- a/src/Core/Cache/MemcacheCacheDriver.php +++ b/src/Core/Cache/MemcacheCacheDriver.php @@ -40,6 +40,28 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri } } + /** + * (@inheritdoc) + */ + public function getAllKeys() + { + $list = []; + $allSlabs = $this->memcache->getExtendedStats('slabs'); + foreach($allSlabs as $slabs) { + foreach(array_keys($slabs) as $slabId) { + $cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId); + foreach($cachedump as $keys => $arrVal) { + if (!is_array($arrVal)) { + continue; + } + $list = array_merge($list, array_keys($arrVal)); + } + } + } + + return $list; + } + /** * (@inheritdoc) */ diff --git a/src/Core/Cache/MemcachedCacheDriver.php b/src/Core/Cache/MemcachedCacheDriver.php index 3bf82a1820..c1d08f3321 100644 --- a/src/Core/Cache/MemcachedCacheDriver.php +++ b/src/Core/Cache/MemcachedCacheDriver.php @@ -53,6 +53,14 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr } } + /** + * (@inheritdoc) + */ + public function getAllKeys() + { + return $this->memcached->getAllKeys(); + } + /** * (@inheritdoc) */ diff --git a/src/Core/Cache/RedisCacheDriver.php b/src/Core/Cache/RedisCacheDriver.php index cd5d8d9bfe..f9d00fde21 100644 --- a/src/Core/Cache/RedisCacheDriver.php +++ b/src/Core/Cache/RedisCacheDriver.php @@ -38,6 +38,14 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver } } + /** + * (@inheritdoc) + */ + public function getAllKeys() + { + return null; + } + /** * (@inheritdoc) */ -- 2.39.5