3 * @file src/Core/Cache.php
5 namespace Friendica\Core;
7 use Friendica\Core\Cache\CacheDriverFactory;
10 * @brief Class for storing data for a short time
12 class Cache extends \Friendica\BaseObject
14 const MONTH = 2592000;
18 const HALF_HOUR = 1800;
19 const QUARTER_HOUR = 900;
20 const FIVE_MINUTES = 300;
24 * @var Cache\ICacheDriver
26 private static $driver = null;
28 public static function init()
30 $driver_name = Config::get('system', 'cache_driver', 'database');
32 self::$driver = CacheDriverFactory::create($driver_name);
36 * Returns the current cache driver
38 * @return Cache\ICacheDriver
40 private static function getDriver()
42 if (self::$driver === null) {
50 * @brief Returns all the cache keys sorted alphabetically
52 * @return array|null Null if the driver doesn't support this feature
54 public static function getAllKeys()
56 $time = microtime(true);
58 $return = self::getDriver()->getAllKeys();
60 // Keys are prefixed with the node hostname, let's remove it
61 array_walk($return, function (&$value) {
62 $value = preg_replace('/^' . self::getApp()->get_hostname() . ':/', '', $value);
67 self::getApp()->save_timestamp($time, 'cache');
73 * @brief Fetch cached data according to the key
75 * @param string $key The key to the cached data
77 * @return mixed Cached $value or "null" if not found
79 public static function get($key)
81 $time = microtime(true);
83 $return = self::getDriver()->get($key);
85 self::getApp()->save_timestamp($time, 'cache');
91 * @brief Put data in the cache according to the key
93 * The input $value can have multiple formats.
95 * @param string $key The key to the cached data
96 * @param mixed $value The value that is about to be stored
97 * @param integer $duration The cache lifespan
101 public static function set($key, $value, $duration = self::MONTH)
103 $time = microtime(true);
105 $return = self::getDriver()->set($key, $value, $duration);
107 self::getApp()->save_timestamp($time, 'cache_write');
113 * @brief Delete a value from the cache
115 * @param string $key The key to the cached data
119 public static function delete($key)
121 $time = microtime(true);
123 $return = self::getDriver()->delete($key);
125 self::getApp()->save_timestamp($time, 'cache_write');
131 * @brief Remove outdated data from the cache
133 * @param boolean $outdated just remove outdated values
137 public static function clear($outdated = true)
139 return self::getDriver()->clear($outdated);