From d56bd28a0762bb5ee293e2e944bdee5df9d24692 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sat, 3 Aug 2019 20:48:56 +0200 Subject: [PATCH 01/12] Refactor Cache/Lock to DICE - Refactor Cache classes - Refactor Lock classes - Improved test speed (removed some seperate class annotations) --- src/Console/Cache.php | 40 +++-- src/Core/Cache.php | 81 +++------ src/Core/Cache/APCuCache.php | 4 +- src/Core/Cache/AbstractCacheDriver.php | 19 ++- src/Core/Cache/ArrayCache.php | 1 - src/Core/Cache/DatabaseCacheDriver.php | 32 ++-- src/Core/Cache/ICacheDriver.php | 14 +- src/Core/Cache/IMemoryCacheDriver.php | 5 +- src/Core/Cache/MemcacheCacheDriver.php | 13 +- src/Core/Cache/MemcachedCacheDriver.php | 23 ++- src/Core/Cache/ProfilerCache.php | 155 ++++++++++++++++++ src/Core/Cache/RedisCacheDriver.php | 14 +- src/Core/Lock.php | 115 ++----------- src/Core/Lock/AbstractLockDriver.php | 4 +- src/Core/Lock/CacheLockDriver.php | 14 +- src/Core/Lock/DatabaseLockDriver.php | 32 ++-- src/Core/Lock/ILockDriver.php | 12 +- src/Factory/CacheDriverFactory.php | 82 ++++++--- src/Factory/LockDriverFactory.php | 128 +++++++++++++++ static/dependencies.config.php | 17 ++ tests/DatabaseTest.php | 48 +----- tests/DatabaseTestTrait.php | 58 +++++++ tests/Util/DbaCacheMockTrait.php | 10 +- tests/Util/VFSTrait.php | 1 + tests/src/Core/Cache/APCuCacheDriverTest.php | 2 +- tests/src/Core/Cache/ArrayCacheDriverTest.php | 2 +- tests/src/Core/Cache/CacheTest.php | 34 ++-- .../Core/Cache/DatabaseCacheDriverTest.php | 139 +++------------- .../Core/Cache/MemcacheCacheDriverTest.php | 14 +- .../Core/Cache/MemcachedCacheDriverTest.php | 12 +- tests/src/Core/Cache/MemoryCacheTest.php | 24 ++- tests/src/Core/Cache/RedisCacheDriverTest.php | 19 ++- .../src/Core/Lock/APCuCacheLockDriverTest.php | 3 +- .../Core/Lock/ArrayCacheLockDriverTest.php | 3 +- .../src/Core/Lock/DatabaseLockDriverTest.php | 113 +++---------- tests/src/Core/Lock/LockTest.php | 38 ++--- .../Core/Lock/MemcacheCacheLockDriverTest.php | 12 +- .../Lock/MemcachedCacheLockDriverTest.php | 12 +- .../Core/Lock/RedisCacheLockDriverTest.php | 19 ++- .../src/Core/Lock/SemaphoreLockDriverTest.php | 19 ++- 40 files changed, 766 insertions(+), 621 deletions(-) create mode 100644 src/Core/Cache/ProfilerCache.php create mode 100644 src/Factory/LockDriverFactory.php create mode 100644 tests/DatabaseTestTrait.php diff --git a/src/Console/Cache.php b/src/Console/Cache.php index e2e1dba6c0..7596ae6d51 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -4,7 +4,9 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; use Friendica\App; -use Friendica\Core; +use Friendica\Core\Cache\ICacheDriver; +use Friendica\Core\Config\Configuration; +use Friendica\Factory\CacheDriverFactory; use RuntimeException; /** @@ -25,6 +27,16 @@ class Cache extends \Asika\SimpleConsole\Console */ private $appMode; + /** + * @var string The cache driver name + */ + private $cacheDriverName; + + /** + * @var ICacheDriver + */ + private $cache; + protected function getHelp() { $help = <<appMode = $appMode; + $this->cache = $cache; + $this->cacheDriverName = $config->get('system', 'cache_driver', CacheDriverFactory::DEFAULT_DRIVER); } protected function doExecute() @@ -79,11 +93,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->cacheDriverName); + $this->out('Cache Driver Class: ' . get_class($this->cache)); } switch ($this->getArgument(0)) { @@ -115,7 +127,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:'); @@ -136,7 +148,7 @@ HELP; { if (count($this->args) >= 2) { $key = $this->getArgument(1); - $value = Core\Cache::get($key); + $value = $this->cache->get($key); $this->out("{$key} => " . var_export($value, true)); } else { @@ -149,15 +161,15 @@ HELP; if (count($this->args) >= 3) { $key = $this->getArgument(1); $value = $this->getArgument(2); - $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES)); + $duration = intval($this->getArgument(3, ICacheDriver::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 +180,7 @@ HELP; private function executeFlush() { - $result = Core\Cache::clear(); + $result = $this->cache->clear(); if ($result) { $this->out('Cache successfully flushed'); } else { @@ -178,7 +190,7 @@ HELP; private function executeClear() { - $result = Core\Cache::clear(false); + $result = $this->cache->clear(false); if ($result) { $this->out('Cache successfully cleared'); } else { diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 7a8f7367ec..0c5597aa5e 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -4,51 +4,33 @@ */ namespace Friendica\Core; -use Friendica\Factory\CacheDriverFactory; +use Friendica\BaseObject; +use Friendica\Core\Cache\ICacheDriver; /** * @brief Class for storing data for a short time */ -class Cache extends \Friendica\BaseObject +class Cache extends BaseObject { - const MONTH = 2592000; + /** @deprecated Use ICacheDriver::MONTH */ + const MONTH = ICacheDriver::MONTH; + /** @deprecated Use ICacheDriver::WEEK */ const WEEK = 604800; + /** @deprecated Use ICacheDriver::DAY */ const DAY = 86400; + /** @deprecated Use ICacheDriver::HOUR */ const HOUR = 3600; + /** @deprecated Use ICacheDriver::HALF_HOUR */ const HALF_HOUR = 1800; + /** @deprecated Use ICacheDriver::QUARTER_HOUR */ const QUARTER_HOUR = 900; + /** @deprecated Use ICacheDriver::FIVE_MINUTES */ const FIVE_MINUTES = 300; + /** @deprecated Use ICacheDriver::MINUTE */ const MINUTE = 60; + /** @deprecated Use ICacheDriver::INFINITE */ 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; - } - /** * @brief Returns all the cache keys sorted alphabetically * @@ -59,13 +41,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(ICacheDriver::class)->getAllKeys($prefix); } /** @@ -78,13 +54,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(ICacheDriver::class)->get($key); } /** @@ -99,15 +69,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 = ICacheDriver::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(ICacheDriver::class)->set($key, $value, $duration); } /** @@ -120,13 +84,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(ICacheDriver::class)->delete($key); } /** @@ -135,9 +93,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(ICacheDriver::class)->clear($outdated); } } diff --git a/src/Core/Cache/APCuCache.php b/src/Core/Cache/APCuCache.php index f658424cdc..b89d7fe4b3 100644 --- a/src/Core/Cache/APCuCache.php +++ b/src/Core/Cache/APCuCache.php @@ -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); } /** diff --git a/src/Core/Cache/AbstractCacheDriver.php b/src/Core/Cache/AbstractCacheDriver.php index f238a7819c..e12f63bfa4 100644 --- a/src/Core/Cache/AbstractCacheDriver.php +++ b/src/Core/Cache/AbstractCacheDriver.php @@ -1,8 +1,6 @@ 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; } - } } diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index a99b05788f..f2f37708de 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -2,7 +2,6 @@ namespace Friendica\Core\Cache; - use Friendica\Core\Cache; /** diff --git a/src/Core/Cache/DatabaseCacheDriver.php b/src/Core/Cache/DatabaseCacheDriver.php index f6f5b6486c..f1d6150701 100644 --- a/src/Core/Cache/DatabaseCacheDriver.php +++ b/src/Core/Cache/DatabaseCacheDriver.php @@ -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 ']); } } } diff --git a/src/Core/Cache/ICacheDriver.php b/src/Core/Cache/ICacheDriver.php index 1188e51877..34b741968a 100644 --- a/src/Core/Cache/ICacheDriver.php +++ b/src/Core/Cache/ICacheDriver.php @@ -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 diff --git a/src/Core/Cache/IMemoryCacheDriver.php b/src/Core/Cache/IMemoryCacheDriver.php index 0c5146f439..e35159a3a6 100644 --- a/src/Core/Cache/IMemoryCacheDriver.php +++ b/src/Core/Cache/IMemoryCacheDriver.php @@ -1,7 +1,6 @@ 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'); } diff --git a/src/Core/Cache/MemcachedCacheDriver.php b/src/Core/Cache/MemcachedCacheDriver.php index 687e67416a..7b3a2d5dfa 100644 --- a/src/Core/Cache/MemcachedCacheDriver.php +++ b/src/Core/Cache/MemcachedCacheDriver.php @@ -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; diff --git a/src/Core/Cache/ProfilerCache.php b/src/Core/Cache/ProfilerCache.php new file mode 100644 index 0000000000..04271e7c69 --- /dev/null +++ b/src/Core/Cache/ProfilerCache.php @@ -0,0 +1,155 @@ +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; + } + } +} diff --git a/src/Core/Cache/RedisCacheDriver.php b/src/Core/Cache/RedisCacheDriver.php index ea4eb4390a..951f51dc13 100644 --- a/src/Core/Cache/RedisCacheDriver.php +++ b/src/Core/Cache/RedisCacheDriver.php @@ -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'); } diff --git a/src/Core/Lock.php b/src/Core/Lock.php index a45490bf39..04dfaa1c5e 100644 --- a/src/Core/Lock.php +++ b/src/Core/Lock.php @@ -7,116 +7,28 @@ namespace Friendica\Core; -use Friendica\Factory\CacheDriverFactory; -use Friendica\Core\Cache\IMemoryCacheDriver; +use Friendica\BaseObject; +use Friendica\Core\Cache\ICacheDriver; +use Friendica\Core\Lock\ILockDriver; /** - * @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) + public static function acquire($key, $timeout = 120, $ttl = ICacheDriver::FIVE_MINUTES) { - return self::getDriver()->acquireLock($key, $timeout, $ttl); + return self::getClass(ILockDriver::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(ILockDriver::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(ILockDriver::class)->releaseAll(); } } diff --git a/src/Core/Lock/AbstractLockDriver.php b/src/Core/Lock/AbstractLockDriver.php index 0aedeeb1b0..71ad08d088 100644 --- a/src/Core/Lock/AbstractLockDriver.php +++ b/src/Core/Lock/AbstractLockDriver.php @@ -1,7 +1,6 @@ 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; } } diff --git a/src/Core/Lock/DatabaseLockDriver.php b/src/Core/Lock/DatabaseLockDriver.php index a8269bc92e..e9e0500bb4 100644 --- a/src/Core/Lock/DatabaseLockDriver.php +++ b/src/Core/Lock/DatabaseLockDriver.php @@ -3,7 +3,7 @@ namespace Friendica\Core\Lock; use Friendica\Core\Cache; -use Friendica\Database\DBA; +use Friendica\Database\Database; use Friendica\Util\DateTimeFormat; /** @@ -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; diff --git a/src/Core/Lock/ILockDriver.php b/src/Core/Lock/ILockDriver.php index 7df5b3f87a..43793d6287 100644 --- a/src/Core/Lock/ILockDriver.php +++ b/src/Core/Lock/ILockDriver.php @@ -1,6 +1,7 @@ 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 $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) { + public function create() + { + $driver = $this->config->get('system', 'cache_driver', self::DEFAULT_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); + $cache = new Cache\MemcacheCacheDriver($this->hostname, $this->config); break; - case 'memcached': - $memcached_hosts = Config::get('system', 'memcached_hosts'); - - return new Cache\MemcachedCacheDriver($memcached_hosts); + $cache = new Cache\MemcachedCacheDriver($this->hostname, $this->config, $this->logger); 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); + $cache = new Cache\RedisCacheDriver($this->hostname, $this->config); break; - case 'apcu': - return new Cache\APCuCache(); + $cache = new Cache\APCuCache($this->hostname); break; - default: - return new Cache\DatabaseCacheDriver(); + $cache = new Cache\DatabaseCacheDriver($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; } } } diff --git a/src/Factory/LockDriverFactory.php b/src/Factory/LockDriverFactory.php new file mode 100644 index 0000000000..d31728be6c --- /dev/null +++ b/src/Factory/LockDriverFactory.php @@ -0,0 +1,128 @@ +cacheDriver = $cacheDriver; + $this->config = $config; + $this->dba = $dba; + $this->logger = $logger; + } + + public function create() + { + $lock_driver = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER); + + try { + switch ($lock_driver) { + case 'memcache': + case 'memcached': + case 'redis': + if ($this->cacheDriver instanceof IMemoryCacheDriver) { + return new Lock\CacheLockDriver($this->cacheDriver); + } + break; + + case 'database': + return new Lock\DatabaseLockDriver($this->dba); + break; + + case 'semaphore': + return new Lock\SemaphoreLockDriver(); + break; + + default: + return self::useAutoDriver(); + } + } catch (\Exception $exception) { + $this->logger->alert('Driver \'' . $lock_driver . '\' 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\ILockDriver + */ + private function useAutoDriver() + { + + // 1. Try to use Semaphores for - local - locking + if (function_exists('sem_get')) { + try { + return new Lock\SemaphoreLockDriver(); + } 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_driver = $this->config->get('system', 'cache_driver', 'database'); + if ($cache_driver != 'database') { + try { + if ($this->cacheDriver instanceof IMemoryCacheDriver) { + return new Lock\CacheLockDriver($this->cacheDriver); + } + } catch (\Exception $exception) { + $this->logger->debug('Using Cache driver for locking failed.', ['exception' => $exception]); + } + } + + // 3. Use Database Locking as a Fallback + return new Lock\DatabaseLockDriver($this->dba); + } +} diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 475f5cceed..55a2c6845b 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -2,7 +2,9 @@ use Dice\Dice; use Friendica\App; +use Friendica\Core\Cache; use Friendica\Core\Config; +use Friendica\Core\Lock\ILockDriver; use Friendica\Database\Database; use Friendica\Factory; use Friendica\Util; @@ -116,4 +118,19 @@ return [ ['createDev', [], Dice::CHAIN_CALL], ] ], + Cache\ICacheDriver::class => [ + 'instanceOf' => Factory\CacheDriverFactory::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], + ], + Cache\IMemoryCacheDriver::class => [ + 'instanceOf' => Cache\ICacheDriver::class, + ], + ILockDriver::class => [ + 'instanceOf' => Factory\LockDriverFactory::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], + ], ]; diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 4f0275493c..7421f16a09 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -5,56 +5,10 @@ namespace Friendica\Test; -use Friendica\Database\Database; -use Friendica\Test\Util\Database\StaticDatabase; - /** * Abstract class used by tests that need a database. */ abstract class DatabaseTest extends MockedTest { - protected function setUp() - { - parent::setUp(); - - StaticDatabase::statConnect($_SERVER); - // Rollbacks every DB usage (in case the test couldn't call tearDown) - StaticDatabase::statRollback(); - // Start the first, outer transaction - StaticDatabase::getGlobConnection()->beginTransaction(); - } - - protected function tearDown() - { - // Rollbacks every DB usage so we don't commit anything into the DB - StaticDatabase::statRollback(); - - parent::tearDown(); - } - - /** - * Loads a given DB fixture for this DB test - * - * @param string $fixture The path to the fixture - * @param Database $dba The DB connection - * - * @throws \Exception - */ - protected function loadFixture(string $fixture, Database $dba) - { - $this->assertFileExists($fixture); - - $data = include $fixture; - - foreach ($data as $tableName => $rows) { - if (!is_array($rows)) { - $dba->p('TRUNCATE TABLE `' . $tableName . '``'); - continue; - } - - foreach ($rows as $row) { - $dba->insert($tableName, $row); - } - } - } + use DatabaseTestTrait; } diff --git a/tests/DatabaseTestTrait.php b/tests/DatabaseTestTrait.php new file mode 100644 index 0000000000..49dc999b69 --- /dev/null +++ b/tests/DatabaseTestTrait.php @@ -0,0 +1,58 @@ +beginTransaction(); + + parent::setUp(); + } + + protected function tearDown() + { + // Rollbacks every DB usage so we don't commit anything into the DB + StaticDatabase::statRollback(); + + parent::tearDown(); + } + + /** + * Loads a given DB fixture for this DB test + * + * @param string $fixture The path to the fixture + * @param Database $dba The DB connection + * + * @throws \Exception + */ + protected function loadFixture(string $fixture, Database $dba) + { + $data = include $fixture; + + foreach ($data as $tableName => $rows) { + if (!is_array($rows)) { + $dba->p('TRUNCATE TABLE `' . $tableName . '``'); + continue; + } + + foreach ($rows as $row) { + $dba->insert($tableName, $row); + } + } + } +} diff --git a/tests/Util/DbaCacheMockTrait.php b/tests/Util/DbaCacheMockTrait.php index 87ab450c9e..95e7cbcb1d 100644 --- a/tests/Util/DbaCacheMockTrait.php +++ b/tests/Util/DbaCacheMockTrait.php @@ -4,8 +4,14 @@ namespace Friendica\Test\Util; trait DbaCacheMockTrait { - use DBAMockTrait; - use DateTimeFormatMockTrait; + /** + * @var + */ + protected $dba; + + public function __construct() + { + } protected function mockDelete($key, $return = true, $times = null) { diff --git a/tests/Util/VFSTrait.php b/tests/Util/VFSTrait.php index 110f24a61a..565e693c95 100644 --- a/tests/Util/VFSTrait.php +++ b/tests/Util/VFSTrait.php @@ -28,6 +28,7 @@ trait VFSTrait // create a virtual directory and copy all needed files and folders to it $this->root = vfsStream::setup('friendica', 0777, $structure); + $this->setConfigFile('dbstructure.config.php', true); $this->setConfigFile('defaults.config.php', true); $this->setConfigFile('settings.config.php', true); $this->setConfigFile('local.config.php'); diff --git a/tests/src/Core/Cache/APCuCacheDriverTest.php b/tests/src/Core/Cache/APCuCacheDriverTest.php index eac00f559a..9925b02352 100644 --- a/tests/src/Core/Cache/APCuCacheDriverTest.php +++ b/tests/src/Core/Cache/APCuCacheDriverTest.php @@ -17,7 +17,7 @@ class APCuCacheDriverTest extends MemoryCacheTest protected function getInstance() { - $this->cache = new APCuCache(); + $this->cache = new APCuCache('localhost'); return $this->cache; } diff --git a/tests/src/Core/Cache/ArrayCacheDriverTest.php b/tests/src/Core/Cache/ArrayCacheDriverTest.php index c92fb98dac..68b8d1dba1 100644 --- a/tests/src/Core/Cache/ArrayCacheDriverTest.php +++ b/tests/src/Core/Cache/ArrayCacheDriverTest.php @@ -8,7 +8,7 @@ class ArrayCacheDriverTest extends MemoryCacheTest { protected function getInstance() { - $this->cache = new ArrayCache(); + $this->cache = new ArrayCache('localhost'); return $this->cache; } diff --git a/tests/src/Core/Cache/CacheTest.php b/tests/src/Core/Cache/CacheTest.php index ef97f5a172..088715cd79 100644 --- a/tests/src/Core/Cache/CacheTest.php +++ b/tests/src/Core/Cache/CacheTest.php @@ -4,15 +4,10 @@ namespace Friendica\Test\src\Core\Cache; use Friendica\Core\Cache\MemcachedCacheDriver; use Friendica\Test\MockedTest; -use Friendica\Test\Util\AppMockTrait; -use Friendica\Test\Util\VFSTrait; use Friendica\Util\PidFile; abstract class CacheTest extends MockedTest { - use VFSTrait; - use AppMockTrait; - /** * @var int Start time of the mock (used for time operations) */ @@ -30,6 +25,7 @@ abstract class CacheTest extends MockedTest /** * Dataset for test setting different types in the cache + * * @return array */ public function dataTypesInCache() @@ -48,6 +44,7 @@ abstract class CacheTest extends MockedTest /** * Dataset for simple value sets/gets + * * @return array */ public function dataSimple() @@ -66,12 +63,6 @@ abstract class CacheTest extends MockedTest protected function setUp() { - $this->setUpVfsDir(); - $this->mockApp($this->root); - $this->app - ->shouldReceive('getHostname') - ->andReturn('friendica.local'); - parent::setUp(); $this->instance = $this->getInstance(); @@ -82,10 +73,12 @@ abstract class CacheTest extends MockedTest /** * @small * @dataProvider dataSimple + * * @param mixed $value1 a first * @param mixed $value2 a second */ - function testSimple($value1, $value2) { + function testSimple($value1, $value2) + { $this->assertNull($this->instance->get('value1')); $this->instance->set('value1', $value1); @@ -110,12 +103,14 @@ abstract class CacheTest extends MockedTest /** * @small * @dataProvider dataSimple + * * @param mixed $value1 a first * @param mixed $value2 a second * @param mixed $value3 a third * @param mixed $value4 a fourth */ - function testClear($value1, $value2, $value3, $value4) { + function testClear($value1, $value2, $value3, $value4) + { $value = 'ipsum lorum'; $this->instance->set('1_value1', $value1); $this->instance->set('1_value2', $value2); @@ -166,7 +161,8 @@ abstract class CacheTest extends MockedTest /** * @medium */ - function testTTL() { + function testTTL() + { $this->markTestSkipped('taking too much time without mocking'); $this->assertNull($this->instance->get('value1')); @@ -183,10 +179,13 @@ abstract class CacheTest extends MockedTest /** * @small + * * @param $data mixed the data to store in the cache + * * @dataProvider dataTypesInCache */ - function testDifferentTypesInCache($data) { + function testDifferentTypesInCache($data) + { $this->instance->set('val', $data); $received = $this->instance->get('val'); $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received)); @@ -194,12 +193,15 @@ abstract class CacheTest extends MockedTest /** * @small + * * @param mixed $value1 a first * @param mixed $value2 a second * @param mixed $value3 a third + * * @dataProvider dataSimple */ - public function testGetAllKeys($value1, $value2, $value3) { + public function testGetAllKeys($value1, $value2, $value3) + { if ($this->cache instanceof MemcachedCacheDriver) { $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore'); } diff --git a/tests/src/Core/Cache/DatabaseCacheDriverTest.php b/tests/src/Core/Cache/DatabaseCacheDriverTest.php index 2d29c2ad96..1aa9bf02b8 100644 --- a/tests/src/Core/Cache/DatabaseCacheDriverTest.php +++ b/tests/src/Core/Cache/DatabaseCacheDriverTest.php @@ -3,33 +3,40 @@ namespace Friendica\Test\src\Core\Cache; use Friendica\Core\Cache; -use Friendica\Factory\CacheDriverFactory; -use Friendica\Test\Util\DbaCacheMockTrait; +use Friendica\Factory\ConfigFactory; +use Friendica\Test\DatabaseTestTrait; +use Friendica\Test\Util\Database\StaticDatabase; +use Friendica\Test\Util\VFSTrait; +use Friendica\Util\ConfigFileLoader; +use Friendica\Util\Profiler; +use Psr\Log\NullLogger; -/** - * @runTestsInSeparateProcesses - * @preserveGlobalState disabled - */ class DatabaseCacheDriverTest extends CacheTest { - use DbaCacheMockTrait; + use DatabaseTestTrait; + use VFSTrait; - public function setUp() + protected function setUp() { - $this->mockUtcNow($this->startTime); - - $this->mockConnected(); - $this->mockConnect(); - - // The first "clear" at setup - $this->mockClear(false, true, 2); + $this->setUpVfsDir(); parent::setUp(); } protected function getInstance() { - $this->cache = CacheDriverFactory::create('database'); + $logger = new NullLogger(); + $profiler = \Mockery::mock(Profiler::class); + $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true); + + // load real config to avoid mocking every config-entry which is related to the Database class + $configFactory = new ConfigFactory(); + $loader = new ConfigFileLoader($this->root->url()); + $configCache = $configFactory->createCache($loader); + + $dba = new StaticDatabase($configCache, $profiler, $logger); + + $this->cache = new Cache\DatabaseCacheDriver('database', $dba); return $this->cache; } @@ -38,104 +45,4 @@ class DatabaseCacheDriverTest extends CacheTest $this->cache->clear(false); parent::tearDown(); } - - /** - * {@inheritdoc} - * @dataProvider dataSimple - */ - public function testSimple($value1, $value2) - { - // assertNull - $this->mockGet('value1', null, $this->startTime, 1); - - // assertEquals - $this->mockSet('value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockGet('value1', $value1, $this->startTime, 1); - - // assertEquals - $this->mockSet('value1', $value2, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockGet('value1', $value2, $this->startTime, 1); - - // assertEquals - $this->mockSet('value2', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockGet('value2', $value1, $this->startTime, 1); - - // assertNull - $this->mockGet('not_set', null, $this->startTime, 1); - - // assertNull - $this->mockDelete('value1', true, 1); - $this->mockGet('value1', null, $this->startTime, 1); - - parent::testSimple($value1, $value2); - } - - /** - * {@inheritdoc} - * @dataProvider dataSimple - */ - public function testClear($value1, $value2, $value3, $value4) - { - // assert Equals - $this->mockSet('1_value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockSet('1_value2', $value2, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockSet('2_value1', $value3, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockSet('3_value1', $value4, Cache::FIVE_MINUTES, $this->startTime, true, 1); - - $this->mockGet('1_value1', $value1, $this->startTime, 2); - $this->mockGet('1_value2', $value2, $this->startTime, 2); - $this->mockGet('2_value1', $value3, $this->startTime, 2); - $this->mockGet('3_value1', $value4, $this->startTime, 2); - - // assertTrue - $this->mockClear(true, true, 1); - $this->mockClear(false, true, 1); - - // assertEquals - $this->mockGet('1_value1', null, $this->startTime, 1); - $this->mockGet('1_value2', null, $this->startTime, 1); - $this->mockGet('2_value3', null, $this->startTime, 1); - $this->mockGet('3_value4', null, $this->startTime, 1); - - parent::testClear($value1, $value2, $value3, $value4); - } - - /** - * {@inheritdoc} - * @dataProvider dataTypesInCache - */ - public function testDifferentTypesInCache($data) - { - $this->mockSet('val', $data, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockGet('val', $data, $this->startTime, 1); - - parent::testDifferentTypesInCache($data); - } - - /** - * {@inheritdoc} - * @dataProvider dataSimple - */ - public function testGetAllKeys($value1, $value2, $value3) - { - $this->mockSet('value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockSet('value2', $value2,Cache::FIVE_MINUTES, $this->startTime, true, 1); - $this->mockSet('test_value3', $value3, Cache::FIVE_MINUTES, $this->startTime, true, 1); - - $result = [ - ['k' => 'value1'], - ['k' => 'value2'], - ['k' => 'test_value3'], - ]; - - $this->mockGetAllKeys(null, $result, $this->startTime, 1); - - $result = [ - ['k' => 'test_value3'], - ]; - - $this->mockGetAllKeys('test', $result, $this->startTime, 1); - - parent::testGetAllKeys($value1, $value2, $value3); - } } diff --git a/tests/src/Core/Cache/MemcacheCacheDriverTest.php b/tests/src/Core/Cache/MemcacheCacheDriverTest.php index f8de88ac95..31aa0b4852 100644 --- a/tests/src/Core/Cache/MemcacheCacheDriverTest.php +++ b/tests/src/Core/Cache/MemcacheCacheDriverTest.php @@ -1,9 +1,9 @@ configMock + $configMock = \Mockery::mock(Configuration::class); + + $configMock ->shouldReceive('get') ->with('system', 'memcache_host') ->andReturn('localhost'); - - $this->configMock + $configMock ->shouldReceive('get') ->with('system', 'memcache_port') ->andReturn(11211); - $this->cache = CacheDriverFactory::create('memcache'); + $this->cache = new MemcacheCacheDriver('localhost', $configMock); return $this->cache; - } public function tearDown() diff --git a/tests/src/Core/Cache/MemcachedCacheDriverTest.php b/tests/src/Core/Cache/MemcachedCacheDriverTest.php index 9f0ed8d4fe..ade3886841 100644 --- a/tests/src/Core/Cache/MemcachedCacheDriverTest.php +++ b/tests/src/Core/Cache/MemcachedCacheDriverTest.php @@ -3,7 +3,9 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Factory\CacheDriverFactory; +use Friendica\Core\Cache\MemcachedCacheDriver; +use Friendica\Core\Config\Configuration; +use Psr\Log\NullLogger; /** * @requires extension memcached @@ -12,12 +14,16 @@ class MemcachedCacheDriverTest extends MemoryCacheTest { protected function getInstance() { - $this->configMock + $configMock = \Mockery::mock(Configuration::class); + + $configMock ->shouldReceive('get') ->with('system', 'memcached_hosts') ->andReturn([0 => 'localhost, 11211']); - $this->cache = CacheDriverFactory::create('memcached'); + $logger = new NullLogger(); + + $this->cache = new MemcachedCacheDriver('localhost', $configMock, $logger); return $this->cache; } diff --git a/tests/src/Core/Cache/MemoryCacheTest.php b/tests/src/Core/Cache/MemoryCacheTest.php index 6688153b05..0bd7617dd0 100644 --- a/tests/src/Core/Cache/MemoryCacheTest.php +++ b/tests/src/Core/Cache/MemoryCacheTest.php @@ -3,8 +3,6 @@ namespace Friendica\Test\src\Core\Cache; use Friendica\Core\Cache\IMemoryCacheDriver; -use Psr\Log\LoggerInterface; -use Psr\Log\NullLogger; abstract class MemoryCacheTest extends CacheTest { @@ -17,11 +15,6 @@ abstract class MemoryCacheTest extends CacheTest { parent::setUp(); - $logger = new NullLogger(); - $this->dice->shouldReceive('create') - ->with(LoggerInterface::class) - ->andReturn($logger); - if (!($this->instance instanceof IMemoryCacheDriver)) { throw new \Exception('MemoryCacheTest unsupported'); } @@ -31,7 +24,8 @@ abstract class MemoryCacheTest extends CacheTest * @small * @dataProvider dataSimple */ - function testCompareSet($value1, $value2) { + function testCompareSet($value1, $value2) + { $this->assertNull($this->instance->get('value1')); $this->instance->add('value1', $value1); @@ -47,7 +41,8 @@ abstract class MemoryCacheTest extends CacheTest * @small * @dataProvider dataSimple */ - function testNegativeCompareSet($value1, $value2) { + function testNegativeCompareSet($value1, $value2) + { $this->assertNull($this->instance->get('value1')); $this->instance->add('value1', $value1); @@ -64,7 +59,8 @@ abstract class MemoryCacheTest extends CacheTest * @small * @dataProvider dataSimple */ - function testCompareDelete($data) { + function testCompareDelete($data) + { $this->assertNull($this->instance->get('value1')); $this->instance->add('value1', $data); @@ -78,7 +74,8 @@ abstract class MemoryCacheTest extends CacheTest * @small * @dataProvider dataSimple */ - function testNegativeCompareDelete($data) { + function testNegativeCompareDelete($data) + { $this->assertNull($this->instance->get('value1')); $this->instance->add('value1', $data); @@ -95,7 +92,8 @@ abstract class MemoryCacheTest extends CacheTest * @small * @dataProvider dataSimple */ - function testAdd($value1, $value2) { + function testAdd($value1, $value2) + { $this->assertNull($this->instance->get('value1')); $this->instance->add('value1', $value1); @@ -111,4 +109,4 @@ abstract class MemoryCacheTest extends CacheTest $this->assertEquals($value2, $received, 'Value was not overwritten by add'); $this->assertNotEquals($value1, $received, 'Value was not overwritten by any other value'); } -} \ No newline at end of file +} diff --git a/tests/src/Core/Cache/RedisCacheDriverTest.php b/tests/src/Core/Cache/RedisCacheDriverTest.php index 6e11c3b8a4..999261834f 100644 --- a/tests/src/Core/Cache/RedisCacheDriverTest.php +++ b/tests/src/Core/Cache/RedisCacheDriverTest.php @@ -3,7 +3,8 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Factory\CacheDriverFactory; +use Friendica\Core\Cache\RedisCacheDriver; +use Friendica\Core\Config\Configuration; /** * @requires extension redis @@ -12,27 +13,27 @@ class RedisCacheDriverTest extends MemoryCacheTest { protected function getInstance() { - $this->configMock + $configMock = \Mockery::mock(Configuration::class); + + $configMock ->shouldReceive('get') ->with('system', 'redis_host') ->andReturn('localhost'); - - $this->configMock + $configMock ->shouldReceive('get') ->with('system', 'redis_port') ->andReturn(null); - $this->configMock + $configMock ->shouldReceive('get') - ->with('system', 'redis_db') + ->with('system', 'redis_db', 0) ->andReturn(3); - - $this->configMock + $configMock ->shouldReceive('get') ->with('system', 'redis_password') ->andReturn(null); - $this->cache = CacheDriverFactory::create('redis'); + $this->cache = new RedisCacheDriver('localhost', $configMock); return $this->cache; } diff --git a/tests/src/Core/Lock/APCuCacheLockDriverTest.php b/tests/src/Core/Lock/APCuCacheLockDriverTest.php index 6e185d6b9a..70796b2698 100644 --- a/tests/src/Core/Lock/APCuCacheLockDriverTest.php +++ b/tests/src/Core/Lock/APCuCacheLockDriverTest.php @@ -2,7 +2,6 @@ namespace Friendica\Test\src\Core\Lock; - use Friendica\Core\Cache\APCuCache; use Friendica\Core\Lock\CacheLockDriver; @@ -19,6 +18,6 @@ class APCuCacheLockDriverTest extends LockTest protected function getInstance() { - return new CacheLockDriver(new APCuCache()); + return new CacheLockDriver(new APCuCache('localhost')); } } diff --git a/tests/src/Core/Lock/ArrayCacheLockDriverTest.php b/tests/src/Core/Lock/ArrayCacheLockDriverTest.php index 671341718b..9b74cd5670 100644 --- a/tests/src/Core/Lock/ArrayCacheLockDriverTest.php +++ b/tests/src/Core/Lock/ArrayCacheLockDriverTest.php @@ -2,7 +2,6 @@ namespace Friendica\Test\src\Core\Lock; - use Friendica\Core\Cache\ArrayCache; use Friendica\Core\Lock\CacheLockDriver; @@ -10,7 +9,7 @@ class ArrayCacheLockDriverTest extends LockTest { protected function getInstance() { - return new CacheLockDriver(new ArrayCache()); + return new CacheLockDriver(new ArrayCache('localhost')); } public function testLockTTL() diff --git a/tests/src/Core/Lock/DatabaseLockDriverTest.php b/tests/src/Core/Lock/DatabaseLockDriverTest.php index 297e76d50b..f1c8376af1 100644 --- a/tests/src/Core/Lock/DatabaseLockDriverTest.php +++ b/tests/src/Core/Lock/DatabaseLockDriverTest.php @@ -2,117 +2,42 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Core\Cache; use Friendica\Core\Lock\DatabaseLockDriver; -use Friendica\Test\Util\DbaLockMockTrait; +use Friendica\Factory\ConfigFactory; +use Friendica\Test\DatabaseTestTrait; +use Friendica\Test\Util\Database\StaticDatabase; +use Friendica\Test\Util\VFSTrait; +use Friendica\Util\ConfigFileLoader; +use Friendica\Util\Profiler; +use Psr\Log\NullLogger; -/** - * @runTestsInSeparateProcesses - * @preserveGlobalState disabled - */ class DatabaseLockDriverTest extends LockTest { - use DbaLockMockTrait; + use VFSTrait; + use DatabaseTestTrait; protected $pid = 123; protected function setUp() { - $this->mockConnected(); - $this->mockConnect(); - - $this->mockReleaseAll($this->pid, 2); + $this->setUpVfsDir(); parent::setUp(); } protected function getInstance() { - return new DatabaseLockDriver($this->pid); - } + $logger = new NullLogger(); + $profiler = \Mockery::mock(Profiler::class); + $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true); - public function testLock() - { - $this->mockIsLocked('foo', false, $this->startTime, 1); - $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockIsLocked('foo', true, $this->startTime, 1); - $this->mockIsLocked('bar', false, $this->startTime, 1); + // load real config to avoid mocking every config-entry which is related to the Database class + $configFactory = new ConfigFactory(); + $loader = new ConfigFileLoader($this->root->url()); + $configCache = $configFactory->createCache($loader); - parent::testLock(); - } + $dba = new StaticDatabase($configCache, $profiler, $logger); - public function testDoubleLock() - { - $this->mockIsLocked('foo', false, $this->startTime, 1); - $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockIsLocked('foo', true, $this->startTime, 1); - $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, true, $this->pid, true, $this->startTime, 1); - - parent::testDoubleLock(); - } - - public function testReleaseLock() - { - $this->mockIsLocked('foo', false, $this->startTime, 1); - $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockIsLocked('foo', true, $this->startTime, 1); - $this->mockReleaseLock('foo', $this->pid, 1); - $this->mockIsLocked('foo', false, $this->startTime, 1); - - parent::testReleaseLock(); - } - - public function testReleaseAll() - { - $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - - $this->mockIsLocked('foo', true, $this->startTime, 1); - $this->mockIsLocked('bar', true, $this->startTime, 1); - $this->mockIsLocked('nice', true, $this->startTime, 1); - - $this->mockReleaseAll($this->pid, 1); - - $this->mockIsLocked('foo', false, $this->startTime, 1); - $this->mockIsLocked('bar', false, $this->startTime, 1); - $this->mockIsLocked('nice', false, $this->startTime, 1); - - parent::testReleaseAll(); - } - - public function testReleaseAfterUnlock() - { - $this->mockIsLocked('foo', false, $this->startTime, 1); - $this->mockIsLocked('bar', false, $this->startTime, 1); - $this->mockIsLocked('nice', false, $this->startTime, 1); - - $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - $this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1); - - $this->mockReleaseLock('foo', $this->pid, 1); - - $this->mockIsLocked('foo', false, $this->startTime, 1); - $this->mockIsLocked('bar', true, $this->startTime, 1); - $this->mockIsLocked('nice', true, $this->startTime, 1); - - $this->mockReleaseAll($this->pid, 1); - - $this->mockIsLocked('bar', false, $this->startTime, 1); - $this->mockIsLocked('nice', false, $this->startTime, 1); - - parent::testReleaseAfterUnlock(); - } - - public function testReleaseWitTTL() - { - $this->mockIsLocked('test', false, $this->startTime, 1); - $this->mockAcquireLock('test', 10, false, $this->pid, false, $this->startTime, 1); - $this->mockIsLocked('test', true, $this->startTime, 1); - $this->mockReleaseLock('test', $this->pid, 1); - $this->mockIsLocked('test', false, $this->startTime, 1); - - parent::testReleaseWitTTL(); + return new DatabaseLockDriver($dba, $this->pid); } } diff --git a/tests/src/Core/Lock/LockTest.php b/tests/src/Core/Lock/LockTest.php index 59d6ee2d5d..5eb409539b 100644 --- a/tests/src/Core/Lock/LockTest.php +++ b/tests/src/Core/Lock/LockTest.php @@ -3,16 +3,9 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Test\MockedTest; -use Friendica\Test\Util\AppMockTrait; -use Friendica\Test\Util\VFSTrait; -use Psr\Log\LoggerInterface; -use Psr\Log\NullLogger; abstract class LockTest extends MockedTest { - use VFSTrait; - use AppMockTrait; - /** * @var int Start time of the mock (used for time operations) */ @@ -27,19 +20,8 @@ abstract class LockTest extends MockedTest protected function setUp() { - // Reusable App object - $this->setUpVfsDir(); - $this->mockApp($this->root); - $this->app - ->shouldReceive('getHostname') - ->andReturn('friendica.local'); - - $logger = new NullLogger(); - $this->dice->shouldReceive('create') - ->with(LoggerInterface::class) - ->andReturn($logger); - parent::setUp(); + $this->instance = $this->getInstance(); $this->instance->releaseAll(); } @@ -53,7 +35,8 @@ abstract class LockTest extends MockedTest /** * @small */ - public function testLock() { + public function testLock() + { $this->assertFalse($this->instance->isLocked('foo')); $this->assertTrue($this->instance->acquireLock('foo', 1)); $this->assertTrue($this->instance->isLocked('foo')); @@ -63,7 +46,8 @@ abstract class LockTest extends MockedTest /** * @small */ - public function testDoubleLock() { + public function testDoubleLock() + { $this->assertFalse($this->instance->isLocked('foo')); $this->assertTrue($this->instance->acquireLock('foo', 1)); $this->assertTrue($this->instance->isLocked('foo')); @@ -74,7 +58,8 @@ abstract class LockTest extends MockedTest /** * @small */ - public function testReleaseLock() { + public function testReleaseLock() + { $this->assertFalse($this->instance->isLocked('foo')); $this->assertTrue($this->instance->acquireLock('foo', 1)); $this->assertTrue($this->instance->isLocked('foo')); @@ -85,7 +70,8 @@ abstract class LockTest extends MockedTest /** * @small */ - public function testReleaseAll() { + public function testReleaseAll() + { $this->assertTrue($this->instance->acquireLock('foo', 1)); $this->assertTrue($this->instance->acquireLock('bar', 1)); $this->assertTrue($this->instance->acquireLock('nice', 1)); @@ -104,7 +90,8 @@ abstract class LockTest extends MockedTest /** * @small */ - public function testReleaseAfterUnlock() { + public function testReleaseAfterUnlock() + { $this->assertFalse($this->instance->isLocked('foo')); $this->assertFalse($this->instance->isLocked('bar')); $this->assertFalse($this->instance->isLocked('nice')); @@ -139,7 +126,8 @@ abstract class LockTest extends MockedTest /** * @medium */ - function testLockTTL() { + function testLockTTL() + { $this->markTestSkipped('taking too much time without mocking'); $this->assertFalse($this->instance->isLocked('foo')); diff --git a/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php b/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php index 8d32ad527d..8c723f34ed 100644 --- a/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php +++ b/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php @@ -3,7 +3,8 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Factory\CacheDriverFactory; +use Friendica\Core\Cache\MemcacheCacheDriver; +use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\CacheLockDriver; /** @@ -13,16 +14,17 @@ class MemcacheCacheLockDriverTest extends LockTest { protected function getInstance() { - $this->configMock + $configMock = \Mockery::mock(Configuration::class); + + $configMock ->shouldReceive('get') ->with('system', 'memcache_host') ->andReturn('localhost'); - - $this->configMock + $configMock ->shouldReceive('get') ->with('system', 'memcache_port') ->andReturn(11211); - return new CacheLockDriver(CacheDriverFactory::create('memcache')); + return new CacheLockDriver(new MemcacheCacheDriver('localhost', $configMock)); } } diff --git a/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php b/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php index f08ffa3817..7685d816de 100644 --- a/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php +++ b/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php @@ -3,8 +3,10 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Factory\CacheDriverFactory; +use Friendica\Core\Cache\MemcachedCacheDriver; +use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\CacheLockDriver; +use Psr\Log\NullLogger; /** * @requires extension memcached @@ -13,11 +15,15 @@ class MemcachedCacheLockDriverTest extends LockTest { protected function getInstance() { - $this->configMock + $configMock = \Mockery::mock(Configuration::class); + + $configMock ->shouldReceive('get') ->with('system', 'memcached_hosts') ->andReturn([0 => 'localhost, 11211']); - return new CacheLockDriver(CacheDriverFactory::create('memcached')); + $logger = new NullLogger(); + + return new CacheLockDriver(new MemcachedCacheDriver('localhost', $configMock, $logger)); } } diff --git a/tests/src/Core/Lock/RedisCacheLockDriverTest.php b/tests/src/Core/Lock/RedisCacheLockDriverTest.php index 21bace5018..fdc961ccc8 100644 --- a/tests/src/Core/Lock/RedisCacheLockDriverTest.php +++ b/tests/src/Core/Lock/RedisCacheLockDriverTest.php @@ -3,8 +3,9 @@ namespace Friendica\Test\src\Core\Lock; +use Friendica\Core\Cache\RedisCacheDriver; +use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\CacheLockDriver; -use Friendica\Factory\CacheDriverFactory; /** * @requires extension redis @@ -13,26 +14,26 @@ class RedisCacheLockDriverTest extends LockTest { protected function getInstance() { - $this->configMock + $configMock = \Mockery::mock(Configuration::class); + + $configMock ->shouldReceive('get') ->with('system', 'redis_host') ->andReturn('localhost'); - - $this->configMock + $configMock ->shouldReceive('get') ->with('system', 'redis_port') ->andReturn(null); - $this->configMock + $configMock ->shouldReceive('get') - ->with('system', 'redis_db') + ->with('system', 'redis_db', 0) ->andReturn(3); - - $this->configMock + $configMock ->shouldReceive('get') ->with('system', 'redis_password') ->andReturn(null); - return new CacheLockDriver(CacheDriverFactory::create('redis')); + return new CacheLockDriver(new RedisCacheDriver('localhost', $configMock)); } } diff --git a/tests/src/Core/Lock/SemaphoreLockDriverTest.php b/tests/src/Core/Lock/SemaphoreLockDriverTest.php index a37fbffbed..d5aaa36ba9 100644 --- a/tests/src/Core/Lock/SemaphoreLockDriverTest.php +++ b/tests/src/Core/Lock/SemaphoreLockDriverTest.php @@ -2,6 +2,10 @@ namespace Friendica\Test\src\Core\Lock; +use Dice\Dice; +use Friendica\App; +use Friendica\BaseObject; +use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\SemaphoreLockDriver; class SemaphoreLockDriverTest extends LockTest @@ -10,12 +14,21 @@ class SemaphoreLockDriverTest extends LockTest { parent::setUp(); - $this->app->shouldReceive('getHostname')->andReturn('friendica.local'); + $dice = \Mockery::mock(Dice::class)->makePartial(); - $this->configMock + $app = \Mockery::mock(App::class); + $app->shouldReceive('getHostname')->andReturn('friendica.local'); + $dice->shouldReceive('create')->with(App::class)->andReturn($app); + + $configMock = \Mockery::mock(Configuration::class); + $configMock ->shouldReceive('get') - ->with('system', 'temppath') + ->with('system', 'temppath', NULL, false) ->andReturn('/tmp/'); + $dice->shouldReceive('create')->with(Configuration::class)->andReturn($configMock); + + // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject + BaseObject::setDependencyInjection($dice); } protected function getInstance() From 86bf2ee45a7c7409dbc470b5b1706b19e7e40507 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 10:26:53 +0200 Subject: [PATCH 02/12] Rename *CacheDriver to *Cache because they don't act as driver anymore --- src/Console/Cache.php | 8 +-- src/Core/Cache.php | 50 +++++++++---------- src/Core/Cache/APCuCache.php | 4 +- ...tractCacheDriver.php => AbstractCache.php} | 4 +- src/Core/Cache/ArrayCache.php | 4 +- ...abaseCacheDriver.php => DatabaseCache.php} | 4 +- .../Cache/{ICacheDriver.php => ICache.php} | 4 +- ...MemoryCacheDriver.php => IMemoryCache.php} | 8 +-- ...cacheCacheDriver.php => MemcacheCache.php} | 4 +- ...chedCacheDriver.php => MemcachedCache.php} | 4 +- src/Core/Cache/ProfilerCache.php | 12 ++--- .../{RedisCacheDriver.php => RedisCache.php} | 4 +- src/Core/Lock.php | 12 ++--- ...bstractLockDriver.php => AbstractLock.php} | 4 +- src/Core/Lock/CacheLockDriver.php | 10 ++-- ...atabaseLockDriver.php => DatabaseLock.php} | 2 +- src/Core/Lock/{ILockDriver.php => ILock.php} | 6 +-- ...aphoreLockDriver.php => SemaphoreLock.php} | 2 +- src/Factory/CacheDriverFactory.php | 12 ++--- src/Factory/LockDriverFactory.php | 22 ++++---- static/dependencies.config.php | 18 +++---- tests/Util/DbaLockMockTrait.php | 24 ++++++--- ...uCacheDriverTest.php => APCuCacheTest.php} | 2 +- ...CacheDriverTest.php => ArrayCacheTest.php} | 2 +- tests/src/Core/Cache/CacheTest.php | 8 +-- ...heDriverTest.php => DatabaseCacheTest.php} | 4 +- ...heDriverTest.php => MemcacheCacheTest.php} | 6 +-- ...eDriverTest.php => MemcachedCacheTest.php} | 6 +-- tests/src/Core/Cache/MemoryCacheTest.php | 6 +-- ...CacheDriverTest.php => RedisCacheTest.php} | 6 +-- ...ckDriverTest.php => APCuCacheLockTest.php} | 2 +- ...kDriverTest.php => ArrayCacheLockTest.php} | 2 +- ...ockDriverTest.php => DatabaseLockTest.php} | 4 +- tests/src/Core/Lock/LockTest.php | 2 +- ...iverTest.php => MemcacheCacheLockTest.php} | 6 +-- ...verTest.php => MemcachedCacheLockTest.php} | 6 +-- ...kDriverTest.php => RedisCacheLockTest.php} | 6 +-- ...ckDriverTest.php => SemaphoreLockTest.php} | 6 +-- 38 files changed, 152 insertions(+), 144 deletions(-) rename src/Core/Cache/{AbstractCacheDriver.php => AbstractCache.php} (95%) rename src/Core/Cache/{DatabaseCacheDriver.php => DatabaseCache.php} (95%) rename src/Core/Cache/{ICacheDriver.php => ICache.php} (96%) rename src/Core/Cache/{IMemoryCacheDriver.php => IMemoryCache.php} (85%) rename src/Core/Cache/{MemcacheCacheDriver.php => MemcacheCache.php} (96%) rename src/Core/Cache/{MemcachedCacheDriver.php => MemcachedCache.php} (96%) rename src/Core/Cache/{RedisCacheDriver.php => RedisCache.php} (96%) rename src/Core/Lock/{AbstractLockDriver.php => AbstractLock.php} (93%) rename src/Core/Lock/{DatabaseLockDriver.php => DatabaseLock.php} (97%) rename src/Core/Lock/{ILockDriver.php => ILock.php} (93%) rename src/Core/Lock/{SemaphoreLockDriver.php => SemaphoreLock.php} (95%) rename tests/src/Core/Cache/{APCuCacheDriverTest.php => APCuCacheTest.php} (89%) rename tests/src/Core/Cache/{ArrayCacheDriverTest.php => ArrayCacheTest.php} (87%) rename tests/src/Core/Cache/{DatabaseCacheDriverTest.php => DatabaseCacheTest.php} (90%) rename tests/src/Core/Cache/{MemcacheCacheDriverTest.php => MemcacheCacheTest.php} (76%) rename tests/src/Core/Cache/{MemcachedCacheDriverTest.php => MemcachedCacheTest.php} (74%) rename tests/src/Core/Cache/{RedisCacheDriverTest.php => RedisCacheTest.php} (82%) rename tests/src/Core/Lock/{APCuCacheLockDriverTest.php => APCuCacheLockTest.php} (88%) rename tests/src/Core/Lock/{ArrayCacheLockDriverTest.php => ArrayCacheLockTest.php} (86%) rename tests/src/Core/Lock/{DatabaseLockDriverTest.php => DatabaseLockTest.php} (91%) rename tests/src/Core/Lock/{MemcacheCacheLockDriverTest.php => MemcacheCacheLockTest.php} (72%) rename tests/src/Core/Lock/{MemcachedCacheLockDriverTest.php => MemcachedCacheLockTest.php} (70%) rename tests/src/Core/Lock/{RedisCacheLockDriverTest.php => RedisCacheLockTest.php} (79%) rename tests/src/Core/Lock/{SemaphoreLockDriverTest.php => SemaphoreLockTest.php} (88%) diff --git a/src/Console/Cache.php b/src/Console/Cache.php index 7596ae6d51..5f508da7a8 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -4,7 +4,7 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; use Friendica\App; -use Friendica\Core\Cache\ICacheDriver; +use Friendica\Core\Cache\ICache; use Friendica\Core\Config\Configuration; use Friendica\Factory\CacheDriverFactory; use RuntimeException; @@ -33,7 +33,7 @@ class Cache extends \Asika\SimpleConsole\Console private $cacheDriverName; /** - * @var ICacheDriver + * @var ICache */ private $cache; @@ -71,7 +71,7 @@ HELP; return $help; } - public function __construct(App\Mode $appMode, Configuration $config, ICacheDriver $cache, array $argv = null) + public function __construct(App\Mode $appMode, Configuration $config, ICache $cache, array $argv = null) { parent::__construct($argv); @@ -161,7 +161,7 @@ HELP; if (count($this->args) >= 3) { $key = $this->getArgument(1); $value = $this->getArgument(2); - $duration = intval($this->getArgument(3, ICacheDriver::FIVE_MINUTES)); + $duration = intval($this->getArgument(3, ICache::FIVE_MINUTES)); if (is_array($this->cache->get($key))) { throw new RuntimeException("$key is an array and can't be set using this command."); diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 0c5597aa5e..e469dcba10 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -5,31 +5,31 @@ namespace Friendica\Core; use Friendica\BaseObject; -use Friendica\Core\Cache\ICacheDriver; +use Friendica\Core\Cache\ICache; /** * @brief Class for storing data for a short time */ class Cache extends BaseObject { - /** @deprecated Use ICacheDriver::MONTH */ - const MONTH = ICacheDriver::MONTH; - /** @deprecated Use ICacheDriver::WEEK */ - const WEEK = 604800; - /** @deprecated Use ICacheDriver::DAY */ - const DAY = 86400; - /** @deprecated Use ICacheDriver::HOUR */ - const HOUR = 3600; - /** @deprecated Use ICacheDriver::HALF_HOUR */ - const HALF_HOUR = 1800; - /** @deprecated Use ICacheDriver::QUARTER_HOUR */ - const QUARTER_HOUR = 900; - /** @deprecated Use ICacheDriver::FIVE_MINUTES */ - const FIVE_MINUTES = 300; - /** @deprecated Use ICacheDriver::MINUTE */ - const MINUTE = 60; - /** @deprecated Use ICacheDriver::INFINITE */ - const INFINITE = 0; + /** @deprecated Use ICache::MONTH */ + const MONTH = ICache::MONTH; + /** @deprecated Use ICache::WEEK */ + const WEEK = ICache::WEEK; + /** @deprecated Use ICache::DAY */ + const DAY = ICache::DAY; + /** @deprecated Use ICache::HOUR */ + const HOUR = ICache::HOUR; + /** @deprecated Use ICache::HALF_HOUR */ + const HALF_HOUR = ICache::HALF_HOUR; + /** @deprecated Use ICache::QUARTER_HOUR */ + const QUARTER_HOUR = ICache::QUARTER_HOUR; + /** @deprecated Use ICache::FIVE_MINUTES */ + const FIVE_MINUTES = ICache::FIVE_MINUTES; + /** @deprecated Use ICache::MINUTE */ + const MINUTE = ICache::MINUTE; + /** @deprecated Use ICache::INFINITE */ + const INFINITE = ICache::INFINITE; /** * @brief Returns all the cache keys sorted alphabetically @@ -41,7 +41,7 @@ class Cache extends BaseObject */ public static function getAllKeys($prefix = null) { - return self::getClass(ICacheDriver::class)->getAllKeys($prefix); + return self::getClass(ICache::class)->getAllKeys($prefix); } /** @@ -54,7 +54,7 @@ class Cache extends BaseObject */ public static function get($key) { - return self::getClass(ICacheDriver::class)->get($key); + return self::getClass(ICache::class)->get($key); } /** @@ -69,9 +69,9 @@ class Cache extends BaseObject * @return bool * @throws \Exception */ - public static function set($key, $value, $duration = ICacheDriver::MONTH) + public static function set($key, $value, $duration = ICache::MONTH) { - return self::getClass(ICacheDriver::class)->set($key, $value, $duration); + return self::getClass(ICache::class)->set($key, $value, $duration); } /** @@ -84,7 +84,7 @@ class Cache extends BaseObject */ public static function delete($key) { - return self::getClass(ICacheDriver::class)->delete($key); + return self::getClass(ICache::class)->delete($key); } /** @@ -97,6 +97,6 @@ class Cache extends BaseObject */ public static function clear($outdated = true) { - return self::getClass(ICacheDriver::class)->clear($outdated); + return self::getClass(ICache::class)->clear($outdated); } } diff --git a/src/Core/Cache/APCuCache.php b/src/Core/Cache/APCuCache.php index b89d7fe4b3..350c8fa067 100644 --- a/src/Core/Cache/APCuCache.php +++ b/src/Core/Cache/APCuCache.php @@ -6,11 +6,11 @@ use Exception; use Friendica\Core\Cache; /** - * APCu Cache Driver. + * APCu Cache. * * @author Philipp Holzer */ -class APCuCache extends AbstractCacheDriver implements IMemoryCacheDriver +class APCuCache extends AbstractCache implements IMemoryCache { use TraitCompareSet; use TraitCompareDelete; diff --git a/src/Core/Cache/AbstractCacheDriver.php b/src/Core/Cache/AbstractCache.php similarity index 95% rename from src/Core/Cache/AbstractCacheDriver.php rename to src/Core/Cache/AbstractCache.php index e12f63bfa4..c60615f089 100644 --- a/src/Core/Cache/AbstractCacheDriver.php +++ b/src/Core/Cache/AbstractCache.php @@ -5,11 +5,11 @@ namespace Friendica\Core\Cache; /** * Abstract class for common used functions * - * Class AbstractCacheDriver + * Class AbstractCache * * @package Friendica\Core\Cache */ -abstract class AbstractCacheDriver implements ICacheDriver +abstract class AbstractCache implements ICache { /** * @var string The hostname diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index f2f37708de..76f867f700 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -5,13 +5,13 @@ 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 AbstractCache implements IMemoryCache { use TraitCompareDelete; diff --git a/src/Core/Cache/DatabaseCacheDriver.php b/src/Core/Cache/DatabaseCache.php similarity index 95% rename from src/Core/Cache/DatabaseCacheDriver.php rename to src/Core/Cache/DatabaseCache.php index f1d6150701..0289ada867 100644 --- a/src/Core/Cache/DatabaseCacheDriver.php +++ b/src/Core/Cache/DatabaseCache.php @@ -7,11 +7,11 @@ use Friendica\Database\Database; use Friendica\Util\DateTimeFormat; /** - * Database Cache Driver + * Database Cache * * @author Hypolite Petovan */ -class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver +class DatabaseCache extends AbstractCache implements ICache { /** * @var Database diff --git a/src/Core/Cache/ICacheDriver.php b/src/Core/Cache/ICache.php similarity index 96% rename from src/Core/Cache/ICacheDriver.php rename to src/Core/Cache/ICache.php index 34b741968a..f57e105cc0 100644 --- a/src/Core/Cache/ICacheDriver.php +++ b/src/Core/Cache/ICache.php @@ -3,11 +3,11 @@ namespace Friendica\Core\Cache; /** - * Cache Driver Interface + * Cache Interface * * @author Hypolite Petovan */ -interface ICacheDriver +interface ICache { const MONTH = 2592000; const WEEK = 604800; diff --git a/src/Core/Cache/IMemoryCacheDriver.php b/src/Core/Cache/IMemoryCache.php similarity index 85% rename from src/Core/Cache/IMemoryCacheDriver.php rename to src/Core/Cache/IMemoryCache.php index e35159a3a6..939e545741 100644 --- a/src/Core/Cache/IMemoryCacheDriver.php +++ b/src/Core/Cache/IMemoryCache.php @@ -5,11 +5,11 @@ namespace 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 @@ -19,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 = ICacheDriver::FIVE_MINUTES); + public function add($key, $value, $ttl = ICache::FIVE_MINUTES); /** * Compares if the old value is set and sets the new value @@ -31,7 +31,7 @@ interface IMemoryCacheDriver extends ICacheDriver * * @return bool */ - public function compareSet($key, $oldValue, $newValue, $ttl = ICacheDriver::FIVE_MINUTES); + public function compareSet($key, $oldValue, $newValue, $ttl = ICache::FIVE_MINUTES); /** * Compares if the old value is set and removes it diff --git a/src/Core/Cache/MemcacheCacheDriver.php b/src/Core/Cache/MemcacheCache.php similarity index 96% rename from src/Core/Cache/MemcacheCacheDriver.php rename to src/Core/Cache/MemcacheCache.php index bf8c34a416..b5b835f488 100644 --- a/src/Core/Cache/MemcacheCacheDriver.php +++ b/src/Core/Cache/MemcacheCache.php @@ -8,11 +8,11 @@ use Friendica\Core\Config\Configuration; use Memcache; /** - * Memcache Cache Driver + * Memcache Cache * * @author Hypolite Petovan */ -class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver +class MemcacheCache extends AbstractCache implements IMemoryCache { use TraitCompareSet; use TraitCompareDelete; diff --git a/src/Core/Cache/MemcachedCacheDriver.php b/src/Core/Cache/MemcachedCache.php similarity index 96% rename from src/Core/Cache/MemcachedCacheDriver.php rename to src/Core/Cache/MemcachedCache.php index 7b3a2d5dfa..f841cae2ce 100644 --- a/src/Core/Cache/MemcachedCacheDriver.php +++ b/src/Core/Cache/MemcachedCache.php @@ -9,11 +9,11 @@ use Memcached; use Psr\Log\LoggerInterface; /** - * Memcached Cache Driver + * Memcached Cache * * @author Hypolite Petovan */ -class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver +class MemcachedCache extends AbstractCache implements IMemoryCache { use TraitCompareSet; use TraitCompareDelete; diff --git a/src/Core/Cache/ProfilerCache.php b/src/Core/Cache/ProfilerCache.php index 04271e7c69..72d72b2716 100644 --- a/src/Core/Cache/ProfilerCache.php +++ b/src/Core/Cache/ProfilerCache.php @@ -11,10 +11,10 @@ use Friendica\Util\Profiler; * * It is using the decorator pattern (@see */ -class ProfilerCache implements ICacheDriver, IMemoryCacheDriver +class ProfilerCache implements ICache, IMemoryCache { /** - * @var ICacheDriver The original cache driver + * @var ICache The original cache driver */ private $cache; @@ -23,7 +23,7 @@ class ProfilerCache implements ICacheDriver, IMemoryCacheDriver */ private $profiler; - public function __construct(ICacheDriver $cache, Profiler $profiler) + public function __construct(ICache $cache, Profiler $profiler) { $this->cache = $cache; $this->profiler = $profiler; @@ -104,7 +104,7 @@ class ProfilerCache implements ICacheDriver, IMemoryCacheDriver */ public function add($key, $value, $ttl = Cache::FIVE_MINUTES) { - if ($this->cache instanceof IMemoryCacheDriver) { + if ($this->cache instanceof IMemoryCache) { $time = microtime(true); $return = $this->cache->add($key, $value, $ttl); @@ -122,7 +122,7 @@ class ProfilerCache implements ICacheDriver, IMemoryCacheDriver */ public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES) { - if ($this->cache instanceof IMemoryCacheDriver) { + if ($this->cache instanceof IMemoryCache) { $time = microtime(true); $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl); @@ -140,7 +140,7 @@ class ProfilerCache implements ICacheDriver, IMemoryCacheDriver */ public function compareDelete($key, $value) { - if ($this->cache instanceof IMemoryCacheDriver) { + if ($this->cache instanceof IMemoryCache) { $time = microtime(true); $return = $this->cache->compareDelete($key, $value); diff --git a/src/Core/Cache/RedisCacheDriver.php b/src/Core/Cache/RedisCache.php similarity index 96% rename from src/Core/Cache/RedisCacheDriver.php rename to src/Core/Cache/RedisCache.php index 951f51dc13..6f3a647c55 100644 --- a/src/Core/Cache/RedisCacheDriver.php +++ b/src/Core/Cache/RedisCache.php @@ -8,12 +8,12 @@ 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 * @author Roland Haeder */ -class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver +class RedisCache extends AbstractCache implements IMemoryCache { /** * @var Redis diff --git a/src/Core/Lock.php b/src/Core/Lock.php index 04dfaa1c5e..06313fdd2d 100644 --- a/src/Core/Lock.php +++ b/src/Core/Lock.php @@ -8,8 +8,8 @@ namespace Friendica\Core; use Friendica\BaseObject; -use Friendica\Core\Cache\ICacheDriver; -use Friendica\Core\Lock\ILockDriver; +use Friendica\Core\Cache\ICache; +use Friendica\Core\Lock\ILock; /** * This class contain Functions for preventing parallel execution of functions @@ -26,9 +26,9 @@ class Lock extends BaseObject * @return boolean Was the lock successful? * @throws \Exception */ - public static function acquire($key, $timeout = 120, $ttl = ICacheDriver::FIVE_MINUTES) + public static function acquire($key, $timeout = 120, $ttl = ICache::FIVE_MINUTES) { - return self::getClass(ILockDriver::class)->acquireLock($key, $timeout, $ttl); + return self::getClass(ILock::class)->acquireLock($key, $timeout, $ttl); } /** @@ -42,7 +42,7 @@ class Lock extends BaseObject */ public static function release($key, $override = false) { - return self::getClass(ILockDriver::class)->releaseLock($key, $override); + return self::getClass(ILock::class)->releaseLock($key, $override); } /** @@ -52,6 +52,6 @@ class Lock extends BaseObject */ public static function releaseAll() { - self::getClass(ILockDriver::class)->releaseAll(); + self::getClass(ILock::class)->releaseAll(); } } diff --git a/src/Core/Lock/AbstractLockDriver.php b/src/Core/Lock/AbstractLock.php similarity index 93% rename from src/Core/Lock/AbstractLockDriver.php rename to src/Core/Lock/AbstractLock.php index 71ad08d088..31744a9245 100644 --- a/src/Core/Lock/AbstractLockDriver.php +++ b/src/Core/Lock/AbstractLock.php @@ -3,13 +3,13 @@ namespace Friendica\Core\Lock; /** - * Class AbstractLockDriver + * Class AbstractLock * * @package Friendica\Core\Lock * * Basic class for Locking with common functions (local acquired locks, releaseAll, ..) */ -abstract class AbstractLockDriver implements ILockDriver +abstract class AbstractLock implements ILock { /** * @var array The local acquired locks diff --git a/src/Core/Lock/CacheLockDriver.php b/src/Core/Lock/CacheLockDriver.php index 6169933ddc..69db1c27f8 100644 --- a/src/Core/Lock/CacheLockDriver.php +++ b/src/Core/Lock/CacheLockDriver.php @@ -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 CacheLockDriver extends AbstractLock { /** - * @var \Friendica\Core\Cache\ICacheDriver; + * @var \Friendica\Core\Cache\ICache; */ private $cache; /** * CacheLockDriver 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; } diff --git a/src/Core/Lock/DatabaseLockDriver.php b/src/Core/Lock/DatabaseLock.php similarity index 97% rename from src/Core/Lock/DatabaseLockDriver.php rename to src/Core/Lock/DatabaseLock.php index e9e0500bb4..e451f5acb0 100644 --- a/src/Core/Lock/DatabaseLockDriver.php +++ b/src/Core/Lock/DatabaseLock.php @@ -9,7 +9,7 @@ use Friendica\Util\DateTimeFormat; /** * Locking driver that stores the locks in the database */ -class DatabaseLockDriver extends AbstractLockDriver +class DatabaseLock extends AbstractLock { /** * The current ID of the process diff --git a/src/Core/Lock/ILockDriver.php b/src/Core/Lock/ILock.php similarity index 93% rename from src/Core/Lock/ILockDriver.php rename to src/Core/Lock/ILock.php index 43793d6287..3c56369eb9 100644 --- a/src/Core/Lock/ILockDriver.php +++ b/src/Core/Lock/ILock.php @@ -5,11 +5,11 @@ namespace Friendica\Core\Lock; use Friendica\Core\Cache; /** - * Lock Driver Interface + * Lock Interface * * @author Philipp Holzer */ -interface ILockDriver +interface ILock { /** * Checks, if a key is currently locked to a or my process @@ -30,7 +30,7 @@ interface ILockDriver * * @return boolean Was the lock successful? */ - public function acquireLock($key, $timeout = 120, $ttl = Cache\ICacheDriver::FIVE_MINUTES); + public function acquireLock($key, $timeout = 120, $ttl = Cache\ICache::FIVE_MINUTES); /** * Releases a lock if it was set by us diff --git a/src/Core/Lock/SemaphoreLockDriver.php b/src/Core/Lock/SemaphoreLock.php similarity index 95% rename from src/Core/Lock/SemaphoreLockDriver.php rename to src/Core/Lock/SemaphoreLock.php index 781e110b17..b791d8c392 100644 --- a/src/Core/Lock/SemaphoreLockDriver.php +++ b/src/Core/Lock/SemaphoreLock.php @@ -4,7 +4,7 @@ namespace Friendica\Core\Lock; use Friendica\Core\Cache; -class SemaphoreLockDriver extends AbstractLockDriver +class SemaphoreLock extends AbstractLock { private static $semaphore = []; diff --git a/src/Factory/CacheDriverFactory.php b/src/Factory/CacheDriverFactory.php index 6b4c878f12..f604a0507d 100644 --- a/src/Factory/CacheDriverFactory.php +++ b/src/Factory/CacheDriverFactory.php @@ -3,7 +3,7 @@ namespace Friendica\Factory; use Friendica\Core\Cache; -use Friendica\Core\Cache\ICacheDriver; +use Friendica\Core\Cache\ICache; use Friendica\Core\Config\Configuration; use Friendica\Database\Database; use Friendica\Util\BaseURL; @@ -61,7 +61,7 @@ class CacheDriverFactory /** * This method creates a CacheDriver for the given cache driver name * - * @return ICacheDriver The instance of the CacheDriver + * @return ICache The instance of the CacheDriver * @throws \Exception The exception if something went wrong during the CacheDriver creation */ public function create() @@ -70,19 +70,19 @@ class CacheDriverFactory switch ($driver) { case 'memcache': - $cache = new Cache\MemcacheCacheDriver($this->hostname, $this->config); + $cache = new Cache\MemcacheCache($this->hostname, $this->config); break; case 'memcached': - $cache = new Cache\MemcachedCacheDriver($this->hostname, $this->config, $this->logger); + $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger); break; case 'redis': - $cache = new Cache\RedisCacheDriver($this->hostname, $this->config); + $cache = new Cache\RedisCache($this->hostname, $this->config); break; case 'apcu': $cache = new Cache\APCuCache($this->hostname); break; default: - $cache = new Cache\DatabaseCacheDriver($this->hostname, $this->dba); + $cache = new Cache\DatabaseCache($this->hostname, $this->dba); } $profiling = $this->config->get('system', 'profiling', false); diff --git a/src/Factory/LockDriverFactory.php b/src/Factory/LockDriverFactory.php index d31728be6c..b0c87cfcec 100644 --- a/src/Factory/LockDriverFactory.php +++ b/src/Factory/LockDriverFactory.php @@ -2,8 +2,8 @@ namespace Friendica\Factory; -use Friendica\Core\Cache\ICacheDriver; -use Friendica\Core\Cache\IMemoryCacheDriver; +use Friendica\Core\Cache\ICache; +use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Lock; use Friendica\Database\Database; @@ -35,7 +35,7 @@ class LockDriverFactory private $dba; /** - * @var ICacheDriver The memory cache driver in case we use it + * @var ICache The memory cache driver in case we use it */ private $cacheDriver; @@ -49,7 +49,7 @@ class LockDriverFactory */ private $logger; - public function __construct(ICacheDriver $cacheDriver, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger) + public function __construct(ICache $cacheDriver, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger) { $this->cacheDriver = $cacheDriver; $this->config = $config; @@ -66,17 +66,17 @@ class LockDriverFactory case 'memcache': case 'memcached': case 'redis': - if ($this->cacheDriver instanceof IMemoryCacheDriver) { + if ($this->cacheDriver instanceof IMemoryCache) { return new Lock\CacheLockDriver($this->cacheDriver); } break; case 'database': - return new Lock\DatabaseLockDriver($this->dba); + return new Lock\DatabaseLock($this->dba); break; case 'semaphore': - return new Lock\SemaphoreLockDriver(); + return new Lock\SemaphoreLock(); break; default: @@ -96,7 +96,7 @@ class LockDriverFactory * 2. Cache Locking * 3. Database Locking * - * @return Lock\ILockDriver + * @return Lock\ILock */ private function useAutoDriver() { @@ -104,7 +104,7 @@ class LockDriverFactory // 1. Try to use Semaphores for - local - locking if (function_exists('sem_get')) { try { - return new Lock\SemaphoreLockDriver(); + return new Lock\SemaphoreLock(); } catch (\Exception $exception) { $this->logger->debug('Using Semaphore driver for locking failed.', ['exception' => $exception]); } @@ -114,7 +114,7 @@ class LockDriverFactory $cache_driver = $this->config->get('system', 'cache_driver', 'database'); if ($cache_driver != 'database') { try { - if ($this->cacheDriver instanceof IMemoryCacheDriver) { + if ($this->cacheDriver instanceof IMemoryCache) { return new Lock\CacheLockDriver($this->cacheDriver); } } catch (\Exception $exception) { @@ -123,6 +123,6 @@ class LockDriverFactory } // 3. Use Database Locking as a Fallback - return new Lock\DatabaseLockDriver($this->dba); + return new Lock\DatabaseLock($this->dba); } } diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 55a2c6845b..1399fe7285 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -4,7 +4,7 @@ use Dice\Dice; use Friendica\App; use Friendica\Core\Cache; use Friendica\Core\Config; -use Friendica\Core\Lock\ILockDriver; +use Friendica\Core\Lock\ILock; use Friendica\Database\Database; use Friendica\Factory; use Friendica\Util; @@ -106,30 +106,30 @@ return [ * $app = $dice->create(App::class, [], ['$channel' => 'index']); * and is automatically passed as an argument with the same name */ - LoggerInterface::class => [ + LoggerInterface::class => [ 'instanceOf' => Factory\LoggerFactory::class, 'call' => [ ['create', [], Dice::CHAIN_CALL], ], ], - '$devLogger' => [ + '$devLogger' => [ 'instanceOf' => Factory\LoggerFactory::class, 'call' => [ ['createDev', [], Dice::CHAIN_CALL], ] ], - Cache\ICacheDriver::class => [ + Cache\ICache::class => [ 'instanceOf' => Factory\CacheDriverFactory::class, - 'call' => [ + 'call' => [ ['create', [], Dice::CHAIN_CALL], ], ], - Cache\IMemoryCacheDriver::class => [ - 'instanceOf' => Cache\ICacheDriver::class, + Cache\IMemoryCache::class => [ + 'instanceOf' => Cache\ICache::class, ], - ILockDriver::class => [ + ILock::class => [ 'instanceOf' => Factory\LockDriverFactory::class, - 'call' => [ + 'call' => [ ['create', [], Dice::CHAIN_CALL], ], ], diff --git a/tests/Util/DbaLockMockTrait.php b/tests/Util/DbaLockMockTrait.php index 3f76edb884..5b405dcfc2 100644 --- a/tests/Util/DbaLockMockTrait.php +++ b/tests/Util/DbaLockMockTrait.php @@ -3,7 +3,7 @@ namespace Friendica\Test\Util; use Friendica\Core\Cache; -use Friendica\Core\Lock\DatabaseLockDriver; +use Friendica\Core\Lock\DatabaseLock; trait DbaLockMockTrait { @@ -12,7 +12,6 @@ trait DbaLockMockTrait /** * Mocking acquireLock with DBA-backend - * @see DatabaseLockDriver::acquireLock() * * @param mixed $key The key to lock * @param int $ttl The TimeToLive @@ -22,6 +21,9 @@ trait DbaLockMockTrait * @param bool $rowExists True, if a row already exists in the lock table * @param null $time The current timestamp * @param null|int $times How often the method will get used + * + *@see DatabaseLock::acquireLock() + * */ public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null) { @@ -55,12 +57,14 @@ trait DbaLockMockTrait /** * Mocking isLocked with DBA-backend - * @see DatabaseLockDriver::isLocked() * * @param mixed $key The key of the lock * @param null|bool $return True, if the key is already locked - * @param null $time The current timestamp + * @param null $time The current timestamp * @param null|int $times How often the method will get used + * + *@see DatabaseLock::isLocked() + * */ public function mockIsLocked($key, $return = true, $time = null, $times = null) { @@ -76,10 +80,12 @@ trait DbaLockMockTrait /** * Mocking releaseAll with DBA-backend - * @see DatabaseLockDriver::releaseAll() * - * @param null $pid The PID which was set - * @param null|int $times How often the method will get used + * @param null $pid The PID which was set + * @param null|int $times How often the method will get used + * + *@see DatabaseLock::releaseAll() + * */ public function mockReleaseAll($pid = null, $times = null) { @@ -92,11 +98,13 @@ trait DbaLockMockTrait /** * Mocking ReleaseLock with DBA-backend - * @see DatabaseLockDriver::releaseLock() * * @param mixed $key The key to release * @param null|int $pid The PID which was set * @param null|int $times How often the method will get used + * + *@see DatabaseLock::releaseLock() + * */ public function mockReleaseLock($key, $pid = null, $times = null) { diff --git a/tests/src/Core/Cache/APCuCacheDriverTest.php b/tests/src/Core/Cache/APCuCacheTest.php similarity index 89% rename from tests/src/Core/Cache/APCuCacheDriverTest.php rename to tests/src/Core/Cache/APCuCacheTest.php index 9925b02352..1b90be574e 100644 --- a/tests/src/Core/Cache/APCuCacheDriverTest.php +++ b/tests/src/Core/Cache/APCuCacheTest.php @@ -4,7 +4,7 @@ namespace Friendica\Test\src\Core\Cache; use Friendica\Core\Cache\APCuCache; -class APCuCacheDriverTest extends MemoryCacheTest +class APCuCacheTest extends MemoryCacheTest { protected function setUp() { diff --git a/tests/src/Core/Cache/ArrayCacheDriverTest.php b/tests/src/Core/Cache/ArrayCacheTest.php similarity index 87% rename from tests/src/Core/Cache/ArrayCacheDriverTest.php rename to tests/src/Core/Cache/ArrayCacheTest.php index 68b8d1dba1..60ca2761e5 100644 --- a/tests/src/Core/Cache/ArrayCacheDriverTest.php +++ b/tests/src/Core/Cache/ArrayCacheTest.php @@ -4,7 +4,7 @@ namespace Friendica\Test\src\Core\Cache; use Friendica\Core\Cache\ArrayCache; -class ArrayCacheDriverTest extends MemoryCacheTest +class ArrayCacheTest extends MemoryCacheTest { protected function getInstance() { diff --git a/tests/src/Core/Cache/CacheTest.php b/tests/src/Core/Cache/CacheTest.php index 088715cd79..92fdaffa32 100644 --- a/tests/src/Core/Cache/CacheTest.php +++ b/tests/src/Core/Cache/CacheTest.php @@ -2,7 +2,7 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Core\Cache\MemcachedCacheDriver; +use Friendica\Core\Cache\MemcachedCache; use Friendica\Test\MockedTest; use Friendica\Util\PidFile; @@ -14,12 +14,12 @@ abstract class CacheTest extends MockedTest protected $startTime = 1417011228; /** - * @var \Friendica\Core\Cache\ICacheDriver + * @var \Friendica\Core\Cache\ICache */ protected $instance; /** - * @var \Friendica\Core\Cache\IMemoryCacheDriver + * @var \Friendica\Core\Cache\IMemoryCache */ protected $cache; @@ -202,7 +202,7 @@ abstract class CacheTest extends MockedTest */ public function testGetAllKeys($value1, $value2, $value3) { - if ($this->cache instanceof MemcachedCacheDriver) { + if ($this->cache instanceof MemcachedCache) { $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore'); } diff --git a/tests/src/Core/Cache/DatabaseCacheDriverTest.php b/tests/src/Core/Cache/DatabaseCacheTest.php similarity index 90% rename from tests/src/Core/Cache/DatabaseCacheDriverTest.php rename to tests/src/Core/Cache/DatabaseCacheTest.php index 1aa9bf02b8..dbc98bcf4f 100644 --- a/tests/src/Core/Cache/DatabaseCacheDriverTest.php +++ b/tests/src/Core/Cache/DatabaseCacheTest.php @@ -11,7 +11,7 @@ use Friendica\Util\ConfigFileLoader; use Friendica\Util\Profiler; use Psr\Log\NullLogger; -class DatabaseCacheDriverTest extends CacheTest +class DatabaseCacheTest extends CacheTest { use DatabaseTestTrait; use VFSTrait; @@ -36,7 +36,7 @@ class DatabaseCacheDriverTest extends CacheTest $dba = new StaticDatabase($configCache, $profiler, $logger); - $this->cache = new Cache\DatabaseCacheDriver('database', $dba); + $this->cache = new Cache\DatabaseCache('database', $dba); return $this->cache; } diff --git a/tests/src/Core/Cache/MemcacheCacheDriverTest.php b/tests/src/Core/Cache/MemcacheCacheTest.php similarity index 76% rename from tests/src/Core/Cache/MemcacheCacheDriverTest.php rename to tests/src/Core/Cache/MemcacheCacheTest.php index 31aa0b4852..ccc3723153 100644 --- a/tests/src/Core/Cache/MemcacheCacheDriverTest.php +++ b/tests/src/Core/Cache/MemcacheCacheTest.php @@ -2,13 +2,13 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Core\Cache\MemcacheCacheDriver; +use Friendica\Core\Cache\MemcacheCache; use Friendica\Core\Config\Configuration; /** * @requires extension memcache */ -class MemcacheCacheDriverTest extends MemoryCacheTest +class MemcacheCacheTest extends MemoryCacheTest { protected function getInstance() { @@ -23,7 +23,7 @@ class MemcacheCacheDriverTest extends MemoryCacheTest ->with('system', 'memcache_port') ->andReturn(11211); - $this->cache = new MemcacheCacheDriver('localhost', $configMock); + $this->cache = new MemcacheCache('localhost', $configMock); return $this->cache; } diff --git a/tests/src/Core/Cache/MemcachedCacheDriverTest.php b/tests/src/Core/Cache/MemcachedCacheTest.php similarity index 74% rename from tests/src/Core/Cache/MemcachedCacheDriverTest.php rename to tests/src/Core/Cache/MemcachedCacheTest.php index ade3886841..d887250197 100644 --- a/tests/src/Core/Cache/MemcachedCacheDriverTest.php +++ b/tests/src/Core/Cache/MemcachedCacheTest.php @@ -3,14 +3,14 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Core\Cache\MemcachedCacheDriver; +use Friendica\Core\Cache\MemcachedCache; use Friendica\Core\Config\Configuration; use Psr\Log\NullLogger; /** * @requires extension memcached */ -class MemcachedCacheDriverTest extends MemoryCacheTest +class MemcachedCacheTest extends MemoryCacheTest { protected function getInstance() { @@ -23,7 +23,7 @@ class MemcachedCacheDriverTest extends MemoryCacheTest $logger = new NullLogger(); - $this->cache = new MemcachedCacheDriver('localhost', $configMock, $logger); + $this->cache = new MemcachedCache('localhost', $configMock, $logger); return $this->cache; } diff --git a/tests/src/Core/Cache/MemoryCacheTest.php b/tests/src/Core/Cache/MemoryCacheTest.php index 0bd7617dd0..19c102396b 100644 --- a/tests/src/Core/Cache/MemoryCacheTest.php +++ b/tests/src/Core/Cache/MemoryCacheTest.php @@ -2,12 +2,12 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Core\Cache\IMemoryCacheDriver; +use Friendica\Core\Cache\IMemoryCache; abstract class MemoryCacheTest extends CacheTest { /** - * @var \Friendica\Core\Cache\IMemoryCacheDriver + * @var \Friendica\Core\Cache\IMemoryCache */ protected $instance; @@ -15,7 +15,7 @@ abstract class MemoryCacheTest extends CacheTest { parent::setUp(); - if (!($this->instance instanceof IMemoryCacheDriver)) { + if (!($this->instance instanceof IMemoryCache)) { throw new \Exception('MemoryCacheTest unsupported'); } } diff --git a/tests/src/Core/Cache/RedisCacheDriverTest.php b/tests/src/Core/Cache/RedisCacheTest.php similarity index 82% rename from tests/src/Core/Cache/RedisCacheDriverTest.php rename to tests/src/Core/Cache/RedisCacheTest.php index 999261834f..df353252df 100644 --- a/tests/src/Core/Cache/RedisCacheDriverTest.php +++ b/tests/src/Core/Cache/RedisCacheTest.php @@ -3,13 +3,13 @@ namespace Friendica\Test\src\Core\Cache; -use Friendica\Core\Cache\RedisCacheDriver; +use Friendica\Core\Cache\RedisCache; use Friendica\Core\Config\Configuration; /** * @requires extension redis */ -class RedisCacheDriverTest extends MemoryCacheTest +class RedisCacheTest extends MemoryCacheTest { protected function getInstance() { @@ -33,7 +33,7 @@ class RedisCacheDriverTest extends MemoryCacheTest ->with('system', 'redis_password') ->andReturn(null); - $this->cache = new RedisCacheDriver('localhost', $configMock); + $this->cache = new RedisCache('localhost', $configMock); return $this->cache; } diff --git a/tests/src/Core/Lock/APCuCacheLockDriverTest.php b/tests/src/Core/Lock/APCuCacheLockTest.php similarity index 88% rename from tests/src/Core/Lock/APCuCacheLockDriverTest.php rename to tests/src/Core/Lock/APCuCacheLockTest.php index 70796b2698..c961013254 100644 --- a/tests/src/Core/Lock/APCuCacheLockDriverTest.php +++ b/tests/src/Core/Lock/APCuCacheLockTest.php @@ -5,7 +5,7 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\APCuCache; use Friendica\Core\Lock\CacheLockDriver; -class APCuCacheLockDriverTest extends LockTest +class APCuCacheLockTest extends LockTest { protected function setUp() { diff --git a/tests/src/Core/Lock/ArrayCacheLockDriverTest.php b/tests/src/Core/Lock/ArrayCacheLockTest.php similarity index 86% rename from tests/src/Core/Lock/ArrayCacheLockDriverTest.php rename to tests/src/Core/Lock/ArrayCacheLockTest.php index 9b74cd5670..aa4ca47538 100644 --- a/tests/src/Core/Lock/ArrayCacheLockDriverTest.php +++ b/tests/src/Core/Lock/ArrayCacheLockTest.php @@ -5,7 +5,7 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\ArrayCache; use Friendica\Core\Lock\CacheLockDriver; -class ArrayCacheLockDriverTest extends LockTest +class ArrayCacheLockTest extends LockTest { protected function getInstance() { diff --git a/tests/src/Core/Lock/DatabaseLockDriverTest.php b/tests/src/Core/Lock/DatabaseLockTest.php similarity index 91% rename from tests/src/Core/Lock/DatabaseLockDriverTest.php rename to tests/src/Core/Lock/DatabaseLockTest.php index f1c8376af1..2b20b2c101 100644 --- a/tests/src/Core/Lock/DatabaseLockDriverTest.php +++ b/tests/src/Core/Lock/DatabaseLockTest.php @@ -2,7 +2,7 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Core\Lock\DatabaseLockDriver; +use Friendica\Core\Lock\DatabaseLock; use Friendica\Factory\ConfigFactory; use Friendica\Test\DatabaseTestTrait; use Friendica\Test\Util\Database\StaticDatabase; @@ -38,6 +38,6 @@ class DatabaseLockDriverTest extends LockTest $dba = new StaticDatabase($configCache, $profiler, $logger); - return new DatabaseLockDriver($dba, $this->pid); + return new DatabaseLock($dba, $this->pid); } } diff --git a/tests/src/Core/Lock/LockTest.php b/tests/src/Core/Lock/LockTest.php index 5eb409539b..0c231713ae 100644 --- a/tests/src/Core/Lock/LockTest.php +++ b/tests/src/Core/Lock/LockTest.php @@ -12,7 +12,7 @@ abstract class LockTest extends MockedTest protected $startTime = 1417011228; /** - * @var \Friendica\Core\Lock\ILockDriver + * @var \Friendica\Core\Lock\ILock */ protected $instance; diff --git a/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php b/tests/src/Core/Lock/MemcacheCacheLockTest.php similarity index 72% rename from tests/src/Core/Lock/MemcacheCacheLockDriverTest.php rename to tests/src/Core/Lock/MemcacheCacheLockTest.php index 8c723f34ed..b4272b0d25 100644 --- a/tests/src/Core/Lock/MemcacheCacheLockDriverTest.php +++ b/tests/src/Core/Lock/MemcacheCacheLockTest.php @@ -3,14 +3,14 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Core\Cache\MemcacheCacheDriver; +use Friendica\Core\Cache\MemcacheCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\CacheLockDriver; /** * @requires extension Memcache */ -class MemcacheCacheLockDriverTest extends LockTest +class MemcacheCacheLockTest extends LockTest { protected function getInstance() { @@ -25,6 +25,6 @@ class MemcacheCacheLockDriverTest extends LockTest ->with('system', 'memcache_port') ->andReturn(11211); - return new CacheLockDriver(new MemcacheCacheDriver('localhost', $configMock)); + return new CacheLockDriver(new MemcacheCache('localhost', $configMock)); } } diff --git a/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php b/tests/src/Core/Lock/MemcachedCacheLockTest.php similarity index 70% rename from tests/src/Core/Lock/MemcachedCacheLockDriverTest.php rename to tests/src/Core/Lock/MemcachedCacheLockTest.php index 7685d816de..ab5d678d68 100644 --- a/tests/src/Core/Lock/MemcachedCacheLockDriverTest.php +++ b/tests/src/Core/Lock/MemcachedCacheLockTest.php @@ -3,7 +3,7 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Core\Cache\MemcachedCacheDriver; +use Friendica\Core\Cache\MemcachedCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\CacheLockDriver; use Psr\Log\NullLogger; @@ -11,7 +11,7 @@ use Psr\Log\NullLogger; /** * @requires extension memcached */ -class MemcachedCacheLockDriverTest extends LockTest +class MemcachedCacheLockTest extends LockTest { protected function getInstance() { @@ -24,6 +24,6 @@ class MemcachedCacheLockDriverTest extends LockTest $logger = new NullLogger(); - return new CacheLockDriver(new MemcachedCacheDriver('localhost', $configMock, $logger)); + return new CacheLockDriver(new MemcachedCache('localhost', $configMock, $logger)); } } diff --git a/tests/src/Core/Lock/RedisCacheLockDriverTest.php b/tests/src/Core/Lock/RedisCacheLockTest.php similarity index 79% rename from tests/src/Core/Lock/RedisCacheLockDriverTest.php rename to tests/src/Core/Lock/RedisCacheLockTest.php index fdc961ccc8..dab31f5e3c 100644 --- a/tests/src/Core/Lock/RedisCacheLockDriverTest.php +++ b/tests/src/Core/Lock/RedisCacheLockTest.php @@ -3,14 +3,14 @@ namespace Friendica\Test\src\Core\Lock; -use Friendica\Core\Cache\RedisCacheDriver; +use Friendica\Core\Cache\RedisCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Lock\CacheLockDriver; /** * @requires extension redis */ -class RedisCacheLockDriverTest extends LockTest +class RedisCacheLockTest extends LockTest { protected function getInstance() { @@ -34,6 +34,6 @@ class RedisCacheLockDriverTest extends LockTest ->with('system', 'redis_password') ->andReturn(null); - return new CacheLockDriver(new RedisCacheDriver('localhost', $configMock)); + return new CacheLockDriver(new RedisCache('localhost', $configMock)); } } diff --git a/tests/src/Core/Lock/SemaphoreLockDriverTest.php b/tests/src/Core/Lock/SemaphoreLockTest.php similarity index 88% rename from tests/src/Core/Lock/SemaphoreLockDriverTest.php rename to tests/src/Core/Lock/SemaphoreLockTest.php index d5aaa36ba9..7b9b03d728 100644 --- a/tests/src/Core/Lock/SemaphoreLockDriverTest.php +++ b/tests/src/Core/Lock/SemaphoreLockTest.php @@ -6,9 +6,9 @@ use Dice\Dice; use Friendica\App; use Friendica\BaseObject; use Friendica\Core\Config\Configuration; -use Friendica\Core\Lock\SemaphoreLockDriver; +use Friendica\Core\Lock\SemaphoreLock; -class SemaphoreLockDriverTest extends LockTest +class SemaphoreLockTest extends LockTest { public function setUp() { @@ -33,7 +33,7 @@ class SemaphoreLockDriverTest extends LockTest protected function getInstance() { - return new SemaphoreLockDriver(); + return new SemaphoreLock(); } function testLockTTL() From 34e4968c060d0860f72f1d0120751e6cf8513dcb Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 15:42:39 +0200 Subject: [PATCH 03/12] Adding possibility to use a different cache-backend for locking and caching - Renaming *LockDriver to *Lock since it isn't a "driver" anymore --- src/Console/Cache.php | 17 +++---- src/Core/Cache/APCuCache.php | 5 ++ src/Core/Cache/AbstractCache.php | 14 ++++++ src/Core/Cache/ArrayCache.php | 5 ++ src/Core/Cache/DatabaseCache.php | 5 ++ src/Core/Cache/MemcacheCache.php | 5 ++ src/Core/Cache/MemcachedCache.php | 5 ++ src/Core/Cache/ProfilerCache.php | 5 ++ src/Core/Cache/RedisCache.php | 5 ++ .../{CacheLockDriver.php => CacheLock.php} | 4 +- ...acheDriverFactory.php => CacheFactory.php} | 34 +++++++------ ...{LockDriverFactory.php => LockFactory.php} | 50 ++++++++++--------- static/dependencies.config.php | 9 ++-- tests/functional/DependencyCheckTest.php | 30 ++++++++++- tests/src/Core/Lock/APCuCacheLockTest.php | 4 +- tests/src/Core/Lock/ArrayCacheLockTest.php | 4 +- tests/src/Core/Lock/MemcacheCacheLockTest.php | 4 +- .../src/Core/Lock/MemcachedCacheLockTest.php | 4 +- tests/src/Core/Lock/RedisCacheLockTest.php | 4 +- 19 files changed, 149 insertions(+), 64 deletions(-) rename src/Core/Lock/{CacheLockDriver.php => CacheLock.php} (96%) rename src/Factory/{CacheDriverFactory.php => CacheFactory.php} (73%) rename src/Factory/{LockDriverFactory.php => LockFactory.php} (60%) diff --git a/src/Console/Cache.php b/src/Console/Cache.php index 5f508da7a8..754b871dbb 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -5,8 +5,6 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; use Friendica\App; use Friendica\Core\Cache\ICache; -use Friendica\Core\Config\Configuration; -use Friendica\Factory\CacheDriverFactory; use RuntimeException; /** @@ -71,13 +69,12 @@ HELP; return $help; } - public function __construct(App\Mode $appMode, Configuration $config, ICache $cache, array $argv = null) + public function __construct(App\Mode $appMode, ICache $cache, array $argv = null) { parent::__construct($argv); $this->appMode = $appMode; - $this->cache = $cache; - $this->cacheDriverName = $config->get('system', 'cache_driver', CacheDriverFactory::DEFAULT_DRIVER); + $this->cache = $cache; } protected function doExecute() @@ -94,7 +91,7 @@ HELP; } if ($this->getOption('v')) { - $this->out('Cache Driver Name: ' . $this->cacheDriverName); + $this->out('Cache Driver Name: ' . (string)$this->cache); $this->out('Cache Driver Class: ' . get_class($this->cache)); } @@ -127,7 +124,7 @@ HELP; private function executeList() { $prefix = $this->getArgument(1); - $keys = $this->cache->getAllKeys($prefix); + $keys = $this->cache->getAllKeys($prefix); if (empty($prefix)) { $this->out('Listing all cache keys:'); @@ -147,7 +144,7 @@ HELP; private function executeGet() { if (count($this->args) >= 2) { - $key = $this->getArgument(1); + $key = $this->getArgument(1); $value = $this->cache->get($key); $this->out("{$key} => " . var_export($value, true)); @@ -159,8 +156,8 @@ HELP; private function executeSet() { if (count($this->args) >= 3) { - $key = $this->getArgument(1); - $value = $this->getArgument(2); + $key = $this->getArgument(1); + $value = $this->getArgument(2); $duration = intval($this->getArgument(3, ICache::FIVE_MINUTES)); if (is_array($this->cache->get($key))) { diff --git a/src/Core/Cache/APCuCache.php b/src/Core/Cache/APCuCache.php index 350c8fa067..eb879590e7 100644 --- a/src/Core/Cache/APCuCache.php +++ b/src/Core/Cache/APCuCache.php @@ -153,4 +153,9 @@ class APCuCache extends AbstractCache implements IMemoryCache return true; } + + public function __toString() + { + return self::TYPE_APCU; + } } diff --git a/src/Core/Cache/AbstractCache.php b/src/Core/Cache/AbstractCache.php index c60615f089..c1cd9643c0 100644 --- a/src/Core/Cache/AbstractCache.php +++ b/src/Core/Cache/AbstractCache.php @@ -11,6 +11,20 @@ namespace Friendica\Core\Cache; */ abstract class AbstractCache 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'; + + /** + * Force each Cache implementation to define the ToString method + * + * @return string + */ + abstract function __toString(); + /** * @var string The hostname */ diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index 76f867f700..451fec363d 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -92,4 +92,9 @@ class ArrayCache extends AbstractCache implements IMemoryCache return false; } } + + public function __toString() + { + return self::TYPE_ARRAY; + } } diff --git a/src/Core/Cache/DatabaseCache.php b/src/Core/Cache/DatabaseCache.php index 0289ada867..42f40ab1ed 100644 --- a/src/Core/Cache/DatabaseCache.php +++ b/src/Core/Cache/DatabaseCache.php @@ -110,4 +110,9 @@ class DatabaseCache extends AbstractCache implements ICache return $this->dba->delete('cache', ['`k` IS NOT NULL ']); } } + + public function __toString() + { + return self::TYPE_DATABASE; + } } diff --git a/src/Core/Cache/MemcacheCache.php b/src/Core/Cache/MemcacheCache.php index b5b835f488..57c1698c9a 100644 --- a/src/Core/Cache/MemcacheCache.php +++ b/src/Core/Cache/MemcacheCache.php @@ -148,4 +148,9 @@ class MemcacheCache extends AbstractCache implements IMemoryCache $cachekey = $this->getCacheKey($key); return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl); } + + public function __toString() + { + return self::TYPE_MEMCACHE; + } } diff --git a/src/Core/Cache/MemcachedCache.php b/src/Core/Cache/MemcachedCache.php index f841cae2ce..e4c4ef352a 100644 --- a/src/Core/Cache/MemcachedCache.php +++ b/src/Core/Cache/MemcachedCache.php @@ -151,4 +151,9 @@ class MemcachedCache extends AbstractCache implements IMemoryCache $cachekey = $this->getCacheKey($key); return $this->memcached->add($cachekey, $value, $ttl); } + + public function __toString() + { + return self::TYPE_MEMCACHED; + } } diff --git a/src/Core/Cache/ProfilerCache.php b/src/Core/Cache/ProfilerCache.php index 72d72b2716..67f606958a 100644 --- a/src/Core/Cache/ProfilerCache.php +++ b/src/Core/Cache/ProfilerCache.php @@ -152,4 +152,9 @@ class ProfilerCache implements ICache, IMemoryCache return false; } } + + public function __toString() + { + return (string)$this->cache . ' (with profiler)'; + } } diff --git a/src/Core/Cache/RedisCache.php b/src/Core/Cache/RedisCache.php index 6f3a647c55..40cb56d35c 100644 --- a/src/Core/Cache/RedisCache.php +++ b/src/Core/Cache/RedisCache.php @@ -192,4 +192,9 @@ class RedisCache extends AbstractCache implements IMemoryCache $this->redis->unwatch(); return false; } + + public function __toString() + { + return self::TYPE_REDIS; + } } diff --git a/src/Core/Lock/CacheLockDriver.php b/src/Core/Lock/CacheLock.php similarity index 96% rename from src/Core/Lock/CacheLockDriver.php rename to src/Core/Lock/CacheLock.php index 69db1c27f8..b38c5ed9af 100644 --- a/src/Core/Lock/CacheLockDriver.php +++ b/src/Core/Lock/CacheLock.php @@ -5,7 +5,7 @@ namespace Friendica\Core\Lock; use Friendica\Core\Cache; use Friendica\Core\Cache\IMemoryCache; -class CacheLockDriver extends AbstractLock +class CacheLock extends AbstractLock { /** * @var \Friendica\Core\Cache\ICache; @@ -13,7 +13,7 @@ class CacheLockDriver extends AbstractLock private $cache; /** - * CacheLockDriver constructor. + * CacheLock constructor. * * @param IMemoryCache $cache The CacheDriver for this type of lock */ diff --git a/src/Factory/CacheDriverFactory.php b/src/Factory/CacheFactory.php similarity index 73% rename from src/Factory/CacheDriverFactory.php rename to src/Factory/CacheFactory.php index f604a0507d..afb799e01c 100644 --- a/src/Factory/CacheDriverFactory.php +++ b/src/Factory/CacheFactory.php @@ -11,18 +11,18 @@ use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; /** - * Class CacheDriverFactory + * Class CacheFactory * * @package Friendica\Core\Cache * * A basic class to generate a CacheDriver */ -class CacheDriverFactory +class CacheFactory { /** - * @var string The default driver for caching + * @var string The default cache if nothing set */ - const DEFAULT_DRIVER = 'database'; + const DEFAULT_TYPE = Cache\AbstractCache::TYPE_DATABASE; /** * @var Configuration The configuration to read parameters out of the config @@ -52,33 +52,37 @@ class CacheDriverFactory public function __construct(BaseURL $baseURL, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger) { $this->hostname = $baseURL->getHostname(); - $this->config = $config; - $this->dba = $dba; + $this->config = $config; + $this->dba = $dba; $this->profiler = $profiler; - $this->logger = $logger; + $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() + public function create(string $type = null) { - $driver = $this->config->get('system', 'cache_driver', self::DEFAULT_DRIVER); + if (empty($type)) { + $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE); + } - switch ($driver) { - case 'memcache': + switch ($type) { + case Cache\AbstractCache::TYPE_MEMCACHE: $cache = new Cache\MemcacheCache($this->hostname, $this->config); break; - case 'memcached': + case Cache\AbstractCache::TYPE_MEMCACHED: $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger); break; - case 'redis': + case Cache\AbstractCache::TYPE_REDIS: $cache = new Cache\RedisCache($this->hostname, $this->config); break; - case 'apcu': + case Cache\AbstractCache::TYPE_APCU: $cache = new Cache\APCuCache($this->hostname); break; default: @@ -89,7 +93,7 @@ class CacheDriverFactory // In case profiling is enabled, wrap the ProfilerCache around the current cache if (isset($profiling) && $profiling !== false) { - return new Cache\ProfilerCache($cache, $this->profiler); + return new Cache\ProfilerCache($cache, $this->profiler); } else { return $cache; } diff --git a/src/Factory/LockDriverFactory.php b/src/Factory/LockFactory.php similarity index 60% rename from src/Factory/LockDriverFactory.php rename to src/Factory/LockFactory.php index b0c87cfcec..c1e76f6dea 100644 --- a/src/Factory/LockDriverFactory.php +++ b/src/Factory/LockFactory.php @@ -2,7 +2,7 @@ namespace Friendica\Factory; -use Friendica\Core\Cache\ICache; +use Friendica\Core\Cache\AbstractCache; use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Lock; @@ -11,13 +11,13 @@ use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; /** - * Class LockDriverFactory + * Class LockFactory * * @package Friendica\Core\Cache * * A basic class to generate a LockDriver */ -class LockDriverFactory +class LockFactory { /** * @var string The default driver for caching @@ -35,9 +35,9 @@ class LockDriverFactory private $dba; /** - * @var ICache The memory cache driver in case we use it + * @var CacheFactory The memory cache driver in case we use it */ - private $cacheDriver; + private $cacheFactory; /** * @var Profiler The optional profiler if the cached should be profiled @@ -49,25 +49,29 @@ class LockDriverFactory */ private $logger; - public function __construct(ICache $cacheDriver, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger) + public function __construct(CacheFactory $cacheFactory, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger) { - $this->cacheDriver = $cacheDriver; - $this->config = $config; - $this->dba = $dba; - $this->logger = $logger; + $this->cacheFactory = $cacheFactory; + $this->config = $config; + $this->dba = $dba; + $this->logger = $logger; } public function create() { - $lock_driver = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER); + $lock_type = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER); try { - switch ($lock_driver) { - case 'memcache': - case 'memcached': - case 'redis': - if ($this->cacheDriver instanceof IMemoryCache) { - return new Lock\CacheLockDriver($this->cacheDriver); + switch ($lock_type) { + case AbstractCache::TYPE_MEMCACHE: + case AbstractCache::TYPE_MEMCACHED: + case AbstractCache::TYPE_REDIS: + case AbstractCache::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; @@ -83,7 +87,7 @@ class LockDriverFactory return self::useAutoDriver(); } } catch (\Exception $exception) { - $this->logger->alert('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]); + $this->logger->alert('Driver \'' . $lock_type . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]); return self::useAutoDriver(); } } @@ -100,7 +104,6 @@ class LockDriverFactory */ private function useAutoDriver() { - // 1. Try to use Semaphores for - local - locking if (function_exists('sem_get')) { try { @@ -111,11 +114,12 @@ class LockDriverFactory } // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!) - $cache_driver = $this->config->get('system', 'cache_driver', 'database'); - if ($cache_driver != 'database') { + $cache_type = $this->config->get('system', 'cache_driver', 'database'); + if ($cache_type != AbstractCache::TYPE_DATABASE) { try { - if ($this->cacheDriver instanceof IMemoryCache) { - return new Lock\CacheLockDriver($this->cacheDriver); + $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]); diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 1399fe7285..1d0908f325 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -119,16 +119,19 @@ return [ ] ], Cache\ICache::class => [ - 'instanceOf' => Factory\CacheDriverFactory::class, + 'instanceOf' => Factory\CacheFactory::class, 'call' => [ ['create', [], Dice::CHAIN_CALL], ], ], Cache\IMemoryCache::class => [ - 'instanceOf' => Cache\ICache::class, + 'instanceOf' => Factory\CacheFactory::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], ], ILock::class => [ - 'instanceOf' => Factory\LockDriverFactory::class, + 'instanceOf' => Factory\LockFactory::class, 'call' => [ ['create', [], Dice::CHAIN_CALL], ], diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index 9fe469d5e8..bc8d256a78 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -4,8 +4,11 @@ namespace functional; use Dice\Dice; use Friendica\App; +use Friendica\Core\Cache\ICache; +use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Config\Configuration; +use Friendica\Core\Lock\ILock; use Friendica\Database\Database; use Friendica\Test\Util\VFSTrait; use Friendica\Util\BasePath; @@ -133,6 +136,31 @@ class dependencyCheck extends TestCase /** @var LoggerInterface $logger */ $logger = $this->dice->create('$devLogger', ['dev']); - self::assertInstanceOf(LoggerInterface::class, $logger); + $this->assertInstanceOf(LoggerInterface::class, $logger); + } + + public function testCache() + { + /** @var ICache $cache */ + $cache = $this->dice->create(ICache::class); + + $this->assertInstanceOf(ICache::class, $cache); + } + + public function testMemoryCache() + { + /** @var IMemoryCache $cache */ + $cache = $this->dice->create(IMemoryCache::class); + + // We need to check "just" ICache, because the default Cache is DB-Cache, which isn't a memorycache + $this->assertInstanceOf(ICache::class, $cache); + } + + public function testLock() + { + /** @var ILock $cache */ + $lock = $this->dice->create(ILock::class); + + $this->assertInstanceOf(ILock::class, $lock); } } diff --git a/tests/src/Core/Lock/APCuCacheLockTest.php b/tests/src/Core/Lock/APCuCacheLockTest.php index c961013254..3fbb3605a1 100644 --- a/tests/src/Core/Lock/APCuCacheLockTest.php +++ b/tests/src/Core/Lock/APCuCacheLockTest.php @@ -3,7 +3,7 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\APCuCache; -use Friendica\Core\Lock\CacheLockDriver; +use Friendica\Core\Lock\CacheLock; class APCuCacheLockTest extends LockTest { @@ -18,6 +18,6 @@ class APCuCacheLockTest extends LockTest protected function getInstance() { - return new CacheLockDriver(new APCuCache('localhost')); + return new CacheLock(new APCuCache('localhost')); } } diff --git a/tests/src/Core/Lock/ArrayCacheLockTest.php b/tests/src/Core/Lock/ArrayCacheLockTest.php index aa4ca47538..cc35d7f5e6 100644 --- a/tests/src/Core/Lock/ArrayCacheLockTest.php +++ b/tests/src/Core/Lock/ArrayCacheLockTest.php @@ -3,13 +3,13 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\ArrayCache; -use Friendica\Core\Lock\CacheLockDriver; +use Friendica\Core\Lock\CacheLock; class ArrayCacheLockTest extends LockTest { protected function getInstance() { - return new CacheLockDriver(new ArrayCache('localhost')); + return new CacheLock(new ArrayCache('localhost')); } public function testLockTTL() diff --git a/tests/src/Core/Lock/MemcacheCacheLockTest.php b/tests/src/Core/Lock/MemcacheCacheLockTest.php index b4272b0d25..f550ac51a6 100644 --- a/tests/src/Core/Lock/MemcacheCacheLockTest.php +++ b/tests/src/Core/Lock/MemcacheCacheLockTest.php @@ -5,7 +5,7 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\MemcacheCache; use Friendica\Core\Config\Configuration; -use Friendica\Core\Lock\CacheLockDriver; +use Friendica\Core\Lock\CacheLock; /** * @requires extension Memcache @@ -25,6 +25,6 @@ class MemcacheCacheLockTest extends LockTest ->with('system', 'memcache_port') ->andReturn(11211); - return new CacheLockDriver(new MemcacheCache('localhost', $configMock)); + return new CacheLock(new MemcacheCache('localhost', $configMock)); } } diff --git a/tests/src/Core/Lock/MemcachedCacheLockTest.php b/tests/src/Core/Lock/MemcachedCacheLockTest.php index ab5d678d68..8b59f91bb7 100644 --- a/tests/src/Core/Lock/MemcachedCacheLockTest.php +++ b/tests/src/Core/Lock/MemcachedCacheLockTest.php @@ -5,7 +5,7 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\MemcachedCache; use Friendica\Core\Config\Configuration; -use Friendica\Core\Lock\CacheLockDriver; +use Friendica\Core\Lock\CacheLock; use Psr\Log\NullLogger; /** @@ -24,6 +24,6 @@ class MemcachedCacheLockTest extends LockTest $logger = new NullLogger(); - return new CacheLockDriver(new MemcachedCache('localhost', $configMock, $logger)); + return new CacheLock(new MemcachedCache('localhost', $configMock, $logger)); } } diff --git a/tests/src/Core/Lock/RedisCacheLockTest.php b/tests/src/Core/Lock/RedisCacheLockTest.php index dab31f5e3c..0ebc02160e 100644 --- a/tests/src/Core/Lock/RedisCacheLockTest.php +++ b/tests/src/Core/Lock/RedisCacheLockTest.php @@ -5,7 +5,7 @@ namespace Friendica\Test\src\Core\Lock; use Friendica\Core\Cache\RedisCache; use Friendica\Core\Config\Configuration; -use Friendica\Core\Lock\CacheLockDriver; +use Friendica\Core\Lock\CacheLock; /** * @requires extension redis @@ -34,6 +34,6 @@ class RedisCacheLockTest extends LockTest ->with('system', 'redis_password') ->andReturn(null); - return new CacheLockDriver(new RedisCache('localhost', $configMock)); + return new CacheLock(new RedisCache('localhost', $configMock)); } } From 19777baa793f51e744adb8e3fc12bb857bd111e3 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 15:51:49 +0200 Subject: [PATCH 04/12] - Move constants to the "Cache" class (more transparent than inside the interface) --- src/Core/Cache.php | 39 ++++++++++--------- src/Core/Cache/APCuCache.php | 3 +- src/Core/Cache/ArrayCache.php | 4 +- .../Cache/{AbstractCache.php => Cache.php} | 12 +++++- src/Core/Cache/DatabaseCache.php | 3 +- src/Core/Cache/ICache.php | 12 +----- src/Core/Cache/MemcacheCache.php | 7 ++-- src/Core/Cache/MemcachedCache.php | 10 ++--- src/Core/Cache/ProfilerCache.php | 1 - src/Core/Cache/RedisCache.php | 3 +- src/Core/Cache/TraitCompareDelete.php | 2 - src/Core/Cache/TraitCompareSet.php | 2 - src/Core/Lock/CacheLock.php | 2 +- src/Core/Lock/DatabaseLock.php | 2 +- src/Core/Lock/{AbstractLock.php => Lock.php} | 2 +- src/Core/Lock/SemaphoreLock.php | 2 +- src/Factory/CacheFactory.php | 10 ++--- src/Factory/LockFactory.php | 12 +++--- 18 files changed, 59 insertions(+), 69 deletions(-) rename src/Core/Cache/{AbstractCache.php => Cache.php} (88%) rename src/Core/Lock/{AbstractLock.php => Lock.php} (96%) diff --git a/src/Core/Cache.php b/src/Core/Cache.php index e469dcba10..7a952ff8a1 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -5,6 +5,7 @@ namespace Friendica\Core; use Friendica\BaseObject; +use Friendica\Core\Cache\Cache as CacheClass; use Friendica\Core\Cache\ICache; /** @@ -12,24 +13,24 @@ use Friendica\Core\Cache\ICache; */ class Cache extends BaseObject { - /** @deprecated Use ICache::MONTH */ - const MONTH = ICache::MONTH; - /** @deprecated Use ICache::WEEK */ - const WEEK = ICache::WEEK; - /** @deprecated Use ICache::DAY */ - const DAY = ICache::DAY; - /** @deprecated Use ICache::HOUR */ - const HOUR = ICache::HOUR; - /** @deprecated Use ICache::HALF_HOUR */ - const HALF_HOUR = ICache::HALF_HOUR; - /** @deprecated Use ICache::QUARTER_HOUR */ - const QUARTER_HOUR = ICache::QUARTER_HOUR; - /** @deprecated Use ICache::FIVE_MINUTES */ - const FIVE_MINUTES = ICache::FIVE_MINUTES; - /** @deprecated Use ICache::MINUTE */ - const MINUTE = ICache::MINUTE; - /** @deprecated Use ICache::INFINITE */ - const INFINITE = ICache::INFINITE; + /** @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 @@ -69,7 +70,7 @@ class Cache extends BaseObject * @return bool * @throws \Exception */ - public static function set($key, $value, $duration = ICache::MONTH) + public static function set($key, $value, $duration = CacheClass::MONTH) { return self::getClass(ICache::class)->set($key, $value, $duration); } diff --git a/src/Core/Cache/APCuCache.php b/src/Core/Cache/APCuCache.php index eb879590e7..9afbf05c46 100644 --- a/src/Core/Cache/APCuCache.php +++ b/src/Core/Cache/APCuCache.php @@ -3,14 +3,13 @@ namespace Friendica\Core\Cache; use Exception; -use Friendica\Core\Cache; /** * APCu Cache. * * @author Philipp Holzer */ -class APCuCache extends AbstractCache implements IMemoryCache +class APCuCache extends Cache implements IMemoryCache { use TraitCompareSet; use TraitCompareDelete; diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index 451fec363d..17fbe2f407 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -2,8 +2,6 @@ namespace Friendica\Core\Cache; -use Friendica\Core\Cache; - /** * Implementation of the IMemoryCache mainly for testing purpose * @@ -11,7 +9,7 @@ use Friendica\Core\Cache; * * @package Friendica\Core\Cache */ -class ArrayCache extends AbstractCache implements IMemoryCache +class ArrayCache extends Cache implements IMemoryCache { use TraitCompareDelete; diff --git a/src/Core/Cache/AbstractCache.php b/src/Core/Cache/Cache.php similarity index 88% rename from src/Core/Cache/AbstractCache.php rename to src/Core/Cache/Cache.php index c1cd9643c0..4e24246e88 100644 --- a/src/Core/Cache/AbstractCache.php +++ b/src/Core/Cache/Cache.php @@ -9,7 +9,7 @@ namespace Friendica\Core\Cache; * * @package Friendica\Core\Cache */ -abstract class AbstractCache implements ICache +abstract class Cache implements ICache { const TYPE_APCU = 'apcu'; const TYPE_ARRAY = 'array'; @@ -18,6 +18,16 @@ abstract class AbstractCache implements ICache 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; + /** * Force each Cache implementation to define the ToString method * diff --git a/src/Core/Cache/DatabaseCache.php b/src/Core/Cache/DatabaseCache.php index 42f40ab1ed..e0e371fe5a 100644 --- a/src/Core/Cache/DatabaseCache.php +++ b/src/Core/Cache/DatabaseCache.php @@ -2,7 +2,6 @@ namespace Friendica\Core\Cache; -use Friendica\Core\Cache; use Friendica\Database\Database; use Friendica\Util\DateTimeFormat; @@ -11,7 +10,7 @@ use Friendica\Util\DateTimeFormat; * * @author Hypolite Petovan */ -class DatabaseCache extends AbstractCache implements ICache +class DatabaseCache extends Cache implements ICache { /** * @var Database diff --git a/src/Core/Cache/ICache.php b/src/Core/Cache/ICache.php index f57e105cc0..1ff6a8c52d 100644 --- a/src/Core/Cache/ICache.php +++ b/src/Core/Cache/ICache.php @@ -9,16 +9,6 @@ namespace Friendica\Core\Cache; */ interface ICache { - 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 * @@ -46,7 +36,7 @@ interface ICache * * @return bool */ - public function set($key, $value, $ttl = self::FIVE_MINUTES); + public function set($key, $value, $ttl = Cache::FIVE_MINUTES); /** * Delete a key from the cache diff --git a/src/Core/Cache/MemcacheCache.php b/src/Core/Cache/MemcacheCache.php index 57c1698c9a..002aabdfde 100644 --- a/src/Core/Cache/MemcacheCache.php +++ b/src/Core/Cache/MemcacheCache.php @@ -3,7 +3,6 @@ namespace Friendica\Core\Cache; use Exception; -use Friendica\Core\Cache; use Friendica\Core\Config\Configuration; use Memcache; @@ -12,7 +11,7 @@ use Memcache; * * @author Hypolite Petovan */ -class MemcacheCache extends AbstractCache implements IMemoryCache +class MemcacheCache extends Cache implements IMemoryCache { use TraitCompareSet; use TraitCompareDelete; @@ -48,7 +47,7 @@ class MemcacheCache extends AbstractCache implements IMemoryCache */ public function getAllKeys($prefix = null) { - $keys = []; + $keys = []; $allSlabs = $this->memcache->getExtendedStats('slabs'); foreach ($allSlabs as $slabs) { foreach (array_keys($slabs) as $slabId) { @@ -72,7 +71,7 @@ class MemcacheCache extends AbstractCache implements IMemoryCache */ 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 diff --git a/src/Core/Cache/MemcachedCache.php b/src/Core/Cache/MemcachedCache.php index e4c4ef352a..9b54f05b02 100644 --- a/src/Core/Cache/MemcachedCache.php +++ b/src/Core/Cache/MemcachedCache.php @@ -3,7 +3,6 @@ namespace Friendica\Core\Cache; use Exception; -use Friendica\Core\Cache; use Friendica\Core\Config\Configuration; use Memcached; use Psr\Log\LoggerInterface; @@ -13,7 +12,7 @@ use Psr\Log\LoggerInterface; * * @author Hypolite Petovan */ -class MemcachedCache extends AbstractCache implements IMemoryCache +class MemcachedCache extends Cache implements IMemoryCache { use TraitCompareSet; use TraitCompareDelete; @@ -36,6 +35,7 @@ class MemcachedCache extends AbstractCache implements IMemoryCache * } * * @param array $memcached_hosts + * * @throws \Exception */ public function __construct(string $hostname, Configuration $config, LoggerInterface $logger) @@ -75,7 +75,7 @@ class MemcachedCache extends AbstractCache implements IMemoryCache if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) { return $this->filterArrayKeysByPrefix($keys, $prefix); } else { - $this->logger->debug('Memcached \'getAllKeys\' failed', ['result' => $this->memcached->getResultMessage()]); + $this->logger->debug('Memcached \'getAllKeys\' failed', ['result' => $this->memcached->getResultMessage()]); return []; } } @@ -85,7 +85,7 @@ class MemcachedCache extends AbstractCache implements IMemoryCache */ 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 @@ -94,7 +94,7 @@ class MemcachedCache extends AbstractCache implements IMemoryCache if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) { $return = $value; } else { - $this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]); + $this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]); } return $return; diff --git a/src/Core/Cache/ProfilerCache.php b/src/Core/Cache/ProfilerCache.php index 67f606958a..d2b0092a88 100644 --- a/src/Core/Cache/ProfilerCache.php +++ b/src/Core/Cache/ProfilerCache.php @@ -2,7 +2,6 @@ namespace Friendica\Core\Cache; -use Friendica\Core\Cache; use Friendica\Core\System; use Friendica\Util\Profiler; diff --git a/src/Core/Cache/RedisCache.php b/src/Core/Cache/RedisCache.php index 40cb56d35c..e3884e6086 100644 --- a/src/Core/Cache/RedisCache.php +++ b/src/Core/Cache/RedisCache.php @@ -3,7 +3,6 @@ namespace Friendica\Core\Cache; use Exception; -use Friendica\Core\Cache; use Friendica\Core\Config\Configuration; use Redis; @@ -13,7 +12,7 @@ use Redis; * @author Hypolite Petovan * @author Roland Haeder */ -class RedisCache extends AbstractCache implements IMemoryCache +class RedisCache extends Cache implements IMemoryCache { /** * @var Redis diff --git a/src/Core/Cache/TraitCompareDelete.php b/src/Core/Cache/TraitCompareDelete.php index ef59f69cd1..a553f87516 100644 --- a/src/Core/Cache/TraitCompareDelete.php +++ b/src/Core/Cache/TraitCompareDelete.php @@ -2,8 +2,6 @@ namespace Friendica\Core\Cache; -use Friendica\Core\Cache; - /** * Trait TraitCompareSetDelete * diff --git a/src/Core/Cache/TraitCompareSet.php b/src/Core/Cache/TraitCompareSet.php index 77a6028355..9c192d9529 100644 --- a/src/Core/Cache/TraitCompareSet.php +++ b/src/Core/Cache/TraitCompareSet.php @@ -2,8 +2,6 @@ namespace Friendica\Core\Cache; -use Friendica\Core\Cache; - /** * Trait TraitCompareSetDelete * diff --git a/src/Core/Lock/CacheLock.php b/src/Core/Lock/CacheLock.php index b38c5ed9af..36a7b4edfb 100644 --- a/src/Core/Lock/CacheLock.php +++ b/src/Core/Lock/CacheLock.php @@ -5,7 +5,7 @@ namespace Friendica\Core\Lock; use Friendica\Core\Cache; use Friendica\Core\Cache\IMemoryCache; -class CacheLock extends AbstractLock +class CacheLock extends Lock { /** * @var \Friendica\Core\Cache\ICache; diff --git a/src/Core/Lock/DatabaseLock.php b/src/Core/Lock/DatabaseLock.php index e451f5acb0..e5274b9b9b 100644 --- a/src/Core/Lock/DatabaseLock.php +++ b/src/Core/Lock/DatabaseLock.php @@ -9,7 +9,7 @@ use Friendica\Util\DateTimeFormat; /** * Locking driver that stores the locks in the database */ -class DatabaseLock extends AbstractLock +class DatabaseLock extends Lock { /** * The current ID of the process diff --git a/src/Core/Lock/AbstractLock.php b/src/Core/Lock/Lock.php similarity index 96% rename from src/Core/Lock/AbstractLock.php rename to src/Core/Lock/Lock.php index 31744a9245..4418fee271 100644 --- a/src/Core/Lock/AbstractLock.php +++ b/src/Core/Lock/Lock.php @@ -9,7 +9,7 @@ namespace Friendica\Core\Lock; * * Basic class for Locking with common functions (local acquired locks, releaseAll, ..) */ -abstract class AbstractLock implements ILock +abstract class Lock implements ILock { /** * @var array The local acquired locks diff --git a/src/Core/Lock/SemaphoreLock.php b/src/Core/Lock/SemaphoreLock.php index b791d8c392..789c9e8eca 100644 --- a/src/Core/Lock/SemaphoreLock.php +++ b/src/Core/Lock/SemaphoreLock.php @@ -4,7 +4,7 @@ namespace Friendica\Core\Lock; use Friendica\Core\Cache; -class SemaphoreLock extends AbstractLock +class SemaphoreLock extends Lock { private static $semaphore = []; diff --git a/src/Factory/CacheFactory.php b/src/Factory/CacheFactory.php index afb799e01c..7b30c553e2 100644 --- a/src/Factory/CacheFactory.php +++ b/src/Factory/CacheFactory.php @@ -22,7 +22,7 @@ class CacheFactory /** * @var string The default cache if nothing set */ - const DEFAULT_TYPE = Cache\AbstractCache::TYPE_DATABASE; + const DEFAULT_TYPE = Cache\Cache::TYPE_DATABASE; /** * @var Configuration The configuration to read parameters out of the config @@ -73,16 +73,16 @@ class CacheFactory } switch ($type) { - case Cache\AbstractCache::TYPE_MEMCACHE: + case Cache\Cache::TYPE_MEMCACHE: $cache = new Cache\MemcacheCache($this->hostname, $this->config); break; - case Cache\AbstractCache::TYPE_MEMCACHED: + case Cache\Cache::TYPE_MEMCACHED: $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger); break; - case Cache\AbstractCache::TYPE_REDIS: + case Cache\Cache::TYPE_REDIS: $cache = new Cache\RedisCache($this->hostname, $this->config); break; - case Cache\AbstractCache::TYPE_APCU: + case Cache\Cache::TYPE_APCU: $cache = new Cache\APCuCache($this->hostname); break; default: diff --git a/src/Factory/LockFactory.php b/src/Factory/LockFactory.php index c1e76f6dea..fef6708d21 100644 --- a/src/Factory/LockFactory.php +++ b/src/Factory/LockFactory.php @@ -2,7 +2,7 @@ namespace Friendica\Factory; -use Friendica\Core\Cache\AbstractCache; +use Friendica\Core\Cache\Cache; use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Lock; @@ -63,10 +63,10 @@ class LockFactory try { switch ($lock_type) { - case AbstractCache::TYPE_MEMCACHE: - case AbstractCache::TYPE_MEMCACHED: - case AbstractCache::TYPE_REDIS: - case AbstractCache::TYPE_APCU: + 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); @@ -115,7 +115,7 @@ class LockFactory // 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 != AbstractCache::TYPE_DATABASE) { + if ($cache_type != Cache::TYPE_DATABASE) { try { $cache = $this->cacheFactory->create($cache_type); if ($cache instanceof IMemoryCache) { From 7cdcad9f6c3cce30ba7cf5645a6699535cada69d Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 15:54:37 +0200 Subject: [PATCH 05/12] Remove unused class variable --- src/Console/Cache.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Console/Cache.php b/src/Console/Cache.php index 754b871dbb..e2a3830980 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -25,11 +25,6 @@ class Cache extends \Asika\SimpleConsole\Console */ private $appMode; - /** - * @var string The cache driver name - */ - private $cacheDriverName; - /** * @var ICache */ From d2211bd6ddc7d860f123a06f66b70a5c239a4084 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 15:58:53 +0200 Subject: [PATCH 06/12] Removed some leftovers --- src/Console/Cache.php | 3 ++- src/Core/Cache/IMemoryCache.php | 4 ++-- src/Core/Lock.php | 4 ++-- src/Core/Lock/ILock.php | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Console/Cache.php b/src/Console/Cache.php index e2a3830980..a13e003aab 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -4,6 +4,7 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; use Friendica\App; +use Friendica\Core\Cache\Cache as CacheClass; use Friendica\Core\Cache\ICache; use RuntimeException; @@ -153,7 +154,7 @@ HELP; if (count($this->args) >= 3) { $key = $this->getArgument(1); $value = $this->getArgument(2); - $duration = intval($this->getArgument(3, ICache::FIVE_MINUTES)); + $duration = intval($this->getArgument(3, CacheClass::FIVE_MINUTES)); if (is_array($this->cache->get($key))) { throw new RuntimeException("$key is an array and can't be set using this command."); diff --git a/src/Core/Cache/IMemoryCache.php b/src/Core/Cache/IMemoryCache.php index 939e545741..339f72d04a 100644 --- a/src/Core/Cache/IMemoryCache.php +++ b/src/Core/Cache/IMemoryCache.php @@ -19,7 +19,7 @@ interface IMemoryCache extends ICache * @param int $ttl The cache lifespan, must be one of the Cache constants * @return bool */ - public function add($key, $value, $ttl = ICache::FIVE_MINUTES); + public function add($key, $value, $ttl = Cache::FIVE_MINUTES); /** * Compares if the old value is set and sets the new value @@ -31,7 +31,7 @@ interface IMemoryCache extends ICache * * @return bool */ - public function compareSet($key, $oldValue, $newValue, $ttl = ICache::FIVE_MINUTES); + public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES); /** * Compares if the old value is set and removes it diff --git a/src/Core/Lock.php b/src/Core/Lock.php index 06313fdd2d..ef62bc8f76 100644 --- a/src/Core/Lock.php +++ b/src/Core/Lock.php @@ -8,7 +8,7 @@ namespace Friendica\Core; use Friendica\BaseObject; -use Friendica\Core\Cache\ICache; +use Friendica\Core\Cache\Cache; use Friendica\Core\Lock\ILock; /** @@ -26,7 +26,7 @@ class Lock extends BaseObject * @return boolean Was the lock successful? * @throws \Exception */ - public static function acquire($key, $timeout = 120, $ttl = ICache::FIVE_MINUTES) + public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES) { return self::getClass(ILock::class)->acquireLock($key, $timeout, $ttl); } diff --git a/src/Core/Lock/ILock.php b/src/Core/Lock/ILock.php index 3c56369eb9..0b91daeb56 100644 --- a/src/Core/Lock/ILock.php +++ b/src/Core/Lock/ILock.php @@ -30,7 +30,7 @@ interface ILock * * @return boolean Was the lock successful? */ - public function acquireLock($key, $timeout = 120, $ttl = Cache\ICache::FIVE_MINUTES); + public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES); /** * Releases a lock if it was set by us From 3834d5e1295bb77d7f3adddc81efff4bca799182 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 16:13:53 +0200 Subject: [PATCH 07/12] Renamed __toString() to getName() --- src/Console/Cache.php | 2 +- src/Core/Cache/APCuCache.php | 5 ++++- src/Core/Cache/ArrayCache.php | 5 ++++- src/Core/Cache/Cache.php | 7 ------- src/Core/Cache/DatabaseCache.php | 5 ++++- src/Core/Cache/ICache.php | 7 +++++++ src/Core/Cache/MemcacheCache.php | 5 ++++- src/Core/Cache/MemcachedCache.php | 5 ++++- src/Core/Cache/ProfilerCache.php | 7 +++++-- src/Core/Cache/RedisCache.php | 5 ++++- 10 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/Console/Cache.php b/src/Console/Cache.php index a13e003aab..afb549f4dc 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -87,7 +87,7 @@ HELP; } if ($this->getOption('v')) { - $this->out('Cache Driver Name: ' . (string)$this->cache); + $this->out('Cache Driver Name: ' . $this->cache->getName()); $this->out('Cache Driver Class: ' . get_class($this->cache)); } diff --git a/src/Core/Cache/APCuCache.php b/src/Core/Cache/APCuCache.php index 9afbf05c46..48880fe986 100644 --- a/src/Core/Cache/APCuCache.php +++ b/src/Core/Cache/APCuCache.php @@ -153,7 +153,10 @@ class APCuCache extends Cache implements IMemoryCache return true; } - public function __toString() + /** + * {@inheritDoc} + */ + public function getName() { return self::TYPE_APCU; } diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index 17fbe2f407..5add98cc2a 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -91,7 +91,10 @@ class ArrayCache extends Cache implements IMemoryCache } } - public function __toString() + /** + * {@inheritDoc} + */ + public function getName() { return self::TYPE_ARRAY; } diff --git a/src/Core/Cache/Cache.php b/src/Core/Cache/Cache.php index 4e24246e88..b40c129ae7 100644 --- a/src/Core/Cache/Cache.php +++ b/src/Core/Cache/Cache.php @@ -28,13 +28,6 @@ abstract class Cache implements ICache const MINUTE = 60; const INFINITE = 0; - /** - * Force each Cache implementation to define the ToString method - * - * @return string - */ - abstract function __toString(); - /** * @var string The hostname */ diff --git a/src/Core/Cache/DatabaseCache.php b/src/Core/Cache/DatabaseCache.php index e0e371fe5a..7fbbdb5e3e 100644 --- a/src/Core/Cache/DatabaseCache.php +++ b/src/Core/Cache/DatabaseCache.php @@ -110,7 +110,10 @@ class DatabaseCache extends Cache implements ICache } } - public function __toString() + /** + * {@inheritDoc} + */ + public function getName() { return self::TYPE_DATABASE; } diff --git a/src/Core/Cache/ICache.php b/src/Core/Cache/ICache.php index 1ff6a8c52d..f8e98c5688 100644 --- a/src/Core/Cache/ICache.php +++ b/src/Core/Cache/ICache.php @@ -54,4 +54,11 @@ interface ICache * @return bool */ public function clear($outdated = true); + + /** + * Returns the name of the current cache + * + * @return string + */ + public function getName(); } diff --git a/src/Core/Cache/MemcacheCache.php b/src/Core/Cache/MemcacheCache.php index 002aabdfde..7171669520 100644 --- a/src/Core/Cache/MemcacheCache.php +++ b/src/Core/Cache/MemcacheCache.php @@ -148,7 +148,10 @@ class MemcacheCache extends Cache implements IMemoryCache return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl); } - public function __toString() + /** + * {@inheritDoc} + */ + public function getName() { return self::TYPE_MEMCACHE; } diff --git a/src/Core/Cache/MemcachedCache.php b/src/Core/Cache/MemcachedCache.php index 9b54f05b02..ac0648a6ce 100644 --- a/src/Core/Cache/MemcachedCache.php +++ b/src/Core/Cache/MemcachedCache.php @@ -152,7 +152,10 @@ class MemcachedCache extends Cache implements IMemoryCache return $this->memcached->add($cachekey, $value, $ttl); } - public function __toString() + /** + * {@inheritDoc} + */ + public function getName() { return self::TYPE_MEMCACHED; } diff --git a/src/Core/Cache/ProfilerCache.php b/src/Core/Cache/ProfilerCache.php index d2b0092a88..d59b885609 100644 --- a/src/Core/Cache/ProfilerCache.php +++ b/src/Core/Cache/ProfilerCache.php @@ -152,8 +152,11 @@ class ProfilerCache implements ICache, IMemoryCache } } - public function __toString() + /** + * {@inheritDoc} + */ + public function GetName() { - return (string)$this->cache . ' (with profiler)'; + return $this->cache->getName() . ' (with profiler)'; } } diff --git a/src/Core/Cache/RedisCache.php b/src/Core/Cache/RedisCache.php index e3884e6086..5f8fd7e4ce 100644 --- a/src/Core/Cache/RedisCache.php +++ b/src/Core/Cache/RedisCache.php @@ -192,7 +192,10 @@ class RedisCache extends Cache implements IMemoryCache return false; } - public function __toString() + /** + * {@inheritDoc} + */ + public function getName() { return self::TYPE_REDIS; } From fba0574ec053adee5de39d94cd77a984dc626e80 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 18:50:24 +0200 Subject: [PATCH 08/12] Switched to Classes for ApiTest --- tests/include/ApiTest.php | 58 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 465641af2c..d00aeeb73d 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -8,8 +8,8 @@ namespace Friendica\Test; use Dice\Dice; use Friendica\App; use Friendica\BaseObject; -use Friendica\Core\Config; -use Friendica\Core\PConfig; +use Friendica\Core\Config\Configuration; +use Friendica\Core\Config\PConfiguration; use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Database\Database; @@ -44,6 +44,12 @@ class ApiTest extends DatabaseTest /** @var App */ protected $app; + /** @var Configuration */ + protected $config; + + /** @var Dice */ + protected $dice; + /** * Create variables used by tests. */ @@ -51,13 +57,13 @@ class ApiTest extends DatabaseTest { parent::setUp(); - $dice = new Dice(); - $dice = $dice->addRules(include __DIR__ . '/../../static/dependencies.config.php'); - $dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); - BaseObject::setDependencyInjection($dice); + $this->dice = new Dice(); + $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php'); + $this->dice = $this->dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); + BaseObject::setDependencyInjection($this->dice); /** @var Database $dba */ - $dba = $dice->create(Database::class); + $dba = $this->dice->create(Database::class); // Load the API dataset for the whole API $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba); @@ -101,16 +107,19 @@ class ApiTest extends DatabaseTest $_GET = []; $_SERVER = []; - Config::set('system', 'url', 'http://localhost'); - Config::set('system', 'hostname', 'localhost'); - Config::set('system', 'worker_dont_fork', true); + /** @var Configuration $config */ + $this->config = $this->dice->create(Configuration::class); + + $this->config->set('system', 'url', 'http://localhost'); + $this->config->set('system', 'hostname', 'localhost'); + $this->config->set('system', 'worker_dont_fork', true); // Default config - Config::set('config', 'hostname', 'localhost'); - Config::set('system', 'throttle_limit_day', 100); - Config::set('system', 'throttle_limit_week', 100); - Config::set('system', 'throttle_limit_month', 100); - Config::set('system', 'theme', 'system_theme'); + $this->config->set('config', 'hostname', 'localhost'); + $this->config->set('system', 'throttle_limit_day', 100); + $this->config->set('system', 'throttle_limit_week', 100); + $this->config->set('system', 'throttle_limit_month', 100); + $this->config->set('system', 'theme', 'system_theme'); } /** @@ -441,8 +450,8 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; - Config::set('system', 'profiler', true); - Config::set('rendertime', 'callstack', true); + $this->configset('system', 'profiler', true); + $this->configset('rendertime', 'callstack', true); $this->app->callstack = [ 'database' => ['some_function' => 200], 'database_write' => ['some_function' => 200], @@ -790,7 +799,8 @@ class ApiTest extends DatabaseTest */ public function testApiGetUserWithFrioSchema() { - PConfig::set($this->selfUser['id'], 'frio', 'schema', 'red'); + $pConfig = $this->dice->create(PConfiguration::class); + $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red'); $user = api_get_user($this->app); $this->assertSelfUser($user); $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']); @@ -805,10 +815,11 @@ class ApiTest extends DatabaseTest */ public function testApiGetUserWithCustomFrioSchema() { - $ret1 = PConfig::set($this->selfUser['id'], 'frio', 'schema', '---'); - $ret2 = PConfig::set($this->selfUser['id'], 'frio', 'nav_bg', '#123456'); - $ret3 = PConfig::set($this->selfUser['id'], 'frio', 'link_color', '#123456'); - $ret4 = PConfig::set($this->selfUser['id'], 'frio', 'background_color', '#123456'); + $pConfig = $this->dice->create(PConfiguration::class); + $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---'); + $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456'); + $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456'); + $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456'); $user = api_get_user($this->app); $this->assertSelfUser($user); $this->assertEquals('123456', $user['profile_sidebar_fill_color']); @@ -823,7 +834,8 @@ class ApiTest extends DatabaseTest */ public function testApiGetUserWithEmptyFrioSchema() { - PConfig::set($this->selfUser['id'], 'frio', 'schema', '---'); + $pConfig = $this->dice->create(PConfiguration::class); + $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---'); $user = api_get_user($this->app); $this->assertSelfUser($user); $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']); From a66580444fe04018eae8a5db92f65769f0df28b9 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 4 Aug 2019 19:02:16 +0200 Subject: [PATCH 09/12] Fixing sort of BaseURL usage for App & Cache usage --- src/Core/System.php | 3 ++- tests/include/ApiTest.php | 33 ++++++++++++++------------- tests/src/Content/Text/BBCodeTest.php | 7 ++++++ 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/Core/System.php b/src/Core/System.php index 42587577da..89526bdb4e 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -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); } /** diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index d00aeeb73d..098337aab9 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -65,9 +65,24 @@ class ApiTest extends DatabaseTest /** @var Database $dba */ $dba = $this->dice->create(Database::class); + /** @var Configuration $config */ + $this->config = $this->dice->create(Configuration::class); + + $this->config->set('system', 'url', 'http://localhost'); + $this->config->set('system', 'hostname', 'localhost'); + $this->config->set('system', 'worker_dont_fork', true); + + // Default config + $this->config->set('config', 'hostname', 'localhost'); + $this->config->set('system', 'throttle_limit_day', 100); + $this->config->set('system', 'throttle_limit_week', 100); + $this->config->set('system', 'throttle_limit_month', 100); + $this->config->set('system', 'theme', 'system_theme'); + // Load the API dataset for the whole API $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba); + /** @var App app */ $this->app = BaseObject::getApp(); $this->app->argc = 1; @@ -106,20 +121,6 @@ class ApiTest extends DatabaseTest $_POST = []; $_GET = []; $_SERVER = []; - - /** @var Configuration $config */ - $this->config = $this->dice->create(Configuration::class); - - $this->config->set('system', 'url', 'http://localhost'); - $this->config->set('system', 'hostname', 'localhost'); - $this->config->set('system', 'worker_dont_fork', true); - - // Default config - $this->config->set('config', 'hostname', 'localhost'); - $this->config->set('system', 'throttle_limit_day', 100); - $this->config->set('system', 'throttle_limit_week', 100); - $this->config->set('system', 'throttle_limit_month', 100); - $this->config->set('system', 'theme', 'system_theme'); } /** @@ -450,8 +451,8 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; - $this->configset('system', 'profiler', true); - $this->configset('rendertime', 'callstack', true); + $this->config->set('system', 'profiler', true); + $this->config->set('rendertime', 'callstack', true); $this->app->callstack = [ 'database' => ['some_function' => 200], 'database_write' => ['some_function' => 200], diff --git a/tests/src/Content/Text/BBCodeTest.php b/tests/src/Content/Text/BBCodeTest.php index 3affe4e778..6938f8ed5a 100644 --- a/tests/src/Content/Text/BBCodeTest.php +++ b/tests/src/Content/Text/BBCodeTest.php @@ -7,6 +7,7 @@ use Friendica\Core\L10n\L10n; use Friendica\Test\MockedTest; use Friendica\Test\Util\AppMockTrait; use Friendica\Test\Util\VFSTrait; +use Friendica\Util\BaseURL; class BBCodeTest extends MockedTest { @@ -44,6 +45,12 @@ class BBCodeTest extends MockedTest $this->dice->shouldReceive('create') ->with(L10n::class) ->andReturn($l10nMock); + + $baseUrlMock = \Mockery::mock(BaseURL::class); + $baseUrlMock->shouldReceive('get')->withAnyArgs()->andReturn('friendica.local'); + $this->dice->shouldReceive('create') + ->with(BaseURL::class) + ->andReturn($baseUrlMock); } public function dataLinks() From 9682cc440ce8ac227079dde81631eac1b7141b83 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Mon, 5 Aug 2019 09:02:55 +0200 Subject: [PATCH 10/12] Using method-chaining for DICE --- bin/auth_ejabberd.php | 4 ++-- bin/console.php | 5 +++-- bin/daemon.php | 4 ++-- bin/worker.php | 9 +++++---- index.php | 5 +++-- tests/functional/DependencyCheckTest.php | 7 +++---- tests/include/ApiTest.php | 6 +++--- tests/src/Database/DBATest.php | 6 +++--- tests/src/Database/DBStructureTest.php | 6 +++--- 9 files changed, 27 insertions(+), 25 deletions(-) diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php index a097febbce..206e484470 100755 --- a/bin/auth_ejabberd.php +++ b/bin/auth_ejabberd.php @@ -32,6 +32,7 @@ * */ +use Dice\Dice; use Friendica\App\Mode; use Friendica\BaseObject; use Friendica\Util\ExAuth; @@ -52,8 +53,7 @@ chdir($directory); require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = new \Dice\Dice(); -$dice = $dice->addRules(include __DIR__ . '/../static/dependencies.config.php'); +$dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php'); BaseObject::setDependencyInjection($dice); $appMode = $dice->create(Mode::class); diff --git a/bin/console.php b/bin/console.php index 3a64d1d2a0..4c396854dc 100755 --- a/bin/console.php +++ b/bin/console.php @@ -1,9 +1,10 @@ #!/usr/bin/env php addRules(include __DIR__ . '/../static/dependencies.config.php'); +$dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php'); (new Friendica\Core\Console($dice, $argv))->execute(); diff --git a/bin/daemon.php b/bin/daemon.php index ac6385cbb9..8ea60fa9ac 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -7,6 +7,7 @@ * This script was taken from http://php.net/manual/en/function.pcntl-fork.php */ +use Dice\Dice; use Friendica\Core\Config; use Friendica\Core\Logger; use Friendica\Core\Worker; @@ -31,8 +32,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = new \Dice\Dice(); -$dice = $dice->addRules(include __DIR__ . '/../static/dependencies.config.php'); +$dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php'); \Friendica\BaseObject::setDependencyInjection($dice); $a = \Friendica\BaseObject::getApp(); diff --git a/bin/worker.php b/bin/worker.php index e630ee2347..f6b2d90a59 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -5,7 +5,9 @@ * @brief Starts the background processing */ +use Dice\Dice; use Friendica\App; +use Friendica\BaseObject; use Friendica\Core\Config; use Friendica\Core\Update; use Friendica\Core\Worker; @@ -29,11 +31,10 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = new \Dice\Dice(); -$dice = $dice->addRules(include __DIR__ . '/../static/dependencies.config.php'); +$dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php'); -\Friendica\BaseObject::setDependencyInjection($dice); -$a = \Friendica\BaseObject::getApp(); +BaseObject::setDependencyInjection($dice); +$a = BaseObject::getApp(); // Check the database structure and possibly fixes it Update::check($a->getBasePath(), true, $a->getMode()); diff --git a/index.php b/index.php index 3e56097089..50e553bcd8 100644 --- a/index.php +++ b/index.php @@ -4,14 +4,15 @@ * Friendica */ +use Dice\Dice; + if (!file_exists(__DIR__ . '/vendor/autoload.php')) { die('Vendor path not found. Please execute "bin/composer.phar --no-dev install" on the command line in the web root.'); } require __DIR__ . '/vendor/autoload.php'; -$dice = new \Dice\Dice(); -$dice = $dice->addRules(include __DIR__ . '/static/dependencies.config.php'); +$dice = (new Dice())->addRules(include __DIR__ . '/static/dependencies.config.php'); \Friendica\BaseObject::setDependencyInjection($dice); diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index bc8d256a78..2a3eed5973 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -32,8 +32,8 @@ class dependencyCheck extends TestCase $this->setUpVfsDir(); - $this->dice = new Dice(); - $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php'); + $this->dice = (new Dice()) + ->addRules(include __DIR__ . '/../../static/dependencies.config.php'); } /** @@ -87,8 +87,7 @@ class dependencyCheck extends TestCase ]); // create new DI-library because of shared instance rule (so the Profiler wouldn't get created twice) - $this->dice = new Dice(include __DIR__ . '/../../static/dependencies.config.php'); - $profiler = $this->dice->create(Profiler::class, [$configCache]); + $this->dice = (new Dice())->create(Profiler::class, [$configCache]); $this->assertInstanceOf(Profiler::class, $profiler); $this->assertTrue($profiler->isRendertime()); diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 098337aab9..3410f3049d 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -57,9 +57,9 @@ class ApiTest extends DatabaseTest { parent::setUp(); - $this->dice = new Dice(); - $this->dice = $this->dice->addRules(include __DIR__ . '/../../static/dependencies.config.php'); - $this->dice = $this->dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); + $this->dice = (new Dice()) + ->addRules(include __DIR__ . '/../../static/dependencies.config.php') + ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); BaseObject::setDependencyInjection($this->dice); /** @var Database $dba */ diff --git a/tests/src/Database/DBATest.php b/tests/src/Database/DBATest.php index 6d52581a98..9b2a2f122b 100644 --- a/tests/src/Database/DBATest.php +++ b/tests/src/Database/DBATest.php @@ -15,9 +15,9 @@ class DBATest extends DatabaseTest { parent::setUp(); - $dice = new Dice(); - $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php'); - $dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); + $dice = (new Dice()) + ->addRules(include __DIR__ . '/../../../static/dependencies.config.php') + ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); BaseObject::setDependencyInjection($dice); // Default config diff --git a/tests/src/Database/DBStructureTest.php b/tests/src/Database/DBStructureTest.php index e2938e3048..38c621d4c8 100644 --- a/tests/src/Database/DBStructureTest.php +++ b/tests/src/Database/DBStructureTest.php @@ -15,9 +15,9 @@ class DBStructureTest extends DatabaseTest { parent::setUp(); - $dice = new Dice(); - $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php'); - $dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); + $dice = (new Dice()) + ->addRules(include __DIR__ . '/../../../static/dependencies.config.php') + ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); BaseObject::setDependencyInjection($dice); } From 2e65b96cb4b0193edcf6c0e6a4647f7ebbe01bb8 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Mon, 5 Aug 2019 09:22:06 +0200 Subject: [PATCH 11/12] Revert ProfilerTest --- tests/functional/DependencyCheckTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index 2a3eed5973..5ade50dfb4 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -87,7 +87,8 @@ class dependencyCheck extends TestCase ]); // create new DI-library because of shared instance rule (so the Profiler wouldn't get created twice) - $this->dice = (new Dice())->create(Profiler::class, [$configCache]); + $this->dice = new Dice(include __DIR__ . '/../../static/dependencies.config.php'); + $profiler = $this->dice->create(Profiler::class, [$configCache]); $this->assertInstanceOf(Profiler::class, $profiler); $this->assertTrue($profiler->isRendertime()); From 5442dfa21ed7882519789811b5928f31df8ec6a3 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Tue, 6 Aug 2019 07:43:13 +0200 Subject: [PATCH 12/12] remove constructor parameter --- tests/functional/DependencyCheckTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index 5ade50dfb4..6de2dc6479 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -87,7 +87,7 @@ class dependencyCheck extends TestCase ]); // create new DI-library because of shared instance rule (so the Profiler wouldn't get created twice) - $this->dice = new Dice(include __DIR__ . '/../../static/dependencies.config.php'); + $this->dice = new Dice(); $profiler = $this->dice->create(Profiler::class, [$configCache]); $this->assertInstanceOf(Profiler::class, $profiler);