2017-11-09 17:05:18 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Core/Cache.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2018-03-01 05:48:09 +01:00
|
|
|
use Friendica\Core\Cache;
|
2017-11-09 17:05:18 +01:00
|
|
|
use Friendica\Core\Config;
|
2017-12-17 21:24:57 +01:00
|
|
|
|
2017-11-09 17:05:18 +01:00
|
|
|
/**
|
|
|
|
* @brief Class for storing data for a short time
|
|
|
|
*/
|
2018-03-01 06:33:56 +01:00
|
|
|
class Cache extends \Friendica\BaseObject
|
2017-11-09 17:05:18 +01:00
|
|
|
{
|
2018-03-01 05:48:09 +01: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 17:05:18 +01:00
|
|
|
/**
|
2018-03-01 05:48:09 +01:00
|
|
|
* @var Cache\ICacheDriver
|
2017-11-09 17:05:18 +01:00
|
|
|
*/
|
2018-03-01 05:48:09 +01:00
|
|
|
static $driver = null;
|
2017-11-09 17:05:18 +01:00
|
|
|
|
2018-03-01 05:48:09 +01: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 17:05:18 +01:00
|
|
|
|
2018-03-01 05:48:09 +01:00
|
|
|
self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
|
|
|
|
break;
|
|
|
|
case 'memcached':
|
2018-03-05 05:28:49 +01:00
|
|
|
$memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
|
2017-11-09 17:05:18 +01:00
|
|
|
|
2018-03-05 05:28:49 +01:00
|
|
|
self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
|
2018-03-01 05:48:09 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self::$driver = new Cache\DatabaseCacheDriver();
|
2017-11-09 17:05:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the duration for a given cache level
|
|
|
|
*
|
|
|
|
* @param integer $level Cache level
|
|
|
|
*
|
|
|
|
* @return integer The cache duration in seconds
|
|
|
|
*/
|
2018-03-01 05:48:09 +01:00
|
|
|
public static function duration($level)
|
2017-11-09 17:05:18 +01:00
|
|
|
{
|
|
|
|
switch ($level) {
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::MONTH:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 2592000;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::WEEK:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 604800;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::DAY:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 86400;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::HOUR:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 3600;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::HALF_HOUR:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 1800;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::QUARTER_HOUR:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 900;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::FIVE_MINUTES:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 300;
|
|
|
|
break;
|
2018-03-01 05:48:09 +01:00
|
|
|
case self::MINUTE:
|
2018-02-14 06:05:00 +01:00
|
|
|
default:
|
2017-11-09 17:05:18 +01:00
|
|
|
$seconds = 60;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $seconds;
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:48:09 +01: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 17:05:18 +01: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-03-01 06:33:56 +01:00
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = self::getDriver()->get($key);
|
|
|
|
|
|
|
|
self::getApp()->save_timestamp($time, 'cache');
|
|
|
|
|
|
|
|
return $return;
|
2017-11-09 17:05:18 +01: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 20:15:25 +01:00
|
|
|
*
|
2018-03-01 05:48:09 +01:00
|
|
|
* @return bool
|
2017-11-09 17:05:18 +01:00
|
|
|
*/
|
2018-03-01 05:48:09 +01:00
|
|
|
public static function set($key, $value, $duration = self::MONTH)
|
2017-11-09 17:05:18 +01:00
|
|
|
{
|
2018-03-01 06:33:56 +01:00
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
$return = self::getDriver()->set($key, $value, $duration);
|
|
|
|
|
|
|
|
self::getApp()->save_timestamp($time, 'cache_write');
|
|
|
|
|
|
|
|
return $return;
|
2017-11-09 17:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove outdated data from the cache
|
|
|
|
*
|
|
|
|
* @param integer $max_level The maximum cache level that is to be cleared
|
2017-11-19 20:15:25 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2017-11-09 17:05:18 +01:00
|
|
|
*/
|
2018-03-01 05:48:09 +01:00
|
|
|
public static function clear()
|
2017-11-09 17:05:18 +01:00
|
|
|
{
|
2018-03-01 05:48:09 +01:00
|
|
|
return self::getDriver()->clear();
|
2017-11-09 17:05:18 +01:00
|
|
|
}
|
|
|
|
}
|