mirror of
https://github.com/friendica/friendica
synced 2025-04-25 20:30:11 +00:00
friendica-5847 Console Cache List command doesn't work
- Added $prefix to all CacheDriver - Moved hostname magic to CacheDriver - Added test for getAllKeys()
This commit is contained in:
parent
1551570b7f
commit
3f0f3b6ae6
15 changed files with 131 additions and 48 deletions
|
@ -17,8 +17,55 @@ abstract class AbstractCacheDriver extends BaseObject
|
|||
* @param string $key The original key
|
||||
* @return string The cache key used for the cache
|
||||
*/
|
||||
protected function getCacheKey($key) {
|
||||
protected function getCacheKey($key)
|
||||
{
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
return self::getApp()->get_hostname() . ":" . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $keys A list of cached keys
|
||||
* @return array A list of original keys
|
||||
*/
|
||||
protected function getOriginalKeys($keys)
|
||||
{
|
||||
if (empty($keys)) {
|
||||
return [];
|
||||
} else {
|
||||
// Keys are prefixed with the node hostname, let's remove it
|
||||
array_walk($keys, function (&$value) {
|
||||
$value = preg_replace('/^' . self::getApp()->get_hostname() . ':/', '', $value);
|
||||
});
|
||||
|
||||
sort($keys);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters a list for a given prefix
|
||||
*
|
||||
* @param array $list the list
|
||||
* @param string|null $prefix the prefix
|
||||
*
|
||||
* @return array the filtered list
|
||||
*/
|
||||
protected function filterPrefix($list, $prefix = null)
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
return array_keys($list);
|
||||
} else {
|
||||
$result = [];
|
||||
|
||||
foreach (array_keys($list) as $key) {
|
||||
if (strpos($key, $prefix) === 0) {
|
||||
array_push($result, $key);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,9 +22,9 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys()
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
return array_keys($this->cachedData);
|
||||
return $this->filterPrefix($this->cachedData, $prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,11 +16,23 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys()
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
$stmt = DBA::select('cache', ['k'], ['`expires` >= ?', DateTimeFormat::utcNow()]);
|
||||
if (empty($prefix)) {
|
||||
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND k LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
return DBA::toArray($stmt);
|
||||
$stmt = DBA::select('cache', ['k'], $where);
|
||||
|
||||
$list = [];
|
||||
while ($key = DBA::fetch($stmt)) {
|
||||
array_push($list, $key['k']);
|
||||
}
|
||||
DBA::close($stmt);
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,9 +14,11 @@ interface ICacheDriver
|
|||
/**
|
||||
* Lists all cache keys
|
||||
*
|
||||
* @param string prefix optional a prefix to search
|
||||
*
|
||||
* @return array|null Null if it isn't supported by the cache driver
|
||||
*/
|
||||
public function getAllKeys();
|
||||
public function getAllKeys($prefix = null);
|
||||
|
||||
/**
|
||||
* Fetches cached data according to the key
|
||||
|
|
|
@ -43,7 +43,7 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys()
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
$list = [];
|
||||
$allSlabs = $this->memcache->getExtendedStats('slabs');
|
||||
|
@ -59,7 +59,9 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
$list = $this->getOriginalKeys($list);
|
||||
|
||||
return $this->filterPrefix($list, $prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Friendica\Core\Cache;
|
|||
use Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Memcached;
|
||||
|
||||
/**
|
||||
|
@ -40,6 +41,9 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
|
||||
$this->memcached = new Memcached();
|
||||
|
||||
|
||||
$this->memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, false);
|
||||
|
||||
array_walk($memcached_hosts, function (&$value) {
|
||||
if (is_string($value)) {
|
||||
$value = array_map('trim', explode(',', $value));
|
||||
|
@ -56,9 +60,15 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys()
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
return $this->memcached->getAllKeys();
|
||||
// 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');
|
||||
|
||||
$list = $this->getOriginalKeys($this->memcached->getAllKeys());
|
||||
|
||||
return $this->filterPrefix($list, $prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -41,9 +41,17 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function getAllKeys()
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
return null;
|
||||
if (empty($prefix)) {
|
||||
$search = '*';
|
||||
} else {
|
||||
$search = $prefix . '*';
|
||||
}
|
||||
|
||||
$list = $this->redis->keys($this->getCacheKey($search));
|
||||
|
||||
return $this->getOriginalKeys($list);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue