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 * @param string $prefix Prefix of the keys (optional)
56 * @return array Empty if the driver doesn't support this feature
58 public static function getAllKeys($prefix = null)
60 $time = microtime(true);
62 $return = self::getDriver()->getAllKeys($prefix);
64 self::getApp()->saveTimestamp($time, 'cache');
70 * @brief Fetch cached data according to the key
72 * @param string $key The key to the cached data
74 * @return mixed Cached $value or "null" if not found
76 public static function get($key)
78 $time = microtime(true);
80 $return = self::getDriver()->get($key);
82 self::getApp()->saveTimestamp($time, 'cache');
88 * @brief Put data in the cache according to the key
90 * The input $value can have multiple formats.
92 * @param string $key The key to the cached data
93 * @param mixed $value The value that is about to be stored
94 * @param integer $duration The cache lifespan
98 public static function set($key, $value, $duration = self::MONTH)
100 $time = microtime(true);
102 $return = self::getDriver()->set($key, $value, $duration);
104 self::getApp()->saveTimestamp($time, 'cache_write');
110 * @brief Delete a value from the cache
112 * @param string $key The key to the cached data
116 public static function delete($key)
118 $time = microtime(true);
120 $return = self::getDriver()->delete($key);
122 self::getApp()->saveTimestamp($time, 'cache_write');
128 * @brief Remove outdated data from the cache
130 * @param boolean $outdated just remove outdated values
134 public static function clear($outdated = true)
136 return self::getDriver()->clear($outdated);