mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-22 21:54:15 +02:00
Refactored CacheFactory
This commit is contained in:
@@ -5,6 +5,8 @@ namespace App\Factories;
|
||||
use App\Config;
|
||||
use App\Exceptions\InvalidConfiguration;
|
||||
use DI\Container;
|
||||
use Memcached;
|
||||
use Redis;
|
||||
use Symfony\Component\Cache\Adapter\ApcuAdapter;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
@@ -37,50 +39,64 @@ class CacheFactory
|
||||
/** Initialize and return a CacheInterface. */
|
||||
public function __invoke(): CacheInterface
|
||||
{
|
||||
switch ($this->config->get('cache_driver')) {
|
||||
case 'apcu':
|
||||
return new ApcuAdapter(
|
||||
self::NAMESPACE_EXTERNAL,
|
||||
$this->config->get('cache_lifetime')
|
||||
);
|
||||
return match ($this->config->get('cache_driver')) {
|
||||
'apcu' => $this->getApcuAdapter(),
|
||||
'array' => $this->getArrayAdapter(),
|
||||
'file' => $this->getFilesystemAdapter(),
|
||||
'memcached' => $this->getMemcachedAdapter(),
|
||||
'php-file' => $this->getPhpFilesAdapter(),
|
||||
'redis' => $this->getRedisAdapter(),
|
||||
default => throw InvalidConfiguration::fromConfig('cache_driver', $this->config->get('cache_driver'))
|
||||
};
|
||||
}
|
||||
|
||||
case 'array':
|
||||
return new ArrayAdapter($this->config->get('cache_lifetime'));
|
||||
private function getApcuAdapter(): ApcuAdapter
|
||||
{
|
||||
return new ApcuAdapter(self::NAMESPACE_EXTERNAL, $this->config->get('cache_lifetime'));
|
||||
}
|
||||
|
||||
case 'file':
|
||||
return new FilesystemAdapter(
|
||||
self::NAMESPACE_INTERNAL,
|
||||
$this->config->get('cache_lifetime'),
|
||||
$this->config->get('cache_path')
|
||||
);
|
||||
private function getArrayAdapter(): ArrayAdapter
|
||||
{
|
||||
return new ArrayAdapter($this->config->get('cache_lifetime'));
|
||||
}
|
||||
|
||||
case 'memcached':
|
||||
$this->container->call('memcached_config', [$memcached = new \Memcached]);
|
||||
private function getFilesystemAdapter(): FilesystemAdapter
|
||||
{
|
||||
return new FilesystemAdapter(
|
||||
self::NAMESPACE_INTERNAL,
|
||||
$this->config->get('cache_lifetime'),
|
||||
$this->config->get('cache_path')
|
||||
);
|
||||
}
|
||||
|
||||
return new MemcachedAdapter(
|
||||
$memcached,
|
||||
self::NAMESPACE_EXTERNAL,
|
||||
$this->config->get('cache_lifetime')
|
||||
);
|
||||
private function getMemcachedAdapter(): MemcachedAdapter
|
||||
{
|
||||
$this->container->call('memcached_config', [$memcached = new Memcached]);
|
||||
|
||||
case 'php-file':
|
||||
return new PhpFilesAdapter(
|
||||
self::NAMESPACE_INTERNAL,
|
||||
$this->config->get('cache_lifetime'),
|
||||
$this->config->get('cache_path')
|
||||
);
|
||||
return new MemcachedAdapter(
|
||||
$memcached,
|
||||
self::NAMESPACE_EXTERNAL,
|
||||
$this->config->get('cache_lifetime')
|
||||
);
|
||||
}
|
||||
|
||||
case 'redis':
|
||||
$this->container->call('redis_config', [$redis = new \Redis]);
|
||||
private function getPhpFilesAdapter(): PhpFilesAdapter
|
||||
{
|
||||
return new PhpFilesAdapter(
|
||||
self::NAMESPACE_INTERNAL,
|
||||
$this->config->get('cache_lifetime'),
|
||||
$this->config->get('cache_path')
|
||||
);
|
||||
}
|
||||
|
||||
return new RedisAdapter(
|
||||
$redis,
|
||||
self::NAMESPACE_EXTERNAL,
|
||||
$this->config->get('cache_lifetime')
|
||||
);
|
||||
private function getRedisAdapter(): RedisAdapter
|
||||
{
|
||||
$this->container->call('redis_config', [$redis = new Redis]);
|
||||
|
||||
default:
|
||||
throw InvalidConfiguration::fromConfig('cache_driver', $this->config->get('cache_driver'));
|
||||
}
|
||||
return new RedisAdapter(
|
||||
$redis,
|
||||
self::NAMESPACE_EXTERNAL,
|
||||
$this->config->get('cache_lifetime')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user