3 * @file src/Core/Cache.php
5 namespace Friendica\Core;
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
11 * @brief Class for storing data for a short time
13 class Cache extends \Friendica\BaseObject
20 const QUARTER_HOUR = 5;
21 const FIVE_MINUTES = 6;
25 * @var Cache\ICacheDriver
27 static $driver = null;
29 public static function init()
31 switch(Config::get('system', 'cache_driver', 'database')) {
33 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
34 $memcache_port = Config::get('system', 'memcache_port', 11211);
36 self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
39 $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
41 self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
44 self::$driver = new Cache\DatabaseCacheDriver();
49 * @brief Return the duration for a given cache level
51 * @param integer $level Cache level
53 * @return integer The cache duration in seconds
55 public static function duration($level)
73 case self::QUARTER_HOUR:
76 case self::FIVE_MINUTES:
88 * Returns the current cache driver
90 * @return Cache\ICacheDriver
92 private static function getDriver()
94 if (self::$driver === null) {
102 * @brief Fetch cached data according to the key
104 * @param string $key The key to the cached data
106 * @return mixed Cached $value or "null" if not found
108 public static function get($key)
110 $time = microtime(true);
112 $return = self::getDriver()->get($key);
114 self::getApp()->save_timestamp($time, 'cache');
120 * @brief Put data in the cache according to the key
122 * The input $value can have multiple formats.
124 * @param string $key The key to the cached data
125 * @param mixed $value The value that is about to be stored
126 * @param integer $duration The cache lifespan
130 public static function set($key, $value, $duration = self::MONTH)
132 $time = microtime(true);
134 $return = self::getDriver()->set($key, $value, $duration);
136 self::getApp()->save_timestamp($time, 'cache_write');
142 * @brief Remove outdated data from the cache
144 * @param integer $max_level The maximum cache level that is to be cleared
148 public static function clear()
150 return self::getDriver()->clear();