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
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_host = Config::get('system', 'memcached_host', '127.0.0.1');
40 $memcached_port = Config::get('system', 'memcached_port', 11211);
42 self::$driver = new Cache\MemcachedCacheDriver($memcached_host, $memcached_port);
45 self::$driver = new Cache\DatabaseCacheDriver();
50 * @brief Return the duration for a given cache level
52 * @param integer $level Cache level
54 * @return integer The cache duration in seconds
56 public static function duration($level)
74 case self::QUARTER_HOUR:
77 case self::FIVE_MINUTES:
89 * Returns the current cache driver
91 * @return Cache\ICacheDriver
93 private static function getDriver()
95 if (self::$driver === null) {
103 * @brief Fetch cached data according to the key
105 * @param string $key The key to the cached data
107 * @return mixed Cached $value or "null" if not found
109 public static function get($key)
111 return self::getDriver()->get($key);
115 * @brief Put data in the cache according to the key
117 * The input $value can have multiple formats.
119 * @param string $key The key to the cached data
120 * @param mixed $value The value that is about to be stored
121 * @param integer $duration The cache lifespan
125 public static function set($key, $value, $duration = self::MONTH)
127 return self::getDriver()->set($key, $value, $duration);
131 * @brief Remove outdated data from the cache
133 * @param integer $max_level The maximum cache level that is to be cleared
137 public static function clear()
139 return self::getDriver()->clear();