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;
27 public static $driver_class = null;
28 public static $driver_name = null;
30 public static function init()
32 self::$driver_name = Config::get('system', 'cache_driver', 'database');
33 self::$driver = CacheDriverFactory::create(self::$driver_name);
34 self::$driver_class = get_class(self::$driver);
38 * Returns the current cache driver
40 * @return Cache\ICacheDriver
42 private static function getDriver()
44 if (self::$driver === null) {
52 * @brief Returns all the cache keys sorted alphabetically
54 * @return array|null Null if the driver doesn't support this feature
56 public static function getAllKeys()
58 $time = microtime(true);
60 $return = self::getDriver()->getAllKeys();
62 // Keys are prefixed with the node hostname, let's remove it
63 array_walk($return, function (&$value) {
64 $value = preg_replace('/^' . self::getApp()->get_hostname() . ':/', '', $value);
69 self::getApp()->save_timestamp($time, 'cache');
75 * @brief Fetch cached data according to the key
77 * @param string $key The key to the cached data
79 * @return mixed Cached $value or "null" if not found
81 public static function get($key)
83 $time = microtime(true);
85 $return = self::getDriver()->get($key);
87 self::getApp()->save_timestamp($time, 'cache');
93 * @brief Put data in the cache according to the key
95 * The input $value can have multiple formats.
97 * @param string $key The key to the cached data
98 * @param mixed $value The value that is about to be stored
99 * @param integer $duration The cache lifespan
103 public static function set($key, $value, $duration = self::MONTH)
105 $time = microtime(true);
107 $return = self::getDriver()->set($key, $value, $duration);
109 self::getApp()->save_timestamp($time, 'cache_write');
115 * @brief Delete a value from the cache
117 * @param string $key The key to the cached data
121 public static function delete($key)
123 $time = microtime(true);
125 $return = self::getDriver()->delete($key);
127 self::getApp()->save_timestamp($time, 'cache_write');
133 * @brief Remove outdated data from the cache
135 * @param boolean $outdated just remove outdated values
139 public static function clear($outdated = true)
141 return self::getDriver()->clear($outdated);