X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FCache.php;h=7a8f7367ec10597613b7ed94385bfdf85c2464f0;hb=79e69ee52807967858f5cb89e702be035f979ba5;hp=3f2edd2e20cefedafe4109194cf628ca096ed425;hpb=b6b67c9044b22a2f04d17dd03a81c794919a27a2;p=friendica.git diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 3f2edd2e20..7a8f7367ec 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\Factory\CacheDriverFactory; /** * @brief Class for storing data for a short time @@ -20,29 +19,20 @@ class Cache extends \Friendica\BaseObject const QUARTER_HOUR = 900; const FIVE_MINUTES = 300; const MINUTE = 60; + const INFINITE = 0; /** * @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,12 +49,32 @@ class Cache extends \Friendica\BaseObject return self::$driver; } + /** + * @brief Returns all the cache keys sorted alphabetically + * + * @param string $prefix Prefix of the keys (optional) + * + * @return array Empty if the driver doesn't support this feature + * @throws \Exception + */ + public static function getAllKeys($prefix = null) + { + $time = microtime(true); + + $return = self::getDriver()->getAllKeys($prefix); + + self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack()); + + return $return; + } + /** * @brief Fetch cached data according to the key * * @param string $key The key to the cached data * * @return mixed Cached $value or "null" if not found + * @throws \Exception */ public static function get($key) { @@ -72,7 +82,7 @@ class Cache extends \Friendica\BaseObject $return = self::getDriver()->get($key); - self::getApp()->save_timestamp($time, 'cache'); + self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack()); return $return; } @@ -87,6 +97,7 @@ class Cache extends \Friendica\BaseObject * @param integer $duration The cache lifespan * * @return bool + * @throws \Exception */ public static function set($key, $value, $duration = self::MONTH) { @@ -94,7 +105,26 @@ class Cache extends \Friendica\BaseObject $return = self::getDriver()->set($key, $value, $duration); - self::getApp()->save_timestamp($time, 'cache_write'); + self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack()); + + return $return; + } + + /** + * @brief Delete a value from the cache + * + * @param string $key The key to the cached data + * + * @return bool + * @throws \Exception + */ + public static function delete($key) + { + $time = microtime(true); + + $return = self::getDriver()->delete($key); + + self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack()); return $return; } @@ -102,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 + * @return bool */ - public static function clear() + public static function clear($outdated = true) { - return self::getDriver()->clear(); + return self::getDriver()->clear($outdated); } }