mirror of
https://github.com/friendica/friendica
synced 2025-04-26 01:50:11 +00:00
Merge pull request #7465 from nupplaphil/task/dice_cache_lock
Refactor Cache/Lock to DICE
This commit is contained in:
commit
6b7dfd0c71
60 changed files with 1150 additions and 881 deletions
|
@ -4,7 +4,8 @@ namespace Friendica\Console;
|
|||
|
||||
use Asika\SimpleConsole\CommandArgsException;
|
||||
use Friendica\App;
|
||||
use Friendica\Core;
|
||||
use Friendica\Core\Cache\Cache as CacheClass;
|
||||
use Friendica\Core\Cache\ICache;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
|
@ -25,6 +26,11 @@ class Cache extends \Asika\SimpleConsole\Console
|
|||
*/
|
||||
private $appMode;
|
||||
|
||||
/**
|
||||
* @var ICache
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
|
@ -59,11 +65,12 @@ HELP;
|
|||
return $help;
|
||||
}
|
||||
|
||||
public function __construct(App\Mode $appMode, array $argv = null)
|
||||
public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->appMode = $appMode;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
|
@ -79,11 +86,9 @@ HELP;
|
|||
$this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
|
||||
}
|
||||
|
||||
Core\Cache::init();
|
||||
|
||||
if ($this->getOption('v')) {
|
||||
$this->out('Cache Driver Name: ' . Core\Cache::$driver_name);
|
||||
$this->out('Cache Driver Class: ' . Core\Cache::$driver_class);
|
||||
$this->out('Cache Driver Name: ' . $this->cache->getName());
|
||||
$this->out('Cache Driver Class: ' . get_class($this->cache));
|
||||
}
|
||||
|
||||
switch ($this->getArgument(0)) {
|
||||
|
@ -115,7 +120,7 @@ HELP;
|
|||
private function executeList()
|
||||
{
|
||||
$prefix = $this->getArgument(1);
|
||||
$keys = Core\Cache::getAllKeys($prefix);
|
||||
$keys = $this->cache->getAllKeys($prefix);
|
||||
|
||||
if (empty($prefix)) {
|
||||
$this->out('Listing all cache keys:');
|
||||
|
@ -135,8 +140,8 @@ HELP;
|
|||
private function executeGet()
|
||||
{
|
||||
if (count($this->args) >= 2) {
|
||||
$key = $this->getArgument(1);
|
||||
$value = Core\Cache::get($key);
|
||||
$key = $this->getArgument(1);
|
||||
$value = $this->cache->get($key);
|
||||
|
||||
$this->out("{$key} => " . var_export($value, true));
|
||||
} else {
|
||||
|
@ -147,17 +152,17 @@ HELP;
|
|||
private function executeSet()
|
||||
{
|
||||
if (count($this->args) >= 3) {
|
||||
$key = $this->getArgument(1);
|
||||
$value = $this->getArgument(2);
|
||||
$duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
|
||||
$key = $this->getArgument(1);
|
||||
$value = $this->getArgument(2);
|
||||
$duration = intval($this->getArgument(3, CacheClass::FIVE_MINUTES));
|
||||
|
||||
if (is_array(Core\Cache::get($key))) {
|
||||
if (is_array($this->cache->get($key))) {
|
||||
throw new RuntimeException("$key is an array and can't be set using this command.");
|
||||
}
|
||||
|
||||
$result = Core\Cache::set($key, $value, $duration);
|
||||
$result = $this->cache->set($key, $value, $duration);
|
||||
if ($result) {
|
||||
$this->out("{$key} <= " . Core\Cache::get($key));
|
||||
$this->out("{$key} <= " . $this->cache->get($key));
|
||||
} else {
|
||||
$this->out("Unable to set {$key}");
|
||||
}
|
||||
|
@ -168,7 +173,7 @@ HELP;
|
|||
|
||||
private function executeFlush()
|
||||
{
|
||||
$result = Core\Cache::clear();
|
||||
$result = $this->cache->clear();
|
||||
if ($result) {
|
||||
$this->out('Cache successfully flushed');
|
||||
} else {
|
||||
|
@ -178,7 +183,7 @@ HELP;
|
|||
|
||||
private function executeClear()
|
||||
{
|
||||
$result = Core\Cache::clear(false);
|
||||
$result = $this->cache->clear(false);
|
||||
if ($result) {
|
||||
$this->out('Cache successfully cleared');
|
||||
} else {
|
||||
|
|
|
@ -4,50 +4,33 @@
|
|||
*/
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Core\Cache\Cache as CacheClass;
|
||||
use Friendica\Core\Cache\ICache;
|
||||
|
||||
/**
|
||||
* @brief Class for storing data for a short time
|
||||
*/
|
||||
class Cache extends \Friendica\BaseObject
|
||||
class Cache extends BaseObject
|
||||
{
|
||||
const MONTH = 2592000;
|
||||
const WEEK = 604800;
|
||||
const DAY = 86400;
|
||||
const HOUR = 3600;
|
||||
const HALF_HOUR = 1800;
|
||||
const QUARTER_HOUR = 900;
|
||||
const FIVE_MINUTES = 300;
|
||||
const MINUTE = 60;
|
||||
const INFINITE = 0;
|
||||
|
||||
/**
|
||||
* @var Cache\ICacheDriver
|
||||
*/
|
||||
private static $driver = null;
|
||||
public static $driver_class = null;
|
||||
public static $driver_name = null;
|
||||
|
||||
public static function init()
|
||||
{
|
||||
self::$driver_name = Config::get('system', 'cache_driver', 'database');
|
||||
self::$driver = CacheDriverFactory::create(self::$driver_name);
|
||||
self::$driver_class = get_class(self::$driver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current cache driver
|
||||
*
|
||||
* @return Cache\ICacheDriver
|
||||
*/
|
||||
private static function getDriver()
|
||||
{
|
||||
if (self::$driver === null) {
|
||||
self::init();
|
||||
}
|
||||
|
||||
return self::$driver;
|
||||
}
|
||||
/** @deprecated Use CacheClass::MONTH */
|
||||
const MONTH = CacheClass::MONTH;
|
||||
/** @deprecated Use CacheClass::WEEK */
|
||||
const WEEK = CacheClass::WEEK;
|
||||
/** @deprecated Use CacheClass::DAY */
|
||||
const DAY = CacheClass::DAY;
|
||||
/** @deprecated Use CacheClass::HOUR */
|
||||
const HOUR = CacheClass::HOUR;
|
||||
/** @deprecated Use CacheClass::HALF_HOUR */
|
||||
const HALF_HOUR = CacheClass::HALF_HOUR;
|
||||
/** @deprecated Use CacheClass::QUARTER_HOUR */
|
||||
const QUARTER_HOUR = CacheClass::QUARTER_HOUR;
|
||||
/** @deprecated Use CacheClass::FIVE_MINUTES */
|
||||
const FIVE_MINUTES = CacheClass::FIVE_MINUTES;
|
||||
/** @deprecated Use CacheClass::MINUTE */
|
||||
const MINUTE = CacheClass::MINUTE;
|
||||
/** @deprecated Use CacheClass::INFINITE */
|
||||
const INFINITE = CacheClass::INFINITE;
|
||||
|
||||
/**
|
||||
* @brief Returns all the cache keys sorted alphabetically
|
||||
|
@ -59,13 +42,7 @@ class Cache extends \Friendica\BaseObject
|
|||
*/
|
||||
public static function getAllKeys($prefix = null)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = self::getDriver()->getAllKeys($prefix);
|
||||
|
||||
self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
return self::getClass(ICache::class)->getAllKeys($prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,13 +55,7 @@ class Cache extends \Friendica\BaseObject
|
|||
*/
|
||||
public static function get($key)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = self::getDriver()->get($key);
|
||||
|
||||
self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
return self::getClass(ICache::class)->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,15 +70,9 @@ class Cache extends \Friendica\BaseObject
|
|||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function set($key, $value, $duration = self::MONTH)
|
||||
public static function set($key, $value, $duration = CacheClass::MONTH)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = self::getDriver()->set($key, $value, $duration);
|
||||
|
||||
self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack());
|
||||
|
||||
return $return;
|
||||
return self::getClass(ICache::class)->set($key, $value, $duration);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,13 +85,7 @@ class Cache extends \Friendica\BaseObject
|
|||
*/
|
||||
public static function delete($key)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = self::getDriver()->delete($key);
|
||||
|
||||
self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack());
|
||||
|
||||
return $return;
|
||||
return self::getClass(ICache::class)->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -135,9 +94,10 @@ class Cache extends \Friendica\BaseObject
|
|||
* @param boolean $outdated just remove outdated values
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function clear($outdated = true)
|
||||
{
|
||||
return self::getDriver()->clear($outdated);
|
||||
return self::getClass(ICache::class)->clear($outdated);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,14 +3,13 @@
|
|||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* APCu Cache Driver.
|
||||
* APCu Cache.
|
||||
*
|
||||
* @author Philipp Holzer <admin@philipp.info>
|
||||
*/
|
||||
class APCuCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
||||
class APCuCache extends Cache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
|
@ -18,11 +17,13 @@ class APCuCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(string $hostname)
|
||||
{
|
||||
if (!self::isAvailable()) {
|
||||
throw new Exception('APCu is not available.');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,4 +152,12 @@ class APCuCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_APCU;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,14 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Implementation of the IMemoryCacheDriver mainly for testing purpose
|
||||
* Implementation of the IMemoryCache mainly for testing purpose
|
||||
*
|
||||
* Class ArrayCache
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*/
|
||||
class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
||||
class ArrayCache extends Cache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareDelete;
|
||||
|
||||
|
@ -93,4 +90,12 @@ class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_ARRAY;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
use Friendica\BaseObject;
|
||||
|
||||
|
||||
/**
|
||||
* Abstract class for common used functions
|
||||
*
|
||||
* Class AbstractCacheDriver
|
||||
* Class AbstractCache
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*/
|
||||
abstract class AbstractCacheDriver extends BaseObject
|
||||
abstract class Cache implements ICache
|
||||
{
|
||||
const TYPE_APCU = 'apcu';
|
||||
const TYPE_ARRAY = 'array';
|
||||
const TYPE_DATABASE = 'database';
|
||||
const TYPE_MEMCACHE = 'memcache';
|
||||
const TYPE_MEMCACHED = 'memcached';
|
||||
const TYPE_REDIS = 'redis';
|
||||
|
||||
const MONTH = 2592000;
|
||||
const WEEK = 604800;
|
||||
const DAY = 86400;
|
||||
const HOUR = 3600;
|
||||
const HALF_HOUR = 1800;
|
||||
const QUARTER_HOUR = 900;
|
||||
const FIVE_MINUTES = 300;
|
||||
const MINUTE = 60;
|
||||
const INFINITE = 0;
|
||||
|
||||
/**
|
||||
* @var string The hostname
|
||||
*/
|
||||
private $hostName;
|
||||
|
||||
public function __construct(string $hostName)
|
||||
{
|
||||
$this->hostName = $hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prefix (to avoid namespace conflicts)
|
||||
*
|
||||
|
@ -22,7 +47,7 @@ abstract class AbstractCacheDriver extends BaseObject
|
|||
protected function getPrefix()
|
||||
{
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
return self::getApp()->getHostName();
|
||||
return $this->hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,7 +71,7 @@ abstract class AbstractCacheDriver extends BaseObject
|
|||
} else {
|
||||
// Keys are prefixed with the node hostname, let's remove it
|
||||
array_walk($keys, function (&$value) {
|
||||
$value = preg_replace('/^' . self::getApp()->getHostName() . ':/', '', $value);
|
||||
$value = preg_replace('/^' . $this->hostName . ':/', '', $value);
|
||||
});
|
||||
|
||||
sort($keys);
|
||||
|
@ -79,6 +104,5 @@ abstract class AbstractCacheDriver extends BaseObject
|
|||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -2,17 +2,28 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* Database Cache Driver
|
||||
* Database Cache
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
||||
class DatabaseCache extends Cache implements ICache
|
||||
{
|
||||
/**
|
||||
* @var Database
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
public function __construct(string $hostname, Database $dba)
|
||||
{
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->dba = $dba;
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
|
@ -24,13 +35,13 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
$stmt = DBA::select('cache', ['k'], $where);
|
||||
$stmt = $this->dba->select('cache', ['k'], $where);
|
||||
|
||||
$keys = [];
|
||||
while ($key = DBA::fetch($stmt)) {
|
||||
while ($key = $this->dba->fetch($stmt)) {
|
||||
array_push($keys, $key['k']);
|
||||
}
|
||||
DBA::close($stmt);
|
||||
$this->dba->close($stmt);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
@ -40,9 +51,9 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
|
||||
$cache = $this->dba->selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if (DBA::isResult($cache)) {
|
||||
if ($this->dba->isResult($cache)) {
|
||||
$cached = $cache['v'];
|
||||
$value = @unserialize($cached);
|
||||
|
||||
|
@ -76,7 +87,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
];
|
||||
}
|
||||
|
||||
return DBA::update('cache', $fields, ['k' => $key], true);
|
||||
return $this->dba->update('cache', $fields, ['k' => $key], true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,7 +95,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return DBA::delete('cache', ['k' => $key]);
|
||||
return $this->dba->delete('cache', ['k' => $key]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,9 +104,17 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
|||
public function clear($outdated = true)
|
||||
{
|
||||
if ($outdated) {
|
||||
return DBA::delete('cache', ['`expires` < NOW()']);
|
||||
return $this->dba->delete('cache', ['`expires` < NOW()']);
|
||||
} else {
|
||||
return DBA::delete('cache', ['`k` IS NOT NULL ']);
|
||||
return $this->dba->delete('cache', ['`k` IS NOT NULL ']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_DATABASE;
|
||||
}
|
||||
}
|
|
@ -2,14 +2,12 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Cache Driver Interface
|
||||
* Cache Interface
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
interface ICacheDriver
|
||||
interface ICache
|
||||
{
|
||||
/**
|
||||
* Lists all cache keys
|
||||
|
@ -56,4 +54,11 @@ interface ICacheDriver
|
|||
* @return bool
|
||||
*/
|
||||
public function clear($outdated = true);
|
||||
|
||||
/**
|
||||
* Returns the name of the current cache
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
}
|
|
@ -1,16 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* This interface defines methods for Memory-Caches only
|
||||
*
|
||||
* Interface IMemoryCacheDriver
|
||||
* Interface IMemoryCache
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*/
|
||||
interface IMemoryCacheDriver extends ICacheDriver
|
||||
interface IMemoryCache extends ICache
|
||||
{
|
||||
/**
|
||||
* Sets a value if it's not already stored
|
|
@ -2,17 +2,16 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Memcache;
|
||||
|
||||
/**
|
||||
* Memcache Cache Driver
|
||||
* Memcache Cache
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
||||
class MemcacheCache extends Cache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
|
@ -23,18 +22,21 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
private $memcache;
|
||||
|
||||
/**
|
||||
* @param string $memcache_host
|
||||
* @param int $memcache_port
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($memcache_host, $memcache_port)
|
||||
public function __construct(string $hostname, Configuration $config)
|
||||
{
|
||||
if (!class_exists('Memcache', false)) {
|
||||
throw new Exception('Memcache class isn\'t available');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->memcache = new Memcache();
|
||||
|
||||
$memcache_host = $config->get('system', 'memcache_host');
|
||||
$memcache_port = $config->get('system', 'memcache_port');
|
||||
|
||||
if (!$this->memcache->connect($memcache_host, $memcache_port)) {
|
||||
throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
|
||||
}
|
||||
|
@ -45,7 +47,7 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
$keys = [];
|
||||
$keys = [];
|
||||
$allSlabs = $this->memcache->getExtendedStats('slabs');
|
||||
foreach ($allSlabs as $slabs) {
|
||||
foreach (array_keys($slabs) as $slabId) {
|
||||
|
@ -69,7 +71,7 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$return = null;
|
||||
$return = null;
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
|
@ -145,4 +147,12 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
|
|||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_MEMCACHE;
|
||||
}
|
||||
}
|
|
@ -2,18 +2,17 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Logger;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Memcached;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Memcached Cache Driver
|
||||
* Memcached Cache
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
||||
class MemcachedCache extends Cache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
|
@ -23,6 +22,11 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
*/
|
||||
private $memcached;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
|
||||
* array {
|
||||
|
@ -31,16 +35,23 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
* }
|
||||
*
|
||||
* @param array $memcached_hosts
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(array $memcached_hosts)
|
||||
public function __construct(string $hostname, Configuration $config, LoggerInterface $logger)
|
||||
{
|
||||
if (!class_exists('Memcached', false)) {
|
||||
throw new Exception('Memcached class isn\'t available');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->logger = $logger;
|
||||
|
||||
$this->memcached = new Memcached();
|
||||
|
||||
$memcached_hosts = $config->get('system', 'memcached_hosts');
|
||||
|
||||
array_walk($memcached_hosts, function (&$value) {
|
||||
if (is_string($value)) {
|
||||
$value = array_map('trim', explode(',', $value));
|
||||
|
@ -64,7 +75,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
|
||||
return $this->filterArrayKeysByPrefix($keys, $prefix);
|
||||
} else {
|
||||
Logger::log('Memcached \'getAllKeys\' failed with ' . $this->memcached->getResultMessage(), Logger::ALL);
|
||||
$this->logger->debug('Memcached \'getAllKeys\' failed', ['result' => $this->memcached->getResultMessage()]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +85,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$return = null;
|
||||
$return = null;
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
// We fetch with the hostname as key to avoid problems with other applications
|
||||
|
@ -83,7 +94,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
|
||||
$return = $value;
|
||||
} else {
|
||||
Logger::log('Memcached \'get\' failed with ' . $this->memcached->getResultMessage(), Logger::ALL);
|
||||
$this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]);
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
@ -140,4 +151,12 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
|
|||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcached->add($cachekey, $value, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_MEMCACHED;
|
||||
}
|
||||
}
|
162
src/Core/Cache/ProfilerCache.php
Normal file
162
src/Core/Cache/ProfilerCache.php
Normal file
|
@ -0,0 +1,162 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
/**
|
||||
* This class wraps cache driver so they can get profiled - in case the profiler is enabled
|
||||
*
|
||||
* It is using the decorator pattern (@see
|
||||
*/
|
||||
class ProfilerCache implements ICache, IMemoryCache
|
||||
{
|
||||
/**
|
||||
* @var ICache The original cache driver
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @var Profiler The profiler of Friendica
|
||||
*/
|
||||
private $profiler;
|
||||
|
||||
public function __construct(ICache $cache, Profiler $profiler)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->profiler = $profiler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getAllKeys($prefix = null)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->getAllKeys($prefix);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->get($key);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->set($key, $value, $ttl);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->delete($key);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function clear($outdated = true)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->clear($outdated);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->add($key, $value, $ttl);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function compareDelete($key, $value)
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
$time = microtime(true);
|
||||
|
||||
$return = $this->cache->compareDelete($key, $value);
|
||||
|
||||
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
|
||||
|
||||
return $return;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function GetName()
|
||||
{
|
||||
return $this->cache->getName() . ' (with profiler)';
|
||||
}
|
||||
}
|
|
@ -3,16 +3,16 @@
|
|||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Redis;
|
||||
|
||||
/**
|
||||
* Redis Cache Driver. This driver is based on Memcache driver
|
||||
* Redis Cache. This driver is based on Memcache driver
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
* @author Roland Haeder <roland@mxchange.org>
|
||||
*/
|
||||
class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
||||
class RedisCache extends Cache implements IMemoryCache
|
||||
{
|
||||
/**
|
||||
* @var Redis
|
||||
|
@ -20,20 +20,23 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
private $redis;
|
||||
|
||||
/**
|
||||
* @param string $redis_host
|
||||
* @param int $redis_port
|
||||
* @param int $redis_db (Default = 0, maximum is 15)
|
||||
* @param string? $redis_pw
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($redis_host, $redis_port, $redis_db = 0, $redis_pw = null)
|
||||
public function __construct(string $hostname, Configuration $config)
|
||||
{
|
||||
if (!class_exists('Redis', false)) {
|
||||
throw new Exception('Redis class isn\'t available');
|
||||
}
|
||||
|
||||
parent::__construct($hostname);
|
||||
|
||||
$this->redis = new Redis();
|
||||
|
||||
$redis_host = $config->get('system', 'redis_host');
|
||||
$redis_port = $config->get('system', 'redis_port');
|
||||
$redis_pw = $config->get('system', 'redis_password');
|
||||
$redis_db = $config->get('system', 'redis_db', 0);
|
||||
|
||||
if (!$this->redis->connect($redis_host, $redis_port)) {
|
||||
throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
|
||||
}
|
||||
|
@ -188,4 +191,12 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
|||
$this->redis->unwatch();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_REDIS;
|
||||
}
|
||||
}
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Trait TraitCompareSetDelete
|
||||
*
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Trait TraitCompareSetDelete
|
||||
*
|
||||
|
|
|
@ -7,116 +7,28 @@
|
|||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\Factory\CacheDriverFactory;
|
||||
use Friendica\Core\Cache\IMemoryCacheDriver;
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Core\Cache\Cache;
|
||||
use Friendica\Core\Lock\ILock;
|
||||
|
||||
/**
|
||||
* @brief This class contain Functions for preventing parallel execution of functions
|
||||
* This class contain Functions for preventing parallel execution of functions
|
||||
*/
|
||||
class Lock
|
||||
class Lock extends BaseObject
|
||||
{
|
||||
/**
|
||||
* @var Lock\ILockDriver;
|
||||
*/
|
||||
static $driver = null;
|
||||
|
||||
public static function init()
|
||||
{
|
||||
$lock_driver = Config::get('system', 'lock_driver', 'default');
|
||||
|
||||
try {
|
||||
switch ($lock_driver) {
|
||||
case 'memcache':
|
||||
case 'memcached':
|
||||
case 'redis':
|
||||
$cache_driver = CacheDriverFactory::create($lock_driver);
|
||||
if ($cache_driver instanceof IMemoryCacheDriver) {
|
||||
self::$driver = new Lock\CacheLockDriver($cache_driver);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'database':
|
||||
self::$driver = new Lock\DatabaseLockDriver();
|
||||
break;
|
||||
|
||||
case 'semaphore':
|
||||
self::$driver = new Lock\SemaphoreLockDriver();
|
||||
break;
|
||||
|
||||
default:
|
||||
self::useAutoDriver();
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
Logger::log('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'');
|
||||
self::useAutoDriver();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This method tries to find the best - local - locking method for Friendica
|
||||
*
|
||||
* The following sequence will be tried:
|
||||
* 1. Semaphore Locking
|
||||
* 2. Cache Locking
|
||||
* 3. Database Locking
|
||||
*
|
||||
*/
|
||||
private static function useAutoDriver() {
|
||||
|
||||
// 1. Try to use Semaphores for - local - locking
|
||||
if (function_exists('sem_get')) {
|
||||
try {
|
||||
self::$driver = new Lock\SemaphoreLockDriver();
|
||||
return;
|
||||
} catch (\Exception $exception) {
|
||||
Logger::log('Using Semaphore driver for locking failed: ' . $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
|
||||
$cache_driver = Config::get('system', 'cache_driver', 'database');
|
||||
if ($cache_driver != 'database') {
|
||||
try {
|
||||
$lock_driver = CacheDriverFactory::create($cache_driver);
|
||||
if ($lock_driver instanceof IMemoryCacheDriver) {
|
||||
self::$driver = new Lock\CacheLockDriver($lock_driver);
|
||||
}
|
||||
return;
|
||||
} catch (\Exception $exception) {
|
||||
Logger::log('Using Cache driver for locking failed: ' . $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Use Database Locking as a Fallback
|
||||
self::$driver = new Lock\DatabaseLockDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current cache driver
|
||||
*
|
||||
* @return Lock\ILockDriver;
|
||||
*/
|
||||
private static function getDriver()
|
||||
{
|
||||
if (self::$driver === null) {
|
||||
self::init();
|
||||
}
|
||||
|
||||
return self::$driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Acquires a lock for a given name
|
||||
*
|
||||
* @param string $key Name of the lock
|
||||
* @param string $key Name of the lock
|
||||
* @param integer $timeout Seconds until we give up
|
||||
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
|
||||
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return boolean Was the lock successful?
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
return self::getDriver()->acquireLock($key, $timeout, $ttl);
|
||||
return self::getClass(ILock::class)->acquireLock($key, $timeout, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -124,19 +36,22 @@ class Lock
|
|||
*
|
||||
* @param string $key Name of the lock
|
||||
* @param bool $override Overrides the lock to get releases
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function release($key, $override = false)
|
||||
{
|
||||
self::getDriver()->releaseLock($key, $override);
|
||||
return self::getClass(ILock::class)->releaseLock($key, $override);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Releases all lock that were set by us
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function releaseAll()
|
||||
{
|
||||
self::getDriver()->releaseAll();
|
||||
self::getClass(ILock::class)->releaseAll();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,21 +3,21 @@
|
|||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache\IMemoryCacheDriver;
|
||||
use Friendica\Core\Cache\IMemoryCache;
|
||||
|
||||
class CacheLockDriver extends AbstractLockDriver
|
||||
class CacheLock extends Lock
|
||||
{
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\ICacheDriver;
|
||||
* @var \Friendica\Core\Cache\ICache;
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* CacheLockDriver constructor.
|
||||
* CacheLock constructor.
|
||||
*
|
||||
* @param IMemoryCacheDriver $cache The CacheDriver for this type of lock
|
||||
* @param IMemoryCache $cache The CacheDriver for this type of lock
|
||||
*/
|
||||
public function __construct(IMemoryCacheDriver $cache)
|
||||
public function __construct(IMemoryCache $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
$start = time();
|
||||
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
|
@ -65,8 +65,6 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
|
||||
$return = false;
|
||||
|
||||
if ($override) {
|
||||
$return = $this->cache->delete($cachekey);
|
||||
} else {
|
||||
|
@ -83,15 +81,17 @@ class CacheLockDriver extends AbstractLockDriver
|
|||
public function isLocked($key)
|
||||
{
|
||||
$cachekey = self::getLockKey($key);
|
||||
$lock = $this->cache->get($cachekey);
|
||||
$lock = $this->cache->get($cachekey);
|
||||
return isset($lock) && ($lock !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key The original key
|
||||
* @return string The cache key used for the cache
|
||||
* @param string $key The original key
|
||||
*
|
||||
* @return string The cache key used for the cache
|
||||
*/
|
||||
private static function getLockKey($key) {
|
||||
private static function getLockKey($key)
|
||||
{
|
||||
return "lock:" . $key;
|
||||
}
|
||||
}
|
|
@ -3,13 +3,13 @@
|
|||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* Locking driver that stores the locks in the database
|
||||
*/
|
||||
class DatabaseLockDriver extends AbstractLockDriver
|
||||
class DatabaseLock extends Lock
|
||||
{
|
||||
/**
|
||||
* The current ID of the process
|
||||
|
@ -18,11 +18,17 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
*/
|
||||
private $pid;
|
||||
|
||||
/**
|
||||
* @var Database The database connection of Friendica
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @param null|int $pid The Id of the current process (null means determine automatically)
|
||||
*/
|
||||
public function __construct($pid = null)
|
||||
public function __construct(Database $dba, $pid = null)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->pid = isset($pid) ? $pid : getmypid();
|
||||
}
|
||||
|
||||
|
@ -32,13 +38,13 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
$start = time();
|
||||
|
||||
do {
|
||||
DBA::lock('locks');
|
||||
$lock = DBA::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
$this->dba->lock('locks');
|
||||
$lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if (DBA::isResult($lock)) {
|
||||
if ($this->dba->isResult($lock)) {
|
||||
if ($lock['locked']) {
|
||||
// We want to lock something that was already locked by us? So we got the lock.
|
||||
if ($lock['pid'] == $this->pid) {
|
||||
|
@ -46,16 +52,16 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
}
|
||||
}
|
||||
if (!$lock['locked']) {
|
||||
DBA::update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
||||
$this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
||||
$got_lock = true;
|
||||
}
|
||||
} else {
|
||||
DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
||||
$got_lock = true;
|
||||
$this->markAcquire($key);
|
||||
}
|
||||
|
||||
DBA::unlock();
|
||||
$this->dba->unlock();
|
||||
|
||||
if (!$got_lock && ($timeout > 0)) {
|
||||
usleep(rand(100000, 2000000));
|
||||
|
@ -76,7 +82,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
$where = ['name' => $key, 'pid' => $this->pid];
|
||||
}
|
||||
|
||||
$return = DBA::delete('locks', $where);
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
|
||||
$this->markRelease($key);
|
||||
|
||||
|
@ -88,7 +94,7 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
*/
|
||||
public function releaseAll()
|
||||
{
|
||||
$return = DBA::delete('locks', ['pid' => $this->pid]);
|
||||
$return = $this->dba->delete('locks', ['pid' => $this->pid]);
|
||||
|
||||
$this->acquiredLocks = [];
|
||||
|
||||
|
@ -100,9 +106,9 @@ class DatabaseLockDriver extends AbstractLockDriver
|
|||
*/
|
||||
public function isLocked($key)
|
||||
{
|
||||
$lock = DBA::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
$lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
||||
|
||||
if (DBA::isResult($lock)) {
|
||||
if ($this->dba->isResult($lock)) {
|
||||
return $lock['locked'] !== false;
|
||||
} else {
|
||||
return false;
|
|
@ -1,19 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Lock Driver Interface
|
||||
* Lock Interface
|
||||
*
|
||||
* @author Philipp Holzer <admin@philipp.info>
|
||||
*/
|
||||
interface ILockDriver
|
||||
interface ILock
|
||||
{
|
||||
/**
|
||||
* Checks, if a key is currently locked to a or my process
|
||||
*
|
||||
* @param string $key The name of the lock
|
||||
* @param string $key The name of the lock
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLocked($key);
|
||||
|
@ -22,13 +24,13 @@ interface ILockDriver
|
|||
*
|
||||
* Acquires a lock for a given name
|
||||
*
|
||||
* @param string $key The Name of the lock
|
||||
* @param integer $timeout Seconds until we give up
|
||||
* @param integer $ttl Seconds The lock lifespan, must be one of the Cache constants
|
||||
* @param string $key The Name of the lock
|
||||
* @param integer $timeout Seconds until we give up
|
||||
* @param integer $ttl Seconds The lock lifespan, must be one of the Cache constants
|
||||
*
|
||||
* @return boolean Was the lock successful?
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES);
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* Releases a lock if it was set by us
|
|
@ -1,16 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Lock;
|
||||
use Friendica\BaseObject;
|
||||
|
||||
/**
|
||||
* Class AbstractLockDriver
|
||||
* Class AbstractLock
|
||||
*
|
||||
* @package Friendica\Core\Lock
|
||||
*
|
||||
* Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
|
||||
*/
|
||||
abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
||||
abstract class Lock implements ILock
|
||||
{
|
||||
/**
|
||||
* @var array The local acquired locks
|
||||
|
@ -21,6 +20,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
|
|||
* Check if we've locally acquired a lock
|
||||
*
|
||||
* @param string key The Name of the lock
|
||||
*
|
||||
* @return bool Returns true if the lock is set
|
||||
*/
|
||||
protected function hasAcquiredLock($key)
|
|
@ -4,7 +4,7 @@ namespace Friendica\Core\Lock;
|
|||
|
||||
use Friendica\Core\Cache;
|
||||
|
||||
class SemaphoreLockDriver extends AbstractLockDriver
|
||||
class SemaphoreLock extends Lock
|
||||
{
|
||||
private static $semaphore = [];
|
||||
|
|
@ -6,6 +6,7 @@ namespace Friendica\Core;
|
|||
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Util\BaseURL;
|
||||
use Friendica\Util\XML;
|
||||
|
||||
/**
|
||||
|
@ -29,7 +30,7 @@ class System extends BaseObject
|
|||
*/
|
||||
public static function baseUrl($ssl = false)
|
||||
{
|
||||
return self::getApp()->getBaseURL($ssl);
|
||||
return self::getClass(BaseURL::class)->get($ssl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache\ICacheDriver;
|
||||
use Friendica\Core\Config;
|
||||
|
||||
/**
|
||||
* Class CacheDriverFactory
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*
|
||||
* A basic class to generate a CacheDriver
|
||||
*/
|
||||
class CacheDriverFactory
|
||||
{
|
||||
/**
|
||||
* This method creates a CacheDriver for the given cache driver name
|
||||
*
|
||||
* @param string $driver The name of the cache driver
|
||||
* @return ICacheDriver The instance of the CacheDriver
|
||||
* @throws \Exception The exception if something went wrong during the CacheDriver creation
|
||||
*/
|
||||
public static function create($driver) {
|
||||
|
||||
switch ($driver) {
|
||||
case 'memcache':
|
||||
$memcache_host = Config::get('system', 'memcache_host');
|
||||
$memcache_port = Config::get('system', 'memcache_port');
|
||||
|
||||
return new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
|
||||
break;
|
||||
|
||||
case 'memcached':
|
||||
$memcached_hosts = Config::get('system', 'memcached_hosts');
|
||||
|
||||
return new Cache\MemcachedCacheDriver($memcached_hosts);
|
||||
break;
|
||||
case 'redis':
|
||||
$redis_host = Config::get('system', 'redis_host');
|
||||
$redis_port = Config::get('system', 'redis_port');
|
||||
$redis_pw = Config::get('system', 'redis_password');
|
||||
$redis_db = Config::get('system', 'redis_db', 0);
|
||||
|
||||
return new Cache\RedisCacheDriver($redis_host, $redis_port, $redis_db, $redis_pw);
|
||||
break;
|
||||
|
||||
case 'apcu':
|
||||
return new Cache\APCuCache();
|
||||
break;
|
||||
|
||||
default:
|
||||
return new Cache\DatabaseCacheDriver();
|
||||
}
|
||||
}
|
||||
}
|
101
src/Factory/CacheFactory.php
Normal file
101
src/Factory/CacheFactory.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Core\Cache\ICache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\BaseURL;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class CacheFactory
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*
|
||||
* A basic class to generate a CacheDriver
|
||||
*/
|
||||
class CacheFactory
|
||||
{
|
||||
/**
|
||||
* @var string The default cache if nothing set
|
||||
*/
|
||||
const DEFAULT_TYPE = Cache\Cache::TYPE_DATABASE;
|
||||
|
||||
/**
|
||||
* @var Configuration The configuration to read parameters out of the config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Database The database connection in case that the cache is used the dba connection
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @var string The hostname, used as Prefix for Caching
|
||||
*/
|
||||
private $hostname;
|
||||
|
||||
/**
|
||||
* @var Profiler The optional profiler if the cached should be profiled
|
||||
*/
|
||||
private $profiler;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface The Friendica Logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(BaseURL $baseURL, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
||||
{
|
||||
$this->hostname = $baseURL->getHostname();
|
||||
$this->config = $config;
|
||||
$this->dba = $dba;
|
||||
$this->profiler = $profiler;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method creates a CacheDriver for the given cache driver name
|
||||
*
|
||||
* @param string $type The cache type to create (default is per config)
|
||||
*
|
||||
* @return ICache The instance of the CacheDriver
|
||||
* @throws \Exception The exception if something went wrong during the CacheDriver creation
|
||||
*/
|
||||
public function create(string $type = null)
|
||||
{
|
||||
if (empty($type)) {
|
||||
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
|
||||
}
|
||||
|
||||
switch ($type) {
|
||||
case Cache\Cache::TYPE_MEMCACHE:
|
||||
$cache = new Cache\MemcacheCache($this->hostname, $this->config);
|
||||
break;
|
||||
case Cache\Cache::TYPE_MEMCACHED:
|
||||
$cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
|
||||
break;
|
||||
case Cache\Cache::TYPE_REDIS:
|
||||
$cache = new Cache\RedisCache($this->hostname, $this->config);
|
||||
break;
|
||||
case Cache\Cache::TYPE_APCU:
|
||||
$cache = new Cache\APCuCache($this->hostname);
|
||||
break;
|
||||
default:
|
||||
$cache = new Cache\DatabaseCache($this->hostname, $this->dba);
|
||||
}
|
||||
|
||||
$profiling = $this->config->get('system', 'profiling', false);
|
||||
|
||||
// In case profiling is enabled, wrap the ProfilerCache around the current cache
|
||||
if (isset($profiling) && $profiling !== false) {
|
||||
return new Cache\ProfilerCache($cache, $this->profiler);
|
||||
} else {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
}
|
132
src/Factory/LockFactory.php
Normal file
132
src/Factory/LockFactory.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Friendica\Core\Cache\Cache;
|
||||
use Friendica\Core\Cache\IMemoryCache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Lock;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class LockFactory
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*
|
||||
* A basic class to generate a LockDriver
|
||||
*/
|
||||
class LockFactory
|
||||
{
|
||||
/**
|
||||
* @var string The default driver for caching
|
||||
*/
|
||||
const DEFAULT_DRIVER = 'default';
|
||||
|
||||
/**
|
||||
* @var Configuration The configuration to read parameters out of the config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Database The database connection in case that the cache is used the dba connection
|
||||
*/
|
||||
private $dba;
|
||||
|
||||
/**
|
||||
* @var CacheFactory The memory cache driver in case we use it
|
||||
*/
|
||||
private $cacheFactory;
|
||||
|
||||
/**
|
||||
* @var Profiler The optional profiler if the cached should be profiled
|
||||
*/
|
||||
private $profiler;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface The Friendica Logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(CacheFactory $cacheFactory, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
||||
{
|
||||
$this->cacheFactory = $cacheFactory;
|
||||
$this->config = $config;
|
||||
$this->dba = $dba;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$lock_type = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
|
||||
|
||||
try {
|
||||
switch ($lock_type) {
|
||||
case Cache::TYPE_MEMCACHE:
|
||||
case Cache::TYPE_MEMCACHED:
|
||||
case Cache::TYPE_REDIS:
|
||||
case Cache::TYPE_APCU:
|
||||
$cache = $this->cacheFactory->create($lock_type);
|
||||
if ($cache instanceof IMemoryCache) {
|
||||
return new Lock\CacheLock($cache);
|
||||
} else {
|
||||
throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'database':
|
||||
return new Lock\DatabaseLock($this->dba);
|
||||
break;
|
||||
|
||||
case 'semaphore':
|
||||
return new Lock\SemaphoreLock();
|
||||
break;
|
||||
|
||||
default:
|
||||
return self::useAutoDriver();
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->alert('Driver \'' . $lock_type . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
|
||||
return self::useAutoDriver();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This method tries to find the best - local - locking method for Friendica
|
||||
*
|
||||
* The following sequence will be tried:
|
||||
* 1. Semaphore Locking
|
||||
* 2. Cache Locking
|
||||
* 3. Database Locking
|
||||
*
|
||||
* @return Lock\ILock
|
||||
*/
|
||||
private function useAutoDriver()
|
||||
{
|
||||
// 1. Try to use Semaphores for - local - locking
|
||||
if (function_exists('sem_get')) {
|
||||
try {
|
||||
return new Lock\SemaphoreLock();
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->debug('Using Semaphore driver for locking failed.', ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
|
||||
$cache_type = $this->config->get('system', 'cache_driver', 'database');
|
||||
if ($cache_type != Cache::TYPE_DATABASE) {
|
||||
try {
|
||||
$cache = $this->cacheFactory->create($cache_type);
|
||||
if ($cache instanceof IMemoryCache) {
|
||||
return new Lock\CacheLock($cache);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->debug('Using Cache driver for locking failed.', ['exception' => $exception]);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Use Database Locking as a Fallback
|
||||
return new Lock\DatabaseLock($this->dba);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue