From d1148f61ea919e822be2a16b3eba525c8a639db6 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 22:46:45 -0400 Subject: [PATCH 1/7] Add missing "outdated" parameter to ICacheDriver->clear in Core\Cache --- src/Core/Cache.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Core/Cache.php b/src/Core/Cache.php index cc77d9bfd0..81eacc80ff 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -107,12 +107,12 @@ class Cache extends \Friendica\BaseObject /** * @brief Remove outdated data from the cache * - * @param integer $max_level The maximum cache level that is to be cleared + * @param boolean $outdated just remove outdated values * * @return void */ - public static function clear() + public static function clear($outdated = true) { - return self::getDriver()->clear(); + return self::getDriver()->clear($outdated); } } From 640f76b05af92b0387b6089641ee3158c8b195a3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 22:51:41 -0400 Subject: [PATCH 2/7] Improve doc blocks in cache drivers --- src/Core/Cache/DatabaseCacheDriver.php | 12 ++++++++++++ src/Core/Cache/MemcacheCacheDriver.php | 5 +++++ src/Core/Cache/MemcachedCacheDriver.php | 19 +++++++++++++------ src/Core/Cache/RedisCacheDriver.php | 18 ++++++++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Core/Cache/DatabaseCacheDriver.php b/src/Core/Cache/DatabaseCacheDriver.php index ca4842a468..58769165d1 100644 --- a/src/Core/Cache/DatabaseCacheDriver.php +++ b/src/Core/Cache/DatabaseCacheDriver.php @@ -13,6 +13,9 @@ use Friendica\Util\DateTimeFormat; */ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver { + /** + * (@inheritdoc) + */ public function get($key) { $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]); @@ -32,6 +35,9 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver return null; } + /** + * (@inheritdoc) + */ public function set($key, $value, $ttl = Cache::FIVE_MINUTES) { $fields = [ @@ -43,11 +49,17 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver return DBA::update('cache', $fields, ['k' => $key], true); } + /** + * (@inheritdoc) + */ public function delete($key) { return DBA::delete('cache', ['k' => $key]); } + /** + * (@inheritdoc) + */ public function clear($outdated = true) { if ($outdated) { diff --git a/src/Core/Cache/MemcacheCacheDriver.php b/src/Core/Cache/MemcacheCacheDriver.php index 4ca5aa38db..37327ebba9 100644 --- a/src/Core/Cache/MemcacheCacheDriver.php +++ b/src/Core/Cache/MemcacheCacheDriver.php @@ -22,6 +22,11 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri */ private $memcache; + /** + * @param string $memcache_host + * @param int $memcache_port + * @throws Exception + */ public function __construct($memcache_host, $memcache_port) { if (!class_exists('Memcache', false)) { diff --git a/src/Core/Cache/MemcachedCacheDriver.php b/src/Core/Cache/MemcachedCacheDriver.php index 9e9c00f0b8..3bf82a1820 100644 --- a/src/Core/Cache/MemcachedCacheDriver.php +++ b/src/Core/Cache/MemcachedCacheDriver.php @@ -53,6 +53,9 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr } } + /** + * (@inheritdoc) + */ public function get($key) { $return = null; @@ -68,6 +71,9 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr return $return; } + /** + * (@inheritdoc) + */ public function set($key, $value, $ttl = Cache::FIVE_MINUTES) { $cachekey = $this->getCacheKey($key); @@ -88,12 +94,18 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr } + /** + * (@inheritdoc) + */ public function delete($key) { $cachekey = $this->getCacheKey($key); return $this->memcached->delete($cachekey); } + /** + * (@inheritdoc) + */ public function clear($outdated = true) { if ($outdated) { @@ -104,12 +116,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr } /** - * @brief Sets a value if it's not already stored - * - * @param string $key The cache key - * @param mixed $value The old value we know from the cache - * @param int $ttl The cache lifespan, must be one of the Cache constants - * @return bool + * (@inheritdoc) */ public function add($key, $value, $ttl = Cache::FIVE_MINUTES) { diff --git a/src/Core/Cache/RedisCacheDriver.php b/src/Core/Cache/RedisCacheDriver.php index 2c2a3e5d7c..cd5d8d9bfe 100644 --- a/src/Core/Cache/RedisCacheDriver.php +++ b/src/Core/Cache/RedisCacheDriver.php @@ -20,6 +20,11 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver */ private $redis; + /** + * @param string $redis_host + * @param int $redis_port + * @throws Exception + */ public function __construct($redis_host, $redis_port) { if (!class_exists('Redis', false)) { @@ -33,6 +38,9 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver } } + /** + * (@inheritdoc) + */ public function get($key) { $return = null; @@ -55,6 +63,9 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver return $return; } + /** + * (@inheritdoc) + */ public function set($key, $value, $ttl = Cache::FIVE_MINUTES) { $cachekey = $this->getCacheKey($key); @@ -75,12 +86,18 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver } } + /** + * (@inheritdoc) + */ public function delete($key) { $cachekey = $this->getCacheKey($key); return ($this->redis->delete($cachekey) > 0); } + /** + * (@inheritdoc) + */ public function clear($outdated = true) { if ($outdated) { @@ -127,6 +144,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver $this->redis->unwatch(); return false; } + /** * (@inheritdoc) */ From 71c08a044f8317c92b3b282a70f55406ae9deb0a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 22:52:32 -0400 Subject: [PATCH 3/7] 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) */ From d420a4dec2be1cf9add1488f3e86dccbd8e7b635 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 22:58:04 -0400 Subject: [PATCH 4/7] Add informative public static members to Core\Cache --- src/Core/Cache.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 68537c5249..ea7807031f 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -23,13 +23,15 @@ class Cache extends \Friendica\BaseObject /** * @var Cache\ICacheDriver */ - private static $driver = null; + private static $driver = null; + public static $driver_class = null; + public static $driver_name = null; public static function init() { - $driver_name = Config::get('system', 'cache_driver', 'database'); - - self::$driver = CacheDriverFactory::create($driver_name); + self::$driver_name = Config::get('system', 'cache_driver', 'database'); + self::$driver = CacheDriverFactory::create(self::$driver_name); + self::$driver_class = get_class(self::$driver); } /** From be0f71bedbb10b84e5925315876cce261177dc2c Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 22:59:22 -0400 Subject: [PATCH 5/7] Add new Cache console command --- src/Core/Console.php | 2 + src/Core/Console/Cache.php | 180 +++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 src/Core/Console/Cache.php diff --git a/src/Core/Console.php b/src/Core/Console.php index 29ccd795b8..9edc12b64c 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -14,6 +14,7 @@ class Console extends \Asika\SimpleConsole\Console protected $customHelpOptions = ['h', 'help', '?']; protected $subConsoles = [ + 'cache' => __NAMESPACE__ . '\Console\Cache', 'config' => __NAMESPACE__ . '\Console\Config', 'createdoxygen' => __NAMESPACE__ . '\Console\CreateDoxygen', 'docbloxerrorchecker' => __NAMESPACE__ . '\Console\DocBloxErrorChecker', @@ -37,6 +38,7 @@ class Console extends \Asika\SimpleConsole\Console Usage: bin/console [--version] [-h|--help|-?] [] [-v] Commands: + cache Manage node cache config Edit site config createdoxygen Generate Doxygen headers dbstructure Do database updates diff --git a/src/Core/Console/Cache.php b/src/Core/Console/Cache.php new file mode 100644 index 0000000000..d0bc427dee --- /dev/null +++ b/src/Core/Console/Cache.php @@ -0,0 +1,180 @@ + + */ +class Cache extends \Asika\SimpleConsole\Console +{ + protected $helpOptions = ['h', 'help', '?']; + + protected function getHelp() + { + $help = << [-h|--help|-?] [-v] + bin/console cache set [-h|--help|-?] [-v] + bin/console cache flush [-h|--help|-?] [-v] + bin/console cache clear [-h|--help|-?] [-v] + +Description + bin/console cache list [] + List all cache keys, optionally filtered by a prefix + + bin/console cache get + Shows the value of the provided cache key + + bin/console cache set [] + Sets the value of the provided cache key, optionally with the provided TTL (time to live) with a default of five minutes. + + bin/console cache flush + Clears expired cache keys + + bin/console cache clear + Clears all cache keys + +Options + -h|--help|-? Show help information + -v Show more debug information. +HELP; + return $help; + } + + protected function doExecute() + { + $a = \Friendica\BaseObject::getApp(); + + if ($this->getOption('v')) { + $this->out('Executable: ' . $this->executable); + $this->out('Class: ' . __CLASS__); + $this->out('Arguments: ' . var_export($this->args, true)); + $this->out('Options: ' . var_export($this->options, true)); + } + + if (!($a->mode & App::MODE_DBCONFIGAVAILABLE)) { + $this->out('Database isn\'t ready or populated yet, database cache won\'t be available'); + } + + Core\Cache::init(); + + if ($this->getOption('v')) { + $this->out('Cache Driver Name: ' . Core\Cache::$driver_name); + $this->out('Cache Driver Class: ' . Core\Cache::$driver_class); + } + + switch ($this->getArgument(0)) { + case 'list': + $this->executeList(); + break; + case 'get': + $this->executeGet(); + break; + case 'set': + $this->executeSet(); + break; + case 'flush': + $this->executeFlush(); + break; + case 'clear': + $this->executeClear(); + break; + } + + if (count($this->args) == 0) { + $this->out($this->getHelp()); + return 0; + } + + return 0; + } + + private function executeList() + { + $prefix = $this->getArgument(1); + $keys = Core\Cache::getAllKeys(); + + if (empty($prefix)) { + $this->out('Listing all cache keys:'); + } else { + $this->out('Listing all cache keys starting with "' . $prefix . '":'); + } + + $count = 0; + foreach ($keys as $key) { + if (empty($prefix) || strpos($key, $prefix) === 0) { + $this->out($key); + $count++; + } + } + + $this->out($count . ' keys found'); + } + + private function executeGet() + { + if (count($this->args) >= 2) { + $key = $this->getArgument(1); + $value = Core\Cache::get($key); + + $this->out("{$key} => " . var_export($value, true)); + } else { + throw new CommandArgsException('Too few arguments for get'); + } + } + + private function executeSet() + { + if (count($this->args) >= 3) { + $key = $this->getArgument(1); + $value = $this->getArgument(2); + $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES)); + + if (is_array(Core\Cache::get($key))) { + throw new RuntimeException("$key is an array and can't be set using this command."); + } + + $result = Core\Cache::set($key, $value, $duration); + if ($result) { + $this->out("{$key} <= " . Core\Cache::get($key)); + } else { + $this->out("Unable to set {$key}"); + } + } else { + throw new CommandArgsException('Too few arguments for set'); + } + } + + private function executeFlush() + { + $result = Core\Cache::clear(); + if ($result) { + $this->out('Cache successfully flushed'); + } else { + $this->out('Unable to flush the cache'); + } + } + + private function executeClear() + { + $result = Core\Cache::clear(false); + if ($result) { + $this->out('Cache successfully cleared'); + } else { + $this->out('Unable to flush the cache'); + } + } +} From c80045edafef1498769d3567461b49afa935dcaf Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 25 Sep 2018 23:00:00 -0400 Subject: [PATCH 6/7] Remove obsolete lines from Console\Config --- src/Core/Console/Config.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Core/Console/Config.php b/src/Core/Console/Config.php index da2d20fd13..db436b8394 100644 --- a/src/Core/Console/Config.php +++ b/src/Core/Console/Config.php @@ -1,11 +1,5 @@ getOption('v')) { $this->out('Executable: ' . $this->executable); From 2a7327a41c3ea9b9910c9426956c93761bf56121 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 3 Oct 2018 00:24:26 -0400 Subject: [PATCH 7/7] Fix spaces in Cache\Memcachedriver --- src/Core/Cache/MemcacheCacheDriver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Core/Cache/MemcacheCacheDriver.php b/src/Core/Cache/MemcacheCacheDriver.php index 9e5246f5b6..207225b1a2 100644 --- a/src/Core/Cache/MemcacheCacheDriver.php +++ b/src/Core/Cache/MemcacheCacheDriver.php @@ -47,10 +47,10 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri { $list = []; $allSlabs = $this->memcache->getExtendedStats('slabs'); - foreach($allSlabs as $slabs) { - foreach(array_keys($slabs) as $slabId) { + 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 $keys => $arrVal) { if (!is_array($arrVal)) { continue; }