Refactor Cache/Lock to DICE

- Refactor Cache classes
- Refactor Lock classes
- Improved test speed (removed some seperate class annotations)
This commit is contained in:
Philipp Holzer 2019-08-03 20:48:56 +02:00
parent b95d4f41b9
commit d56bd28a07
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
40 changed files with 766 additions and 621 deletions

View file

@ -18,11 +18,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);
}
/**

View file

@ -1,8 +1,6 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
/**
* Abstract class for common used functions
@ -11,8 +9,18 @@ use Friendica\BaseObject;
*
* @package Friendica\Core\Cache
*/
abstract class AbstractCacheDriver extends BaseObject
abstract class AbstractCacheDriver implements ICacheDriver
{
/**
* @var string The hostname
*/
private $hostName;
public function __construct(string $hostName)
{
$this->hostName = $hostName;
}
/**
* Returns the prefix (to avoid namespace conflicts)
*
@ -22,7 +30,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 +54,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 +87,5 @@ abstract class AbstractCacheDriver extends BaseObject
return $result;
}
}
}

View file

@ -2,7 +2,6 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**

View file

@ -3,7 +3,7 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
use Friendica\Database\DBA;
use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;
/**
@ -13,6 +13,18 @@ use Friendica\Util\DateTimeFormat;
*/
class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
{
/**
* @var Database
*/
private $dba;
public function __construct(string $hostname, Database $dba)
{
parent::__construct($hostname);
$this->dba = $dba;
}
/**
* (@inheritdoc)
*/
@ -24,13 +36,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 +52,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 +88,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 +96,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 +105,9 @@ 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 ']);
}
}
}

View file

@ -2,8 +2,6 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Cache Driver Interface
*
@ -11,6 +9,16 @@ use Friendica\Core\Cache;
*/
interface ICacheDriver
{
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;
/**
* Lists all cache keys
*
@ -38,7 +46,7 @@ interface ICacheDriver
*
* @return bool
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
public function set($key, $value, $ttl = self::FIVE_MINUTES);
/**
* Delete a key from the cache

View file

@ -1,7 +1,6 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* This interface defines methods for Memory-Caches only
@ -20,7 +19,7 @@ interface IMemoryCacheDriver extends ICacheDriver
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @return bool
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
public function add($key, $value, $ttl = ICacheDriver::FIVE_MINUTES);
/**
* Compares if the old value is set and sets the new value
@ -32,7 +31,7 @@ interface IMemoryCacheDriver extends ICacheDriver
*
* @return bool
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES);
public function compareSet($key, $oldValue, $newValue, $ttl = ICacheDriver::FIVE_MINUTES);
/**
* Compares if the old value is set and removes it

View file

@ -2,9 +2,9 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration;
use Memcache;
/**
@ -23,18 +23,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');
}

View file

@ -2,11 +2,11 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
use Friendica\Core\Logger;
use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration;
use Memcached;
use Psr\Log\LoggerInterface;
/**
* Memcached Cache Driver
@ -23,6 +23,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 {
@ -33,14 +38,20 @@ 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 [];
}
}
@ -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;

View file

@ -0,0 +1,155 @@
<?php
namespace Friendica\Core\Cache;
use 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 ICacheDriver, IMemoryCacheDriver
{
/**
* @var ICacheDriver The original cache driver
*/
private $cache;
/**
* @var Profiler The profiler of Friendica
*/
private $profiler;
public function __construct(ICacheDriver $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 IMemoryCacheDriver) {
$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 IMemoryCacheDriver) {
$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 IMemoryCacheDriver) {
$time = microtime(true);
$return = $this->cache->compareDelete($key, $value);
$this->profiler->saveTimestamp($time, 'cache', System::callstack());
return $return;
} else {
return false;
}
}
}

View file

@ -4,6 +4,7 @@ namespace Friendica\Core\Cache;
use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration;
use Redis;
/**
@ -20,20 +21,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');
}