namespace Friendica\Core;
use Friendica\BaseObject;
+use Friendica\Core\Cache\Cache as CacheClass;
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
* @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);
}
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;
+++ /dev/null
-<?php
-
-namespace Friendica\Core\Cache;
-
-/**
- * Abstract class for common used functions
- *
- * Class AbstractCache
- *
- * @package Friendica\Core\Cache
- */
-abstract class AbstractCache 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';
-
- /**
- * Force each Cache implementation to define the ToString method
- *
- * @return string
- */
- abstract function __toString();
-
- /**
- * @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 $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;
- }
- }
-}
namespace Friendica\Core\Cache;
-use Friendica\Core\Cache;
-
/**
* Implementation of the IMemoryCache mainly for testing purpose
*
*
* @package Friendica\Core\Cache
*/
-class ArrayCache extends AbstractCache implements IMemoryCache
+class ArrayCache extends Cache implements IMemoryCache
{
use TraitCompareDelete;
--- /dev/null
+<?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;
+
+ /**
+ * Force each Cache implementation to define the ToString method
+ *
+ * @return string
+ */
+ abstract function __toString();
+
+ /**
+ * @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 $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;
+ }
+ }
+}
namespace Friendica\Core\Cache;
-use Friendica\Core\Cache;
use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
-class DatabaseCache extends AbstractCache implements ICache
+class DatabaseCache extends Cache implements ICache
{
/**
* @var Database
*/
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
*
*
* @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
namespace Friendica\Core\Cache;
use Exception;
-use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration;
use Memcache;
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
-class MemcacheCache extends AbstractCache implements IMemoryCache
+class MemcacheCache extends Cache implements IMemoryCache
{
use TraitCompareSet;
use TraitCompareDelete;
*/
public function getAllKeys($prefix = null)
{
- $keys = [];
+ $keys = [];
$allSlabs = $this->memcache->getExtendedStats('slabs');
foreach ($allSlabs as $slabs) {
foreach (array_keys($slabs) as $slabId) {
*/
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
namespace Friendica\Core\Cache;
use Exception;
-use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration;
use Memcached;
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;
* }
*
* @param array $memcached_hosts
+ *
* @throws \Exception
*/
public function __construct(string $hostname, Configuration $config, LoggerInterface $logger)
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 [];
}
}
*/
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
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;
namespace Friendica\Core\Cache;
-use Friendica\Core\Cache;
use Friendica\Core\System;
use Friendica\Util\Profiler;
namespace Friendica\Core\Cache;
use Exception;
-use Friendica\Core\Cache;
use Friendica\Core\Config\Configuration;
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
namespace Friendica\Core\Cache;
-use Friendica\Core\Cache;
-
/**
* Trait TraitCompareSetDelete
*
namespace Friendica\Core\Cache;
-use Friendica\Core\Cache;
-
/**
* Trait TraitCompareSetDelete
*
+++ /dev/null
-<?php
-
-namespace Friendica\Core\Lock;
-
-/**
- * Class AbstractLock
- *
- * @package Friendica\Core\Lock
- *
- * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
- */
-abstract class AbstractLock implements ILock
-{
- /**
- * @var array The local acquired locks
- */
- protected $acquiredLocks = [];
-
- /**
- * Check if we've locally acquired a lock
- *
- * @param string key The Name of the lock
- *
- * @return bool Returns true if the lock is set
- */
- protected function hasAcquiredLock($key)
- {
- return isset($this->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;
- }
-}
use Friendica\Core\Cache;
use Friendica\Core\Cache\IMemoryCache;
-class CacheLock extends AbstractLock
+class CacheLock extends Lock
{
/**
* @var \Friendica\Core\Cache\ICache;
/**
* Locking driver that stores the locks in the database
*/
-class DatabaseLock extends AbstractLock
+class DatabaseLock extends Lock
{
/**
* The current ID of the process
--- /dev/null
+<?php
+
+namespace Friendica\Core\Lock;
+
+/**
+ * Class AbstractLock
+ *
+ * @package Friendica\Core\Lock
+ *
+ * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
+ */
+abstract class Lock implements ILock
+{
+ /**
+ * @var array The local acquired locks
+ */
+ protected $acquiredLocks = [];
+
+ /**
+ * Check if we've locally acquired a lock
+ *
+ * @param string key The Name of the lock
+ *
+ * @return bool Returns true if the lock is set
+ */
+ protected function hasAcquiredLock($key)
+ {
+ return isset($this->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;
+ }
+}
use Friendica\Core\Cache;
-class SemaphoreLock extends AbstractLock
+class SemaphoreLock extends Lock
{
private static $semaphore = [];
/**
* @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
}
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:
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;
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);
// 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) {