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
15 const MONTH = 2592000;
19 const HALF_HOUR = 1800;
20 const QUARTER_HOUR = 900;
21 const FIVE_MINUTES = 300;
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 * Returns the current cache driver
51 * @return Cache\ICacheDriver
53 private static function getDriver()
55 if (self::$driver === null) {
63 * @brief Fetch cached data according to the key
65 * @param string $key The key to the cached data
67 * @return mixed Cached $value or "null" if not found
69 public static function get($key)
71 $time = microtime(true);
73 $return = self::getDriver()->get($key);
75 self::getApp()->save_timestamp($time, 'cache');
81 * @brief Put data in the cache according to the key
83 * The input $value can have multiple formats.
85 * @param string $key The key to the cached data
86 * @param mixed $value The value that is about to be stored
87 * @param integer $duration The cache lifespan
91 public static function set($key, $value, $duration = self::MONTH)
93 $time = microtime(true);
95 $return = self::getDriver()->set($key, $value, $duration);
97 self::getApp()->save_timestamp($time, 'cache_write');
103 * @brief Delete a value from the cache
105 * @param string $key The key to the cached data
109 public static function delete($key)
111 $time = microtime(true);
113 $return = self::getDriver()->delete($key);
115 self::getApp()->save_timestamp($time, 'cache_write');
121 * @brief Remove outdated data from the cache
123 * @param integer $max_level The maximum cache level that is to be cleared
127 public static function clear()
129 return self::getDriver()->clear();