mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Improved CacheFactory so that adapter can be set in config
This commit is contained in:
@@ -11,6 +11,11 @@ use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
class CacheFactory implements FactoryInterface
|
||||
{
|
||||
const VALID_CACHE_ADAPTERS = [
|
||||
ApcuCache::class,
|
||||
ArrayCache::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Create an object
|
||||
*
|
||||
@@ -25,6 +30,16 @@ class CacheFactory implements FactoryInterface
|
||||
*/
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||
{
|
||||
return env('APP_ENV', 'dev') === 'pro' ? new ApcuCache() : new ArrayCache();
|
||||
// Try to get the adapter from config
|
||||
$config = $container->get('config');
|
||||
if (isset($config['cache'])
|
||||
&& isset($config['cache']['adapter'])
|
||||
&& in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)
|
||||
) {
|
||||
return new $config['cache']['adapter']();
|
||||
}
|
||||
|
||||
// If the adapter has not been set in config, create one based on environment
|
||||
return env('APP_ENV', 'pro') === 'pro' ? new ApcuCache() : new ArrayCache();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user