]> git.mxchange.org Git - friendica.git/commitdiff
Add ICacheDriver->getAllKeys method
authorHypolite Petovan <mrpetovan@gmail.com>
Wed, 26 Sep 2018 02:52:32 +0000 (22:52 -0400)
committerHypolite Petovan <mrpetovan@gmail.com>
Wed, 26 Sep 2018 02:52:32 +0000 (22:52 -0400)
src/Core/Cache.php
src/Core/Cache/ArrayCache.php
src/Core/Cache/DatabaseCacheDriver.php
src/Core/Cache/ICacheDriver.php
src/Core/Cache/MemcacheCacheDriver.php
src/Core/Cache/MemcachedCacheDriver.php
src/Core/Cache/RedisCacheDriver.php

index 81eacc80ff129d178af500d8a83b6d61bd0b2cb9..68537c5249c955597d1ce3026e0865d4792a389c 100644 (file)
@@ -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
         *
index d1302c1d6ec327d5f04a0a570b58c4da747954b5..47c9c166808b86b8b53b4bac601223ac2a3c00e3 100644 (file)
@@ -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)
         */
index 58769165d148ae6b419f873b8ecbc534b740c6b0..74dfe3991e1ca4beeb818db1bf23f885a5903cb7 100644 (file)
@@ -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)
         */
index 9ddcf5ad1fe8d530c44bb6ca628e813d1b147fb2..b77aa03c162de44abea89ac60415e735cc03d5e0 100644 (file)
@@ -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
         *
index 37327ebba92e9d6230b6a5be588682d117f802fa..9e5246f5b6a38b9e75d4ba0507f7eb28904038d2 100644 (file)
@@ -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)
         */
index 3bf82a18209ede2df054889e35ef869ca0cfc67c..c1d08f33212855d3ef9b8f16df081fa636fbd84d 100644 (file)
@@ -53,6 +53,14 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
                }
        }
 
+       /**
+        * (@inheritdoc)
+        */
+       public function getAllKeys()
+       {
+               return $this->memcached->getAllKeys();
+       }
+
        /**
         * (@inheritdoc)
         */
index cd5d8d9bfeef4f82924014eaf3d9b4836af48fb6..f9d00fde21fb4c3366ec8878f5e3f8dcdf4a69df 100644 (file)
@@ -38,6 +38,14 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
                }
        }
 
+       /**
+        * (@inheritdoc)
+        */
+       public function getAllKeys()
+       {
+               return null;
+       }
+
        /**
         * (@inheritdoc)
         */