- Move constants to the "Cache" class (more transparent than inside the interface)

This commit is contained in:
Philipp Holzer 2019-08-04 15:51:49 +02:00
parent 34e4968c06
commit 19777baa79
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
18 changed files with 59 additions and 69 deletions

View File

@ -5,6 +5,7 @@
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Core\Cache\Cache as CacheClass;
use Friendica\Core\Cache\ICache; use Friendica\Core\Cache\ICache;
/** /**
@ -12,24 +13,24 @@ use Friendica\Core\Cache\ICache;
*/ */
class Cache extends BaseObject class Cache extends BaseObject
{ {
/** @deprecated Use ICache::MONTH */ /** @deprecated Use CacheClass::MONTH */
const MONTH = ICache::MONTH; const MONTH = CacheClass::MONTH;
/** @deprecated Use ICache::WEEK */ /** @deprecated Use CacheClass::WEEK */
const WEEK = ICache::WEEK; const WEEK = CacheClass::WEEK;
/** @deprecated Use ICache::DAY */ /** @deprecated Use CacheClass::DAY */
const DAY = ICache::DAY; const DAY = CacheClass::DAY;
/** @deprecated Use ICache::HOUR */ /** @deprecated Use CacheClass::HOUR */
const HOUR = ICache::HOUR; const HOUR = CacheClass::HOUR;
/** @deprecated Use ICache::HALF_HOUR */ /** @deprecated Use CacheClass::HALF_HOUR */
const HALF_HOUR = ICache::HALF_HOUR; const HALF_HOUR = CacheClass::HALF_HOUR;
/** @deprecated Use ICache::QUARTER_HOUR */ /** @deprecated Use CacheClass::QUARTER_HOUR */
const QUARTER_HOUR = ICache::QUARTER_HOUR; const QUARTER_HOUR = CacheClass::QUARTER_HOUR;
/** @deprecated Use ICache::FIVE_MINUTES */ /** @deprecated Use CacheClass::FIVE_MINUTES */
const FIVE_MINUTES = ICache::FIVE_MINUTES; const FIVE_MINUTES = CacheClass::FIVE_MINUTES;
/** @deprecated Use ICache::MINUTE */ /** @deprecated Use CacheClass::MINUTE */
const MINUTE = ICache::MINUTE; const MINUTE = CacheClass::MINUTE;
/** @deprecated Use ICache::INFINITE */ /** @deprecated Use CacheClass::INFINITE */
const INFINITE = ICache::INFINITE; const INFINITE = CacheClass::INFINITE;
/** /**
* @brief Returns all the cache keys sorted alphabetically * @brief Returns all the cache keys sorted alphabetically
@ -69,7 +70,7 @@ class Cache extends BaseObject
* @return bool * @return bool
* @throws \Exception * @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); return self::getClass(ICache::class)->set($key, $value, $duration);
} }

View File

@ -3,14 +3,13 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Exception; use Exception;
use Friendica\Core\Cache;
/** /**
* APCu Cache. * APCu Cache.
* *
* @author Philipp Holzer <admin@philipp.info> * @author Philipp Holzer <admin@philipp.info>
*/ */
class APCuCache extends AbstractCache implements IMemoryCache class APCuCache extends Cache implements IMemoryCache
{ {
use TraitCompareSet; use TraitCompareSet;
use TraitCompareDelete; use TraitCompareDelete;

View File

@ -2,8 +2,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/** /**
* Implementation of the IMemoryCache mainly for testing purpose * Implementation of the IMemoryCache mainly for testing purpose
* *
@ -11,7 +9,7 @@ use Friendica\Core\Cache;
* *
* @package Friendica\Core\Cache * @package Friendica\Core\Cache
*/ */
class ArrayCache extends AbstractCache implements IMemoryCache class ArrayCache extends Cache implements IMemoryCache
{ {
use TraitCompareDelete; use TraitCompareDelete;

View File

@ -9,7 +9,7 @@ namespace Friendica\Core\Cache;
* *
* @package Friendica\Core\Cache * @package Friendica\Core\Cache
*/ */
abstract class AbstractCache implements ICache abstract class Cache implements ICache
{ {
const TYPE_APCU = 'apcu'; const TYPE_APCU = 'apcu';
const TYPE_ARRAY = 'array'; const TYPE_ARRAY = 'array';
@ -18,6 +18,16 @@ abstract class AbstractCache implements ICache
const TYPE_MEMCACHED = 'memcached'; const TYPE_MEMCACHED = 'memcached';
const TYPE_REDIS = 'redis'; 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 * Force each Cache implementation to define the ToString method
* *

View File

@ -2,7 +2,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -11,7 +10,7 @@ use Friendica\Util\DateTimeFormat;
* *
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/ */
class DatabaseCache extends AbstractCache implements ICache class DatabaseCache extends Cache implements ICache
{ {
/** /**
* @var Database * @var Database

View File

@ -9,16 +9,6 @@ namespace Friendica\Core\Cache;
*/ */
interface ICache 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 * Lists all cache keys
* *
@ -46,7 +36,7 @@ interface ICache
* *
* @return bool * @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 * Delete a key from the cache

View File

@ -3,7 +3,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Exception; use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Memcache; use Memcache;
@ -12,7 +11,7 @@ use Memcache;
* *
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/ */
class MemcacheCache extends AbstractCache implements IMemoryCache class MemcacheCache extends Cache implements IMemoryCache
{ {
use TraitCompareSet; use TraitCompareSet;
use TraitCompareDelete; use TraitCompareDelete;
@ -48,7 +47,7 @@ class MemcacheCache extends AbstractCache implements IMemoryCache
*/ */
public function getAllKeys($prefix = null) public function getAllKeys($prefix = null)
{ {
$keys = []; $keys = [];
$allSlabs = $this->memcache->getExtendedStats('slabs'); $allSlabs = $this->memcache->getExtendedStats('slabs');
foreach ($allSlabs as $slabs) { foreach ($allSlabs as $slabs) {
foreach (array_keys($slabs) as $slabId) { foreach (array_keys($slabs) as $slabId) {
@ -72,7 +71,7 @@ class MemcacheCache extends AbstractCache implements IMemoryCache
*/ */
public function get($key) public function get($key)
{ {
$return = null; $return = null;
$cachekey = $this->getCacheKey($key); $cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications // We fetch with the hostname as key to avoid problems with other applications

View File

@ -3,7 +3,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Exception; use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Memcached; use Memcached;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -13,7 +12,7 @@ use Psr\Log\LoggerInterface;
* *
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/ */
class MemcachedCache extends AbstractCache implements IMemoryCache class MemcachedCache extends Cache implements IMemoryCache
{ {
use TraitCompareSet; use TraitCompareSet;
use TraitCompareDelete; use TraitCompareDelete;
@ -36,6 +35,7 @@ class MemcachedCache extends AbstractCache implements IMemoryCache
* } * }
* *
* @param array $memcached_hosts * @param array $memcached_hosts
*
* @throws \Exception * @throws \Exception
*/ */
public function __construct(string $hostname, Configuration $config, LoggerInterface $logger) 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) { if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
return $this->filterArrayKeysByPrefix($keys, $prefix); return $this->filterArrayKeysByPrefix($keys, $prefix);
} else { } else {
$this->logger->debug('Memcached \'getAllKeys\' failed', ['result' => $this->memcached->getResultMessage()]); $this->logger->debug('Memcached \'getAllKeys\' failed', ['result' => $this->memcached->getResultMessage()]);
return []; return [];
} }
} }
@ -85,7 +85,7 @@ class MemcachedCache extends AbstractCache implements IMemoryCache
*/ */
public function get($key) public function get($key)
{ {
$return = null; $return = null;
$cachekey = $this->getCacheKey($key); $cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications // 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) { if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
$return = $value; $return = $value;
} else { } else {
$this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]); $this->logger->debug('Memcached \'get\' failed', ['result' => $this->memcached->getResultMessage()]);
} }
return $return; return $return;

View File

@ -2,7 +2,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;

View File

@ -3,7 +3,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Exception; use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Redis; use Redis;
@ -13,7 +12,7 @@ use Redis;
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
* @author Roland Haeder <roland@mxchange.org> * @author Roland Haeder <roland@mxchange.org>
*/ */
class RedisCache extends AbstractCache implements IMemoryCache class RedisCache extends Cache implements IMemoryCache
{ {
/** /**
* @var Redis * @var Redis

View File

@ -2,8 +2,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/** /**
* Trait TraitCompareSetDelete * Trait TraitCompareSetDelete
* *

View File

@ -2,8 +2,6 @@
namespace Friendica\Core\Cache; namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/** /**
* Trait TraitCompareSetDelete * Trait TraitCompareSetDelete
* *

View File

@ -5,7 +5,7 @@ namespace Friendica\Core\Lock;
use Friendica\Core\Cache; use Friendica\Core\Cache;
use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Cache\IMemoryCache;
class CacheLock extends AbstractLock class CacheLock extends Lock
{ {
/** /**
* @var \Friendica\Core\Cache\ICache; * @var \Friendica\Core\Cache\ICache;

View File

@ -9,7 +9,7 @@ use Friendica\Util\DateTimeFormat;
/** /**
* Locking driver that stores the locks in the database * Locking driver that stores the locks in the database
*/ */
class DatabaseLock extends AbstractLock class DatabaseLock extends Lock
{ {
/** /**
* The current ID of the process * The current ID of the process

View File

@ -9,7 +9,7 @@ namespace Friendica\Core\Lock;
* *
* Basic class for Locking with common functions (local acquired locks, releaseAll, ..) * 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 * @var array The local acquired locks

View File

@ -4,7 +4,7 @@ namespace Friendica\Core\Lock;
use Friendica\Core\Cache; use Friendica\Core\Cache;
class SemaphoreLock extends AbstractLock class SemaphoreLock extends Lock
{ {
private static $semaphore = []; private static $semaphore = [];

View File

@ -22,7 +22,7 @@ class CacheFactory
/** /**
* @var string The default cache if nothing set * @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 * @var Configuration The configuration to read parameters out of the config
@ -73,16 +73,16 @@ class CacheFactory
} }
switch ($type) { switch ($type) {
case Cache\AbstractCache::TYPE_MEMCACHE: case Cache\Cache::TYPE_MEMCACHE:
$cache = new Cache\MemcacheCache($this->hostname, $this->config); $cache = new Cache\MemcacheCache($this->hostname, $this->config);
break; break;
case Cache\AbstractCache::TYPE_MEMCACHED: case Cache\Cache::TYPE_MEMCACHED:
$cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger); $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
break; break;
case Cache\AbstractCache::TYPE_REDIS: case Cache\Cache::TYPE_REDIS:
$cache = new Cache\RedisCache($this->hostname, $this->config); $cache = new Cache\RedisCache($this->hostname, $this->config);
break; break;
case Cache\AbstractCache::TYPE_APCU: case Cache\Cache::TYPE_APCU:
$cache = new Cache\APCuCache($this->hostname); $cache = new Cache\APCuCache($this->hostname);
break; break;
default: default:

View File

@ -2,7 +2,7 @@
namespace Friendica\Factory; namespace Friendica\Factory;
use Friendica\Core\Cache\AbstractCache; use Friendica\Core\Cache\Cache;
use Friendica\Core\Cache\IMemoryCache; use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Lock; use Friendica\Core\Lock;
@ -63,10 +63,10 @@ class LockFactory
try { try {
switch ($lock_type) { switch ($lock_type) {
case AbstractCache::TYPE_MEMCACHE: case Cache::TYPE_MEMCACHE:
case AbstractCache::TYPE_MEMCACHED: case Cache::TYPE_MEMCACHED:
case AbstractCache::TYPE_REDIS: case Cache::TYPE_REDIS:
case AbstractCache::TYPE_APCU: case Cache::TYPE_APCU:
$cache = $this->cacheFactory->create($lock_type); $cache = $this->cacheFactory->create($lock_type);
if ($cache instanceof IMemoryCache) { if ($cache instanceof IMemoryCache) {
return new Lock\CacheLock($cache); 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!) // 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'); $cache_type = $this->config->get('system', 'cache_driver', 'database');
if ($cache_type != AbstractCache::TYPE_DATABASE) { if ($cache_type != Cache::TYPE_DATABASE) {
try { try {
$cache = $this->cacheFactory->create($cache_type); $cache = $this->cacheFactory->create($cache_type);
if ($cache instanceof IMemoryCache) { if ($cache instanceof IMemoryCache) {