friendica-5847 Console Cache List command doesn't work

- filterPrefix => filterArrayKeysByPrefix()
- Logging failed Executions of getAllKeys and get from Memcached
This commit is contained in:
Philipp Holzer 2018-10-07 10:35:37 +02:00
parent 3f0f3b6ae6
commit 1ec5c6b284
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
5 changed files with 26 additions and 28 deletions

View 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 array $array The array, which should get filtered
* @param string|null $prefix the prefix * @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)) { if (empty($prefix)) {
return array_keys($list); return array_keys($array);
} else { } else {
$result = []; $result = [];
foreach (array_keys($list) as $key) { foreach (array_keys($array) as $key) {
if (strpos($key, $prefix) === 0) { if (strpos($key, $prefix) === 0) {
array_push($result, $key); array_push($result, $key);
} }

View File

@ -24,7 +24,7 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
*/ */
public function getAllKeys($prefix = null) public function getAllKeys($prefix = null)
{ {
return $this->filterPrefix($this->cachedData, $prefix); return $this->filterArrayKeysByPrefix($this->cachedData, $prefix);
} }
/** /**

View File

@ -45,23 +45,23 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
*/ */
public function getAllKeys($prefix = null) public function getAllKeys($prefix = null)
{ {
$list = []; $keys = [];
$allSlabs = $this->memcache->getExtendedStats('slabs'); $allSlabs = $this->memcache->getExtendedStats('slabs');
foreach ($allSlabs as $slabs) { foreach ($allSlabs as $slabs) {
foreach (array_keys($slabs) as $slabId) { foreach (array_keys($slabs) as $slabId) {
$cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId); $cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId);
foreach ($cachedump as $keys => $arrVal) { foreach ($cachedump as $key => $arrVal) {
if (!is_array($arrVal)) { if (!is_array($arrVal)) {
continue; 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);
} }
/** /**

View File

@ -62,13 +62,14 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
*/ */
public function getAllKeys($prefix = null) public function getAllKeys($prefix = null)
{ {
// Doesn't work because of https://github.com/php-memcached-dev/php-memcached/issues/367 $keys = $this->getOriginalKeys($this->memcached->getAllKeys());
// returns everytime an empty array
throw new InternalServerErrorException('getAllKeys for Memcached not supported yet');
$list = $this->getOriginalKeys($this->memcached->getAllKeys()); if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
return $this->filterArrayKeysByPrefix($keys, $prefix);
return $this->filterPrefix($list, $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) public function get($key)
{ {
$return = null;
$cachekey = $this->getCacheKey($key); $cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications // We fetch with the hostname as key to avoid problems with other applications
$value = $this->memcached->get($cachekey); $value = $this->memcached->get($cachekey);
if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) { 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 $value
); );
} }
} }
/** /**

View File

@ -115,10 +115,7 @@ HELP;
$count = 0; $count = 0;
foreach ($keys as $key) { foreach ($keys as $key) {
if (empty($prefix) || strpos($key, $prefix) === 0) { $this->out($key);
$this->out($key);
$count++;
}
} }
$this->out($count . ' keys found'); $this->out($count . ' keys found');