X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FCache.php;h=ea7807031fde7389479dfb2bad49c1d342094e2c;hb=743e7d3ecb4bc06999284bffd977265abc23731f;hp=3f2edd2e20cefedafe4109194cf628ca096ed425;hpb=3086666c38e6595d2254a08534668b900524927f;p=friendica.git diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 3f2edd2e20..ea7807031f 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -4,8 +4,7 @@ */ namespace Friendica\Core; -use Friendica\Core\Cache; -use Friendica\Core\Config; +use Friendica\Core\Cache\CacheDriverFactory; /** * @brief Class for storing data for a short time @@ -24,25 +23,15 @@ class Cache extends \Friendica\BaseObject /** * @var Cache\ICacheDriver */ - static $driver = null; + private static $driver = null; + public static $driver_class = null; + public static $driver_name = null; public static function init() { - switch(Config::get('system', 'cache_driver', 'database')) { - case 'memcache': - $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1'); - $memcache_port = Config::get('system', 'memcache_port', 11211); - - self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port); - break; - case 'memcached': - $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]); - - self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts); - break; - default: - self::$driver = new Cache\DatabaseCacheDriver(); - } + self::$driver_name = Config::get('system', 'cache_driver', 'database'); + self::$driver = CacheDriverFactory::create(self::$driver_name); + self::$driver_class = get_class(self::$driver); } /** @@ -59,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 * @@ -99,15 +111,33 @@ class Cache extends \Friendica\BaseObject return $return; } + /** + * @brief Delete a value from the cache + * + * @param string $key The key to the cached data + * + * @return bool + */ + public static function delete($key) + { + $time = microtime(true); + + $return = self::getDriver()->delete($key); + + self::getApp()->save_timestamp($time, 'cache_write'); + + return $return; + } + /** * @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); } }