mirror of
https://github.com/friendica/friendica
synced 2025-04-26 07:10:12 +00:00
CleanUp Cache namespace
- Introduce enum "Duration" - Introduce enum "Type" - Move "Cache\Cache" to "BaseCache"
This commit is contained in:
parent
4e83b2930e
commit
424c87195b
37 changed files with 139 additions and 118 deletions
|
@ -3,13 +3,14 @@
|
|||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
|
||||
/**
|
||||
* APCu Cache.
|
||||
*
|
||||
* @author Philipp Holzer <admin@philipp.info>
|
||||
*/
|
||||
class APCuCache extends Cache implements IMemoryCache
|
||||
class APCuCache extends BaseCache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
|
@ -76,7 +77,7 @@ class APCuCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
|
@ -129,7 +130,7 @@ class APCuCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cached = serialize($value);
|
||||
|
@ -158,6 +159,6 @@ class APCuCache extends Cache implements IMemoryCache
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_APCU;
|
||||
return Type::APCU;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Friendica\Core\BaseCache;
|
||||
|
||||
/**
|
||||
* Implementation of the IMemoryCache mainly for testing purpose
|
||||
*
|
||||
|
@ -9,7 +11,7 @@ namespace Friendica\Core\Cache;
|
|||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*/
|
||||
class ArrayCache extends Cache implements IMemoryCache
|
||||
class ArrayCache extends BaseCache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareDelete;
|
||||
|
||||
|
@ -38,7 +40,7 @@ class ArrayCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$this->cachedData[$key] = $value;
|
||||
return true;
|
||||
|
@ -70,7 +72,7 @@ class ArrayCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
if (isset($this->cachedData[$key])) {
|
||||
return false;
|
||||
|
@ -82,7 +84,7 @@ class ArrayCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
if ($this->get($key) === $oldValue) {
|
||||
return $this->set($key, $newValue);
|
||||
|
@ -96,6 +98,6 @@ class ArrayCache extends Cache implements IMemoryCache
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_ARRAY;
|
||||
return Type::ARRAY;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,108 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Abstract class for common used functions
|
||||
*
|
||||
* Class AbstractCache
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*/
|
||||
abstract class Cache 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';
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @var string The hostname
|
||||
*/
|
||||
private $hostName;
|
||||
|
||||
public function __construct(string $hostName)
|
||||
{
|
||||
$this->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 $keys The keys, 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 $keys, string $prefix = null)
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
return $keys;
|
||||
} else {
|
||||
$result = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (strpos($key, $prefix) === 0) {
|
||||
array_push($result, $key);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,13 +4,14 @@ namespace Friendica\Core\Cache;
|
|||
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Core\BaseCache;
|
||||
|
||||
/**
|
||||
* Database Cache
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class DatabaseCache extends Cache implements ICache
|
||||
class DatabaseCache extends BaseCache implements ICache
|
||||
{
|
||||
/**
|
||||
* @var Database
|
||||
|
@ -71,7 +72,7 @@ class DatabaseCache extends Cache implements ICache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
if ($ttl > 0) {
|
||||
$fields = [
|
||||
|
@ -115,6 +116,6 @@ class DatabaseCache extends Cache implements ICache
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_DATABASE;
|
||||
return Type::DATABASE;
|
||||
}
|
||||
}
|
||||
|
|
19
src/Core/Cache/Duration.php
Normal file
19
src/Core/Cache/Duration.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Enumeration for cache durations
|
||||
*/
|
||||
abstract class Duration
|
||||
{
|
||||
const MONTH = 2592000;
|
||||
const HOUR = 3600;
|
||||
const HALF_HOUR = 1800;
|
||||
const QUARTER_HOUR = 900;
|
||||
const MINUTE = 60;
|
||||
const WEEK = 604800;
|
||||
const INFINITE = 0;
|
||||
const DAY = 86400;
|
||||
const FIVE_MINUTES = 300;
|
||||
}
|
|
@ -36,7 +36,7 @@ interface ICache
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* Delete a key from the cache
|
||||
|
|
|
@ -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 = Cache::FIVE_MINUTES);
|
||||
public function add($key, $value, $ttl = Duration::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 = Cache::FIVE_MINUTES);
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* Compares if the old value is set and removes it
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Config\IConfiguration;
|
||||
use Memcache;
|
||||
|
||||
|
@ -11,7 +12,7 @@ use Memcache;
|
|||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class MemcacheCache extends Cache implements IMemoryCache
|
||||
class MemcacheCache extends BaseCache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
|
@ -84,7 +85,7 @@ class MemcacheCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
|
@ -129,7 +130,7 @@ class MemcacheCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
|
||||
|
@ -140,6 +141,6 @@ class MemcacheCache extends Cache implements IMemoryCache
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_MEMCACHE;
|
||||
return Type::MEMCACHE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Config\IConfiguration;
|
||||
use Memcached;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -12,7 +13,7 @@ use Psr\Log\LoggerInterface;
|
|||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class MemcachedCache extends Cache implements IMemoryCache
|
||||
class MemcachedCache extends BaseCache implements IMemoryCache
|
||||
{
|
||||
use TraitCompareSet;
|
||||
use TraitCompareDelete;
|
||||
|
@ -102,7 +103,7 @@ class MemcachedCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
|
@ -145,7 +146,7 @@ class MemcachedCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
return $this->memcached->add($cachekey, $value, $ttl);
|
||||
|
@ -156,6 +157,6 @@ class MemcachedCache extends Cache implements IMemoryCache
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_MEMCACHED;
|
||||
return Type::MEMCACHED;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
|
@ -101,7 +101,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
$time = microtime(true);
|
||||
|
@ -119,7 +119,7 @@ class ProfilerCache implements ICache, IMemoryCache
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
if ($this->cache instanceof IMemoryCache) {
|
||||
$time = microtime(true);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Friendica\Core\Cache;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\BaseCache;
|
||||
use Friendica\Core\Config\IConfiguration;
|
||||
use Redis;
|
||||
|
||||
|
@ -12,7 +13,7 @@ use Redis;
|
|||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
* @author Roland Haeder <roland@mxchange.org>
|
||||
*/
|
||||
class RedisCache extends Cache implements IMemoryCache
|
||||
class RedisCache extends BaseCache implements IMemoryCache
|
||||
{
|
||||
/**
|
||||
* @var Redis
|
||||
|
@ -96,7 +97,7 @@ class RedisCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
|
@ -140,7 +141,7 @@ class RedisCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
||||
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
$cached = serialize($value);
|
||||
|
@ -151,7 +152,7 @@ class RedisCache extends Cache implements IMemoryCache
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$cachekey = $this->getCacheKey($key);
|
||||
|
||||
|
@ -199,6 +200,6 @@ class RedisCache extends Cache implements IMemoryCache
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_REDIS;
|
||||
return Type::REDIS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,11 @@ trait TraitCompareDelete
|
|||
{
|
||||
abstract public function get($key);
|
||||
|
||||
abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
|
||||
abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
abstract public function delete($key);
|
||||
|
||||
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
|
||||
abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* NonNative - Compares if the old value is set and removes it
|
||||
|
|
|
@ -13,11 +13,11 @@ trait TraitCompareSet
|
|||
{
|
||||
abstract public function get($key);
|
||||
|
||||
abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
|
||||
abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
abstract public function delete($key);
|
||||
|
||||
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
|
||||
abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* NonNative - Compares if the old value is set and sets the new value
|
||||
|
@ -29,7 +29,7 @@ trait TraitCompareSet
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES) {
|
||||
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES) {
|
||||
if ($this->add($key . "_lock", true)) {
|
||||
if ($this->get($key) === $oldValue) {
|
||||
$this->set($key, $newValue, $ttl);
|
||||
|
|
16
src/Core/Cache/Type.php
Normal file
16
src/Core/Cache/Type.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
/**
|
||||
* Enumeration for cache types
|
||||
*/
|
||||
abstract class Type
|
||||
{
|
||||
const APCU = 'apcu';
|
||||
const REDIS = 'redis';
|
||||
const ARRAY = 'array';
|
||||
const MEMCACHE = 'memcache';
|
||||
const DATABASE = 'database';
|
||||
const MEMCACHED = 'memcached';
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue