- 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;
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);
}

View File

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

View File

@ -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;

View File

@ -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
*

View File

@ -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 <hypolite@mrpetovan.com>
*/
class DatabaseCache extends AbstractCache implements ICache
class DatabaseCache extends Cache implements ICache
{
/**
* @var Database

View File

@ -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

View File

@ -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 <hypolite@mrpetovan.com>
*/
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

View File

@ -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 <hypolite@mrpetovan.com>
*/
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;

View File

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

View File

@ -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 <hypolite@mrpetovan.com>
* @author Roland Haeder <roland@mxchange.org>
*/
class RedisCache extends AbstractCache implements IMemoryCache
class RedisCache extends Cache implements IMemoryCache
{
/**
* @var Redis

View File

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

View File

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

View File

@ -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;

View File

@ -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

View File

@ -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

View File

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

View File

@ -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:

View File

@ -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) {