mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-12 01:54:26 +02: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
|
@ -1,33 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\Core\Cache\ICache;
|
||||
|
||||
/**
|
||||
* Abstract class for common used functions
|
||||
*
|
||||
* Class AbstractCache
|
||||
*
|
||||
* @package Friendica\Core\Cache
|
||||
*/
|
||||
abstract class Cache implements ICache
|
||||
abstract class BaseCache 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
|
||||
*/
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
}
|
|
@ -30,7 +30,7 @@ class CacheLock extends Lock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache\Duration::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache\Cache;
|
||||
use Friendica\Core\Cache\Duration;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
|
@ -35,7 +35,7 @@ class DatabaseLock extends Lock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
|
|
@ -30,7 +30,7 @@ interface ILock
|
|||
*
|
||||
* @return boolean Was the lock successful?
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache\Duration::FIVE_MINUTES);
|
||||
|
||||
/**
|
||||
* Releases a lock if it was set by us
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache\Cache;
|
||||
use Friendica\Core\Cache\Type;
|
||||
|
||||
/**
|
||||
* Class AbstractLock
|
||||
|
@ -13,7 +13,7 @@ use Friendica\Core\Cache\Cache;
|
|||
*/
|
||||
abstract class Lock implements ILock
|
||||
{
|
||||
const TYPE_DATABASE = Cache::TYPE_DATABASE;
|
||||
const TYPE_DATABASE = Type::DATABASE;
|
||||
const TYPE_SEMAPHORE = 'semaphore';
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,7 +36,7 @@ class SemaphoreLock extends Lock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
|
||||
public function acquire($key, $timeout = 120, $ttl = Cache\Duration::FIVE_MINUTES)
|
||||
{
|
||||
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
|
||||
if (!empty(self::$semaphore[$key])) {
|
||||
|
|
|
@ -7,7 +7,6 @@ use Friendica\Database\DBA;
|
|||
use Friendica\Database\DBStructure;
|
||||
use Friendica\DI;
|
||||
use Friendica\Util\Strings;
|
||||
use Friendica\Core\Cache\Cache;
|
||||
|
||||
class Update
|
||||
{
|
||||
|
@ -97,7 +96,7 @@ class Update
|
|||
|
||||
// Compare the current structure with the defined structure
|
||||
// If the Lock is acquired, never release it automatically to avoid double updates
|
||||
if (DI::lock()->acquire('dbupdate', 120, Cache::INFINITE)) {
|
||||
if (DI::lock()->acquire('dbupdate', 120, Cache\Duration::INFINITE)) {
|
||||
|
||||
// Checks if the build changed during Lock acquiring (so no double update occurs)
|
||||
$retryBuild = Config::get('system', 'build', null, true);
|
||||
|
@ -183,7 +182,7 @@ class Update
|
|||
// If the update fails or times-out completely you may need to
|
||||
// delete the config entry to try again.
|
||||
|
||||
if (DI::lock()->acquire('dbupdate_function', 120,Cache::INFINITE)) {
|
||||
if (DI::lock()->acquire('dbupdate_function', 120, Cache\Duration::INFINITE)) {
|
||||
|
||||
// call the specific update
|
||||
$retval = $funcname();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue