From 090479fa62b09290690c1dd34b03c6fcd367895b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 9 Aug 2016 17:58:47 +0200 Subject: [PATCH] Improved CacheFactory supporting more adapters --- module/Common/src/Factory/CacheFactory.php | 48 ++++++++++++++++--- .../Common/test/Factory/CacheFactoryTest.php | 44 +++++++++++++++-- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/module/Common/src/Factory/CacheFactory.php b/module/Common/src/Factory/CacheFactory.php index c866b980..905ebf56 100644 --- a/module/Common/src/Factory/CacheFactory.php +++ b/module/Common/src/Factory/CacheFactory.php @@ -1,8 +1,7 @@ resolveCacheAdapter($config['cache']); } // 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(); + } } } diff --git a/module/Common/test/Factory/CacheFactoryTest.php b/module/Common/test/Factory/CacheFactoryTest.php index 2e938dfa..607849ac 100644 --- a/module/Common/test/Factory/CacheFactoryTest.php +++ b/module/Common/test/Factory/CacheFactoryTest.php @@ -4,6 +4,8 @@ namespace ShlinkioTest\Shlink\Common\Factory; use Doctrine\Common\Cache\ApcuCache; use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\FilesystemCache; +use Doctrine\Common\Cache\MemcachedCache; +use Doctrine\Common\Cache\RedisCache; use PHPUnit_Framework_TestCase as TestCase; use Shlinkio\Shlink\Common\Factory\CacheFactory; use Zend\ServiceManager\ServiceManager; @@ -61,15 +63,51 @@ class CacheFactoryTest extends TestCase public function invalidAdapterDefinedInConfigFallbacksToEnvironment() { 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); } - 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' => [ 'config' => isset($cacheAdapter) ? [ - 'cache' => ['adapter' => $cacheAdapter], + 'cache' => [ + 'adapter' => $cacheAdapter, + 'options' => $options, + ], ] : [], ]]); }