mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Improved CacheFactory supporting more adapters
This commit is contained in:
parent
9ee2064ba1
commit
090479fa62
@ -1,8 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Shlinkio\Shlink\Common\Factory;
|
namespace Shlinkio\Shlink\Common\Factory;
|
||||||
|
|
||||||
use Doctrine\Common\Cache\ApcuCache;
|
use Doctrine\Common\Cache;
|
||||||
use Doctrine\Common\Cache\ArrayCache;
|
|
||||||
use Interop\Container\ContainerInterface;
|
use Interop\Container\ContainerInterface;
|
||||||
use Interop\Container\Exception\ContainerException;
|
use Interop\Container\Exception\ContainerException;
|
||||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||||
@ -12,8 +11,11 @@ use Zend\ServiceManager\Factory\FactoryInterface;
|
|||||||
class CacheFactory implements FactoryInterface
|
class CacheFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
const VALID_CACHE_ADAPTERS = [
|
const VALID_CACHE_ADAPTERS = [
|
||||||
ApcuCache::class,
|
Cache\ApcuCache::class,
|
||||||
ArrayCache::class,
|
Cache\ArrayCache::class,
|
||||||
|
Cache\FilesystemCache::class,
|
||||||
|
Cache\PhpFileCache::class,
|
||||||
|
Cache\MemcachedCache::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,10 +38,44 @@ class CacheFactory implements FactoryInterface
|
|||||||
&& isset($config['cache']['adapter'])
|
&& isset($config['cache']['adapter'])
|
||||||
&& in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)
|
&& in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)
|
||||||
) {
|
) {
|
||||||
return new $config['cache']['adapter']();
|
return $this->resolveCacheAdapter($config['cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the adapter has not been set in config, create one based on environment
|
// If the adapter has not been set in config, create one based on environment
|
||||||
return env('APP_ENV', 'pro') === 'pro' ? new ApcuCache() : new ArrayCache();
|
return env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $cacheConfig
|
||||||
|
* @return Cache\Cache
|
||||||
|
*/
|
||||||
|
protected function resolveCacheAdapter(array $cacheConfig)
|
||||||
|
{
|
||||||
|
switch ($cacheConfig['adapter']) {
|
||||||
|
case Cache\ArrayCache::class:
|
||||||
|
case Cache\ApcuCache::class:
|
||||||
|
return new $cacheConfig['adapter']();
|
||||||
|
case Cache\FilesystemCache::class:
|
||||||
|
case Cache\PhpFileCache::class:
|
||||||
|
return new $cacheConfig['adapter']($cacheConfig['options']['dir']);
|
||||||
|
case Cache\MemcachedCache::class:
|
||||||
|
$memcached = new \Memcached();
|
||||||
|
$servers = isset($cacheConfig['options']['servers']) ? $cacheConfig['options']['servers'] : [];
|
||||||
|
|
||||||
|
foreach ($servers as $server) {
|
||||||
|
if (! isset($server['host'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$port = isset($server['port']) ? intval($server['port']) : 11211;
|
||||||
|
|
||||||
|
$memcached->addServer($server['host'], $port);
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache = new Cache\MemcachedCache();
|
||||||
|
$cache->setMemcached($memcached);
|
||||||
|
return $cache;
|
||||||
|
default:
|
||||||
|
return new Cache\ArrayCache();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ namespace ShlinkioTest\Shlink\Common\Factory;
|
|||||||
use Doctrine\Common\Cache\ApcuCache;
|
use Doctrine\Common\Cache\ApcuCache;
|
||||||
use Doctrine\Common\Cache\ArrayCache;
|
use Doctrine\Common\Cache\ArrayCache;
|
||||||
use Doctrine\Common\Cache\FilesystemCache;
|
use Doctrine\Common\Cache\FilesystemCache;
|
||||||
|
use Doctrine\Common\Cache\MemcachedCache;
|
||||||
|
use Doctrine\Common\Cache\RedisCache;
|
||||||
use PHPUnit_Framework_TestCase as TestCase;
|
use PHPUnit_Framework_TestCase as TestCase;
|
||||||
use Shlinkio\Shlink\Common\Factory\CacheFactory;
|
use Shlinkio\Shlink\Common\Factory\CacheFactory;
|
||||||
use Zend\ServiceManager\ServiceManager;
|
use Zend\ServiceManager\ServiceManager;
|
||||||
@ -61,15 +63,51 @@ class CacheFactoryTest extends TestCase
|
|||||||
public function invalidAdapterDefinedInConfigFallbacksToEnvironment()
|
public function invalidAdapterDefinedInConfigFallbacksToEnvironment()
|
||||||
{
|
{
|
||||||
putenv('APP_ENV=pro');
|
putenv('APP_ENV=pro');
|
||||||
$instance = $this->factory->__invoke($this->createSM(FilesystemCache::class), '');
|
$instance = $this->factory->__invoke($this->createSM(RedisCache::class), '');
|
||||||
$this->assertInstanceOf(ApcuCache::class, $instance);
|
$this->assertInstanceOf(ApcuCache::class, $instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSM($cacheAdapter = null)
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function filesystemCacheAdaptersReadDirOption()
|
||||||
|
{
|
||||||
|
$dir = sys_get_temp_dir();
|
||||||
|
/** @var FilesystemCache $instance */
|
||||||
|
$instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), '');
|
||||||
|
$this->assertInstanceOf(FilesystemCache::class, $instance);
|
||||||
|
$this->assertEquals($dir, $instance->getDirectory());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function memcachedCacheAdaptersReadServersOption()
|
||||||
|
{
|
||||||
|
$servers = [
|
||||||
|
[
|
||||||
|
'host' => '1.2.3.4',
|
||||||
|
'port' => 123
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'host' => '4.3.2.1',
|
||||||
|
'port' => 321
|
||||||
|
],
|
||||||
|
];
|
||||||
|
/** @var MemcachedCache $instance */
|
||||||
|
$instance = $this->factory->__invoke($this->createSM(MemcachedCache::class, ['servers' => $servers]), '');
|
||||||
|
$this->assertInstanceOf(MemcachedCache::class, $instance);
|
||||||
|
$this->assertEquals($servers, $instance->getMemcached()->getServerList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createSM($cacheAdapter = null, array $options = [])
|
||||||
{
|
{
|
||||||
return new ServiceManager(['services' => [
|
return new ServiceManager(['services' => [
|
||||||
'config' => isset($cacheAdapter) ? [
|
'config' => isset($cacheAdapter) ? [
|
||||||
'cache' => ['adapter' => $cacheAdapter],
|
'cache' => [
|
||||||
|
'adapter' => $cacheAdapter,
|
||||||
|
'options' => $options,
|
||||||
|
],
|
||||||
] : [],
|
] : [],
|
||||||
]]);
|
]]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user