Move Cache to strategies

This commit is contained in:
Philipp 2023-07-22 23:57:38 +02:00
parent f1da323b07
commit f2c02a79b9
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
12 changed files with 98 additions and 119 deletions

View file

@ -21,16 +21,14 @@
namespace Friendica\Core\Cache\Factory;
use Friendica\App\BaseURL;
use Friendica\Core\Cache\Enum;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Exception\CachePersistenceException;
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
use Friendica\Core\Cache\Type;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Class CacheFactory
@ -45,39 +43,18 @@ class Cache
* @var string The default cache if nothing set
*/
const DEFAULT_TYPE = Enum\Type::DATABASE;
/** @var ICanCreateInstances */
protected $instanceCreator;
/** @var IManageConfigValues */
protected $config;
/** @var Profiler */
protected $profiler;
/**
* @var IManageConfigValues 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, IManageConfigValues $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
public function __construct(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler)
{
$this->hostname = $baseURL->getHost();
$this->config = $config;
$this->dba = $dba;
$this->profiler = $profiler;
$this->logger = $logger;
$this->config = $config;
$this->instanceCreator = $instanceCreator;
$this->profiler = $profiler;
}
/**
@ -92,7 +69,7 @@ class Cache
*/
public function createDistributed(string $type = null): ICanCache
{
if ($type === Enum\Type::APCU) {
if ($type === Type\APCuCache::$NAME) {
throw new InvalidCacheDriverException('apcu doesn\'t support distributed caching.');
}
@ -117,31 +94,17 @@ class Cache
/**
* Creates a new Cache instance
*
* @param string $type The type of cache
* @param string $strategy The strategy, which cache instance should be used
*
* @return ICanCache
*
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
* @throws CachePersistenceException In case the underlying cache has errors during persistence
*/
protected function create(string $type): ICanCache
protected function create(string $strategy): ICanCache
{
switch ($type) {
case Enum\Type::MEMCACHE:
$cache = new Type\MemcacheCache($this->hostname, $this->config);
break;
case Enum\Type::MEMCACHED:
$cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
break;
case Enum\Type::REDIS:
$cache = new Type\RedisCache($this->hostname, $this->config);
break;
case Enum\Type::APCU:
$cache = new Type\APCuCache($this->hostname);
break;
default:
$cache = new Type\DatabaseCache($this->hostname, $this->dba);
}
/** @var ICanCache $cache */
$cache = $this->instanceCreator->create(ICanCache::class, $strategy);
$profiling = $this->config->get('system', 'profiling', false);