]> git.mxchange.org Git - friendica.git/commitdiff
friendica-5847 Console Cache List command doesn't work
authorPhilipp Holzer <admin@philipp.info>
Sun, 7 Oct 2018 08:35:37 +0000 (10:35 +0200)
committerPhilipp Holzer <admin@philipp.info>
Sun, 7 Oct 2018 08:35:37 +0000 (10:35 +0200)
- filterPrefix => filterArrayKeysByPrefix()
- Logging failed Executions of getAllKeys and get from Memcached

src/Core/Cache/AbstractCacheDriver.php
src/Core/Cache/ArrayCache.php
src/Core/Cache/MemcacheCacheDriver.php
src/Core/Cache/MemcachedCacheDriver.php
src/Core/Console/Cache.php

index e694b9c31405ba8b7ee93837c1664c0de0092374..1cdecf6ceb0ce6ff9473bdc8f0a8f4647e600298 100644 (file)
@@ -44,21 +44,22 @@ abstract class AbstractCacheDriver extends BaseObject
        }
 
        /**
-        * Filters a list for a given prefix
+        * Filters the keys of an array with a given prefix
+        * Returns the filtered keys as an new array
         *
-        * @param array $list the list
-        * @param string|null $prefix the prefix
+        * @param array $array The array, which should get filtered
+        * @param string|null $prefix The prefix (if null, all keys will get returned)
         *
-        * @return array the filtered list
+        * @return array The filtered array with just the keys
         */
-       protected function filterPrefix($list, $prefix = null)
+       protected function filterArrayKeysByPrefix($array, $prefix = null)
        {
                if (empty($prefix)) {
-                       return array_keys($list);
+                       return array_keys($array);
                } else {
                        $result = [];
 
-                       foreach (array_keys($list) as $key) {
+                       foreach (array_keys($array) as $key) {
                                if (strpos($key, $prefix) === 0) {
                                        array_push($result, $key);
                                }
index 0d314fadb7d6580bf7f0b487b01c269f43ef900f..a99b05788f64e75e46d5d8e4df8c230bdcf6c414 100644 (file)
@@ -24,7 +24,7 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
         */
        public function getAllKeys($prefix = null)
        {
-               return $this->filterPrefix($this->cachedData, $prefix);
+               return $this->filterArrayKeysByPrefix($this->cachedData, $prefix);
        }
 
        /**
index f31e21d14e1a9a239f8aabb30a38ee2cb0d042de..fd928c6fcc15ed2a5245f2bca27c664a6f696531 100644 (file)
@@ -45,23 +45,23 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
         */
        public function getAllKeys($prefix = null)
        {
-               $list = [];
+               $keys = [];
                $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) {
+                               foreach ($cachedump as $key => $arrVal) {
                                        if (!is_array($arrVal)) {
                                                continue;
                                        }
-                                       $list = array_merge($list, array_keys($arrVal));
+                                       $keys = array_merge($keys, array_keys($arrVal));
                                }
                        }
                }
 
-               $list = $this->getOriginalKeys($list);
+               $keys = $this->getOriginalKeys($keys);
 
-               return $this->filterPrefix($list, $prefix);
+               return $this->filterArrayKeysByPrefix($keys, $prefix);
        }
 
        /**
index 82df98f13bb5c6c584fbb348caa01516aab209ee..a6e1bad950e343c3a0aef88ec37db65162f3ff25 100644 (file)
@@ -62,13 +62,14 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
         */
        public function getAllKeys($prefix = null)
        {
-               // Doesn't work because of https://github.com/php-memcached-dev/php-memcached/issues/367
-               // returns everytime an empty array
-               throw new InternalServerErrorException('getAllKeys for Memcached not supported yet');
+               $keys = $this->getOriginalKeys($this->memcached->getAllKeys());
 
-               $list = $this->getOriginalKeys($this->memcached->getAllKeys());
-
-               return $this->filterPrefix($list, $prefix);
+               if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
+                       return $this->filterArrayKeysByPrefix($keys, $prefix);
+               } else {
+                       logger('Memcached \'getAllKeys\' failed with ' . $this->memcached->getResultMessage(), LOGGER_ALL);
+                       return [];
+               }
        }
 
        /**
@@ -76,17 +77,17 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
         */
        public function get($key)
        {
-               $return = null;
                $cachekey = $this->getCacheKey($key);
 
                // We fetch with the hostname as key to avoid problems with other applications
                $value = $this->memcached->get($cachekey);
 
                if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
-                       $return = $value;
+                       return $value;
+               } else {
+                       logger('Memcached \'get\' failed with ' . $this->memcached->getResultMessage(), LOGGER_ALL);
+                       return [];
                }
-
-               return $return;
        }
 
        /**
@@ -109,7 +110,6 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
                                $value
                        );
                }
-
        }
 
        /**
index 0dfef43f35b4bbe64fe167de9f91fb1b1200538a..459a27e20bed216ce0392ec60d4e03f122e3a12a 100644 (file)
@@ -115,10 +115,7 @@ HELP;
 
                $count = 0;
                foreach ($keys as $key) {
-                       if (empty($prefix) || strpos($key, $prefix) === 0) {
-                               $this->out($key);
-                               $count++;
-                       }
+                       $this->out($key);
                }
 
                $this->out($count . ' keys found');