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 Fetch cached data according to the key
52 * @param string $key The key to the cached data
54 * @return mixed Cached $value or "null" if not found
56 public static function get($key)
58 $time = microtime(true);
60 $return = self::getDriver()->get($key);
62 self::getApp()->save_timestamp($time, 'cache');
68 * @brief Put data in the cache according to the key
70 * The input $value can have multiple formats.
72 * @param string $key The key to the cached data
73 * @param mixed $value The value that is about to be stored
74 * @param integer $duration The cache lifespan
78 public static function set($key, $value, $duration = self::MONTH)
80 $time = microtime(true);
82 $return = self::getDriver()->set($key, $value, $duration);
84 self::getApp()->save_timestamp($time, 'cache_write');
90 * @brief Delete a value from the cache
92 * @param string $key The key to the cached data
96 public static function delete($key)
98 $time = microtime(true);
100 $return = self::getDriver()->delete($key);
102 self::getApp()->save_timestamp($time, 'cache_write');
108 * @brief Remove outdated data from the cache
110 * @param integer $max_level The maximum cache level that is to be cleared
114 public static function clear()
116 return self::getDriver()->clear();