Merge pull request #5792 from MrPetovan/task/add-cache-console

Add Cache console
This commit is contained in:
Michael Vogel 2018-10-03 11:17:43 +02:00 committed by GitHub
commit ec898ecd2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 326 additions and 23 deletions

View File

@ -24,12 +24,14 @@ class Cache extends \Friendica\BaseObject
* @var Cache\ICacheDriver
*/
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);
}
/**
@ -46,6 +48,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
*
@ -107,12 +132,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);
}
}

View 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)
*/

View File

@ -13,6 +13,19 @@ 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)
*/
public function get($key)
{
$cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
@ -32,6 +45,9 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
return null;
}
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$fields = [
@ -43,11 +59,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) {

View 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
*

View File

@ -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)) {
@ -35,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)
*/

View File

@ -53,6 +53,17 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
}
}
/**
* (@inheritdoc)
*/
public function getAllKeys()
{
return $this->memcached->getAllKeys();
}
/**
* (@inheritdoc)
*/
public function get($key)
{
$return = null;
@ -68,6 +79,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 +102,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 +124,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)
{

View File

@ -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,17 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
}
}
/**
* (@inheritdoc)
*/
public function getAllKeys()
{
return null;
}
/**
* (@inheritdoc)
*/
public function get($key)
{
$return = null;
@ -55,6 +71,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 +94,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 +152,7 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
$this->redis->unwatch();
return false;
}
/**
* (@inheritdoc)
*/

View File

@ -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|-?] <command> [<args>] [-v]
Commands:
cache Manage node cache
config Edit site config
createdoxygen Generate Doxygen headers
dbstructure Do database updates

180
src/Core/Console/Cache.php Normal file
View File

@ -0,0 +1,180 @@
<?php
namespace Friendica\Core\Console;
use Asika\SimpleConsole\CommandArgsException;
use Friendica\App;
use Friendica\Core;
use RuntimeException;
/**
* @brief tool to access the cache from the CLI
*
* With this script you can access the cache of your node from the CLI.
* You can read current values stored in the cache and set new values
* in cache keys.
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class Cache extends \Asika\SimpleConsole\Console
{
protected $helpOptions = ['h', 'help', '?'];
protected function getHelp()
{
$help = <<<HELP
console cache - Manage node cache
Synopsis
bin/console cache list [-h|--help|-?] [-v]
bin/console cache get <key> [-h|--help|-?] [-v]
bin/console cache set <key> <value> [-h|--help|-?] [-v]
bin/console cache flush [-h|--help|-?] [-v]
bin/console cache clear [-h|--help|-?] [-v]
Description
bin/console cache list [<prefix>]
List all cache keys, optionally filtered by a prefix
bin/console cache get <key>
Shows the value of the provided cache key
bin/console cache set <key> <value> [<ttl>]
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');
}
}
}

View File

@ -1,11 +1,5 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Friendica\Core\Console;
use Asika\SimpleConsole\CommandArgsException;
@ -13,9 +7,6 @@ use Friendica\App;
use Friendica\Core;
use RuntimeException;
require_once 'include/dba.php';
require_once 'include/text.php';
/**
* @brief tool to access the system config from the CLI
*
@ -80,7 +71,7 @@ HELP;
protected function doExecute()
{
$a = get_app();
$a = \Friendica\BaseObject::getApp();
if ($this->getOption('v')) {
$this->out('Executable: ' . $this->executable);