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;
25 * @var Cache\ICacheDriver
27 private static $driver = null;
28 public static $driver_class = null;
29 public static $driver_name = null;
31 public static function init()
33 self::$driver_name = Config::get('system', 'cache_driver', 'database');
34 self::$driver = CacheDriverFactory::create(self::$driver_name);
35 self::$driver_class = get_class(self::$driver);
39 * Returns the current cache driver
41 * @return Cache\ICacheDriver
43 private static function getDriver()
45 if (self::$driver === null) {
53 * @brief Returns all the cache keys sorted alphabetically
55 * @param string $prefix Prefix of the keys (optional)
57 * @return array Empty if the driver doesn't support this feature
59 public static function getAllKeys($prefix = null)
61 $time = microtime(true);
63 $return = self::getDriver()->getAllKeys($prefix);
65 self::getApp()->saveTimestamp($time, 'cache');
71 * @brief Fetch cached data according to the key
73 * @param string $key The key to the cached data
75 * @return mixed Cached $value or "null" if not found
77 public static function get($key)
79 $time = microtime(true);
81 $return = self::getDriver()->get($key);
83 self::getApp()->saveTimestamp($time, 'cache');
89 * @brief Put data in the cache according to the key
91 * The input $value can have multiple formats.
93 * @param string $key The key to the cached data
94 * @param mixed $value The value that is about to be stored
95 * @param integer $duration The cache lifespan
99 public static function set($key, $value, $duration = self::MONTH)
101 $time = microtime(true);
103 $return = self::getDriver()->set($key, $value, $duration);
105 self::getApp()->saveTimestamp($time, 'cache_write');
111 * @brief Delete a value from the cache
113 * @param string $key The key to the cached data
117 public static function delete($key)
119 $time = microtime(true);
121 $return = self::getDriver()->delete($key);
123 self::getApp()->saveTimestamp($time, 'cache_write');
129 * @brief Remove outdated data from the cache
131 * @param boolean $outdated just remove outdated values
135 public static function clear($outdated = true)
137 return self::getDriver()->clear($outdated);