2017-11-09 11:05:18 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Core/Cache.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2018-02-28 23:48:09 -05:00
|
|
|
use Friendica\Core\Cache;
|
2017-11-09 11:05:18 -05:00
|
|
|
use Friendica\Core\Config;
|
2017-12-17 15:24:57 -05:00
|
|
|
|
2017-11-09 11:05:18 -05:00
|
|
|
/**
|
|
|
|
* @brief Class for storing data for a short time
|
|
|
|
*/
|
|
|
|
class Cache
|
|
|
|
{
|
2018-02-28 23:48:09 -05:00
|
|
|
const MONTH = 0;
|
|
|
|
const WEEK = 1;
|
|
|
|
const DAY = 2;
|
|
|
|
const HOUR = 3;
|
|
|
|
const HALF_HOUR = 4;
|
|
|
|
const QUARTER_HOUR = 5;
|
|
|
|
const FIVE_MINUTES = 6;
|
|
|
|
const MINUTE = 7;
|
|
|
|
|
2017-11-09 11:05:18 -05:00
|
|
|
/**
|
2018-02-28 23:48:09 -05:00
|
|
|
* @var Cache\ICacheDriver
|
2017-11-09 11:05:18 -05:00
|
|
|
*/
|
2018-02-28 23:48:09 -05:00
|
|
|
static $driver = null;
|
2017-11-09 11:05:18 -05:00
|
|
|
|
2018-02-28 23:48:09 -05:00
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
switch(Config::get('system', 'cache_driver', 'database')) {
|
|
|
|
case 'memcache':
|
|
|
|
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
|
|
|
|
$memcache_port = Config::get('system', 'memcache_port', 11211);
|
2017-11-09 11:05:18 -05:00
|
|
|
|
2018-02-28 23:48:09 -05:00
|
|
|
self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
|
|
|
|
break;
|
|
|
|
case 'memcached':
|
|
|
|
$memcached_host = Config::get('system', 'memcached_host', '127.0.0.1');
|
|
|
|
$memcached_port = Config::get('system', 'memcached_port', 11211);
|
2017-11-09 11:05:18 -05:00
|
|
|
|
2018-02-28 23:48:09 -05:00
|
|
|
self::$driver = new Cache\MemcachedCacheDriver($memcached_host, $memcached_port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self::$driver = new Cache\DatabaseCacheDriver();
|
2017-11-09 11:05:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the duration for a given cache level
|
|
|
|
*
|
|
|
|
* @param integer $level Cache level
|
|
|
|
*
|
|
|
|
* @return integer The cache duration in seconds
|
|
|
|
*/
|
2018-02-28 23:48:09 -05:00
|
|
|
public static function duration($level)
|
2017-11-09 11:05:18 -05:00
|
|
|
{
|
|
|
|
switch ($level) {
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::MONTH:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 2592000;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::WEEK:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 604800;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::DAY:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 86400;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::HOUR:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 3600;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::HALF_HOUR:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 1800;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::QUARTER_HOUR:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 900;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::FIVE_MINUTES:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 300;
|
|
|
|
break;
|
2018-02-28 23:48:09 -05:00
|
|
|
case self::MINUTE:
|
2018-02-14 00:05:00 -05:00
|
|
|
default:
|
2017-11-09 11:05:18 -05:00
|
|
|
$seconds = 60;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $seconds;
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:48:09 -05:00
|
|
|
/**
|
|
|
|
* Returns the current cache driver
|
|
|
|
*
|
|
|
|
* @return Cache\ICacheDriver
|
|
|
|
*/
|
|
|
|
private static function getDriver()
|
|
|
|
{
|
|
|
|
if (self::$driver === null) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$driver;
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:05:18 -05:00
|
|
|
/**
|
|
|
|
* @brief Fetch cached data according to the key
|
|
|
|
*
|
|
|
|
* @param string $key The key to the cached data
|
|
|
|
*
|
|
|
|
* @return mixed Cached $value or "null" if not found
|
|
|
|
*/
|
|
|
|
public static function get($key)
|
|
|
|
{
|
2018-02-28 23:48:09 -05:00
|
|
|
return self::getDriver()->get($key);
|
2017-11-09 11:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put data in the cache according to the key
|
|
|
|
*
|
|
|
|
* The input $value can have multiple formats.
|
|
|
|
*
|
|
|
|
* @param string $key The key to the cached data
|
|
|
|
* @param mixed $value The value that is about to be stored
|
|
|
|
* @param integer $duration The cache lifespan
|
2017-11-19 14:15:25 -05:00
|
|
|
*
|
2018-02-28 23:48:09 -05:00
|
|
|
* @return bool
|
2017-11-09 11:05:18 -05:00
|
|
|
*/
|
2018-02-28 23:48:09 -05:00
|
|
|
public static function set($key, $value, $duration = self::MONTH)
|
2017-11-09 11:05:18 -05:00
|
|
|
{
|
2018-02-28 23:48:09 -05:00
|
|
|
return self::getDriver()->set($key, $value, $duration);
|
2017-11-09 11:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove outdated data from the cache
|
|
|
|
*
|
|
|
|
* @param integer $max_level The maximum cache level that is to be cleared
|
2017-11-19 14:15:25 -05:00
|
|
|
*
|
|
|
|
* @return void
|
2017-11-09 11:05:18 -05:00
|
|
|
*/
|
2018-02-28 23:48:09 -05:00
|
|
|
public static function clear()
|
2017-11-09 11:05:18 -05:00
|
|
|
{
|
2018-02-28 23:48:09 -05:00
|
|
|
return self::getDriver()->clear();
|
2017-11-09 11:05:18 -05:00
|
|
|
}
|
|
|
|
}
|