Restructure Cache to follow new paradigm

This commit is contained in:
Philipp 2021-10-23 10:49:27 +02:00
parent 6a85d09c59
commit 68046573a4
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
52 changed files with 103 additions and 71 deletions

View file

@ -19,7 +19,7 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Enum;
/**
* Enumeration for cache durations

View file

@ -19,7 +19,7 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Enum;
/**
* Enumeration for cache types

View file

@ -0,0 +1,120 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Cache\Factory;
use Friendica\App\BaseURL;
use Friendica\Core\Cache;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Config\IConfig;
use Friendica\Database\Database;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Class CacheFactory
*
* @package Friendica\Core\Cache
*
* A basic class to generate a CacheDriver
*/
class CacheFactory
{
/**
* @var string The default cache if nothing set
*/
const DEFAULT_TYPE = Cache\Enum\Type::DATABASE;
/**
* @var IConfig The IConfiguration to read parameters out of the config
*/
private $config;
/**
* @var Database The database connection in case that the cache is used the dba connection
*/
private $dba;
/**
* @var string The hostname, used as Prefix for Caching
*/
private $hostname;
/**
* @var Profiler The optional profiler if the cached should be profiled
*/
private $profiler;
/**
* @var LoggerInterface The Friendica Logger
*/
private $logger;
public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
{
$this->hostname = $baseURL->getHostname();
$this->config = $config;
$this->dba = $dba;
$this->profiler = $profiler;
$this->logger = $logger;
}
/**
* This method creates a CacheDriver for the given cache driver name
*
* @param string $type The cache type to create (default is per config)
*
* @return ICache The instance of the CacheDriver
* @throws \Exception The exception if something went wrong during the CacheDriver creation
*/
public function create(string $type = null)
{
if (empty($type)) {
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
}
switch ($type) {
case Cache\Enum\Type::MEMCACHE:
$cache = new Cache\Type\MemcacheCache($this->hostname, $this->config);
break;
case Cache\Enum\Type::MEMCACHED:
$cache = new Cache\Type\MemcachedCache($this->hostname, $this->config, $this->logger);
break;
case Cache\Enum\Type::REDIS:
$cache = new Cache\Type\RedisCache($this->hostname, $this->config);
break;
case Cache\Enum\Type::APCU:
$cache = new Cache\Type\APCuCache($this->hostname);
break;
default:
$cache = new Cache\Type\DatabaseCache($this->hostname, $this->dba);
}
$profiling = $this->config->get('system', 'profiling', false);
// In case profiling is enabled, wrap the ProfilerCache around the current cache
if (isset($profiling) && $profiling !== false) {
return new Cache\Type\ProfilerCache($cache, $this->profiler);
} else {
return $cache;
}
}
}

View file

@ -21,6 +21,8 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache\Enum\Duration;
/**
* Cache Interface
*/

View file

@ -21,6 +21,8 @@
namespace Friendica\Core\Cache;
use Friendica\Core\Cache\Enum\Duration;
/**
* This interface defines methods for Memory-Caches only
*/

View file

@ -19,10 +19,14 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Exception;
use Friendica\Core\BaseCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Type\TraitCompareDelete;
use Friendica\Core\Cache\Type\TraitCompareSet;
use Friendica\Core\Cache\Enum\Type;
/**
* APCu Cache.

View file

@ -19,9 +19,12 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Friendica\Core\BaseCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Type\TraitCompareDelete;
use Friendica\Core\Cache\Enum\Type;
/**
* Implementation of the IMemoryCache mainly for testing purpose

View file

@ -0,0 +1,108 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\ICache;
/**
* Abstract class for common used functions
*/
abstract class BaseCache implements ICache
{
/**
* @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;
}
}
}

View file

@ -19,11 +19,13 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;
use Friendica\Core\BaseCache;
/**
* Database Cache

View file

@ -19,10 +19,15 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Exception;
use Friendica\Core\BaseCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Type\TraitCompareDelete;
use Friendica\Core\Cache\Type\TraitCompareSet;
use Friendica\Core\Cache\Type\TraitMemcacheCommand;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Config\IConfig;
use Memcache;

View file

@ -19,10 +19,15 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Exception;
use Friendica\Core\BaseCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Type\TraitCompareDelete;
use Friendica\Core\Cache\Type\TraitCompareSet;
use Friendica\Core\Cache\Type\TraitMemcacheCommand;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Config\IConfig;
use Memcached;
use Psr\Log\LoggerInterface;

View file

@ -19,9 +19,11 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Friendica\Core\System;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Util\Profiler;
/**

View file

@ -19,10 +19,12 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Exception;
use Friendica\Core\BaseCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Config\IConfig;
use Redis;

View file

@ -19,7 +19,9 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
/**
* Trait TraitCompareSetDelete

View file

@ -19,7 +19,9 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
/**
* Trait TraitCompareSetDelete

View file

@ -19,7 +19,7 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Type;
use Friendica\Network\HTTPException\InternalServerErrorException;