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
60 public static function getAllKeys($prefix = null)
62 $time = microtime(true);
64 $return = self::getDriver()->getAllKeys($prefix);
66 self::getApp()->saveTimestamp($time, 'cache');
72 * @brief Fetch cached data according to the key
74 * @param string $key The key to the cached data
76 * @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()->saveTimestamp($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
102 public static function set($key, $value, $duration = self::MONTH)
104 $time = microtime(true);
106 $return = self::getDriver()->set($key, $value, $duration);
108 self::getApp()->saveTimestamp($time, 'cache_write');
114 * @brief Delete a value from the cache
116 * @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()->saveTimestamp($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);