From: Philipp Holzer Date: Sun, 4 Aug 2019 13:51:49 +0000 (+0200) Subject: - Move constants to the "Cache" class (more transparent than inside the interface) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=19777baa793f51e744adb8e3fc12bb857bd111e3;p=friendica.git - Move constants to the "Cache" class (more transparent than inside the interface) --- 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/AbstractCache.php b/src/Core/Cache/AbstractCache.php deleted file mode 100644 index c1cd9643c0..0000000000 --- a/src/Core/Cache/AbstractCache.php +++ /dev/null @@ -1,105 +0,0 @@ -hostName = $hostName; - } - - /** - * Returns the prefix (to avoid namespace conflicts) - * - * @return string - * @throws \Exception - */ - protected function getPrefix() - { - // We fetch with the hostname as key to avoid problems with other applications - return $this->hostName; - } - - /** - * @param string $key The original key - * @return string The cache key used for the cache - * @throws \Exception - */ - protected function getCacheKey($key) - { - return $this->getPrefix() . ":" . $key; - } - - /** - * @param array $keys A list of cached keys - * @return array A list of original keys - */ - protected function getOriginalKeys($keys) - { - if (empty($keys)) { - return []; - } else { - // Keys are prefixed with the node hostname, let's remove it - array_walk($keys, function (&$value) { - $value = preg_replace('/^' . $this->hostName . ':/', '', $value); - }); - - sort($keys); - - return $keys; - } - } - - /** - * Filters the keys of an array with a given prefix - * Returns the filtered keys as an new array - * - * @param array $array The array, which should get filtered - * @param string|null $prefix The prefix (if null, all keys will get returned) - * - * @return array The filtered array with just the keys - */ - protected function filterArrayKeysByPrefix($array, $prefix = null) - { - if (empty($prefix)) { - return array_keys($array); - } else { - $result = []; - - foreach (array_keys($array) as $key) { - if (strpos($key, $prefix) === 0) { - array_push($result, $key); - } - } - - return $result; - } - } -} 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/Cache.php b/src/Core/Cache/Cache.php new file mode 100644 index 0000000000..4e24246e88 --- /dev/null +++ b/src/Core/Cache/Cache.php @@ -0,0 +1,115 @@ +hostName = $hostName; + } + + /** + * Returns the prefix (to avoid namespace conflicts) + * + * @return string + * @throws \Exception + */ + protected function getPrefix() + { + // We fetch with the hostname as key to avoid problems with other applications + return $this->hostName; + } + + /** + * @param string $key The original key + * @return string The cache key used for the cache + * @throws \Exception + */ + protected function getCacheKey($key) + { + return $this->getPrefix() . ":" . $key; + } + + /** + * @param array $keys A list of cached keys + * @return array A list of original keys + */ + protected function getOriginalKeys($keys) + { + if (empty($keys)) { + return []; + } else { + // Keys are prefixed with the node hostname, let's remove it + array_walk($keys, function (&$value) { + $value = preg_replace('/^' . $this->hostName . ':/', '', $value); + }); + + sort($keys); + + return $keys; + } + } + + /** + * Filters the keys of an array with a given prefix + * Returns the filtered keys as an new array + * + * @param array $array The array, which should get filtered + * @param string|null $prefix The prefix (if null, all keys will get returned) + * + * @return array The filtered array with just the keys + */ + protected function filterArrayKeysByPrefix($array, $prefix = null) + { + if (empty($prefix)) { + return array_keys($array); + } else { + $result = []; + + foreach (array_keys($array) as $key) { + if (strpos($key, $prefix) === 0) { + array_push($result, $key); + } + } + + return $result; + } + } +} 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/AbstractLock.php b/src/Core/Lock/AbstractLock.php deleted file mode 100644 index 31744a9245..0000000000 --- a/src/Core/Lock/AbstractLock.php +++ /dev/null @@ -1,68 +0,0 @@ -acquireLock[$key]) && $this->acquiredLocks[$key] === true; - } - - /** - * Mark a locally acquired lock - * - * @param string $key The Name of the lock - */ - protected function markAcquire($key) - { - $this->acquiredLocks[$key] = true; - } - - /** - * Mark a release of a locally acquired lock - * - * @param string $key The Name of the lock - */ - protected function markRelease($key) - { - unset($this->acquiredLocks[$key]); - } - - /** - * Releases all lock that were set by us - * - * @return boolean Was the unlock of all locks successful? - */ - public function releaseAll() - { - $return = true; - - foreach ($this->acquiredLocks as $acquiredLock => $hasLock) { - if (!$this->releaseLock($acquiredLock)) { - $return = false; - } - } - - return $return; - } -} 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/Lock.php b/src/Core/Lock/Lock.php new file mode 100644 index 0000000000..4418fee271 --- /dev/null +++ b/src/Core/Lock/Lock.php @@ -0,0 +1,68 @@ +acquireLock[$key]) && $this->acquiredLocks[$key] === true; + } + + /** + * Mark a locally acquired lock + * + * @param string $key The Name of the lock + */ + protected function markAcquire($key) + { + $this->acquiredLocks[$key] = true; + } + + /** + * Mark a release of a locally acquired lock + * + * @param string $key The Name of the lock + */ + protected function markRelease($key) + { + unset($this->acquiredLocks[$key]); + } + + /** + * Releases all lock that were set by us + * + * @return boolean Was the unlock of all locks successful? + */ + public function releaseAll() + { + $return = true; + + foreach ($this->acquiredLocks as $acquiredLock => $hasLock) { + if (!$this->releaseLock($acquiredLock)) { + $return = false; + } + } + + return $return; + } +} 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) {