From 8b9f41d4f444b281aa86402c00e8babd68a035a3 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Wed, 3 May 2017 09:54:14 +1200 Subject: [PATCH] BUG Fix ApcuCache and MemCache namespace --- src/Core/Cache/ApcuCacheFactory.php | 10 ++- src/Core/Cache/DefaultCacheFactory.php | 3 +- src/Core/Cache/MemcachedCacheFactory.php | 12 ++- tests/php/Core/Cache/CacheTest.php | 88 ++++++++++++++++++++ tests/php/Core/Cache/CacheTest/MockCache.php | 25 ++++++ 5 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 tests/php/Core/Cache/CacheTest.php create mode 100644 tests/php/Core/Cache/CacheTest/MockCache.php diff --git a/src/Core/Cache/ApcuCacheFactory.php b/src/Core/Cache/ApcuCacheFactory.php index f697b67d8..b1f0e9ef5 100644 --- a/src/Core/Cache/ApcuCacheFactory.php +++ b/src/Core/Cache/ApcuCacheFactory.php @@ -27,9 +27,13 @@ class ApcuCacheFactory implements CacheFactory */ public function create($service, array $params = array()) { - return Injector::inst()->create(ApcuCache::class, false, [ - (isset($args['namespace'])) ? $args['namespace'] : '', - (isset($args['defaultLifetime'])) ? $args['defaultLifetime'] : 0, + $namespace = isset($params['namespace']) + ? $params['namespace'] . '_' . md5(BASE_PATH) + : md5(BASE_PATH); + $defaultLifetime = isset($params['defaultLifetime']) ? $params['defaultLifetime'] : 0; + return Injector::inst()->createWithArgs(ApcuCache::class, [ + $namespace, + $defaultLifetime, $this->version ]); } diff --git a/src/Core/Cache/DefaultCacheFactory.php b/src/Core/Cache/DefaultCacheFactory.php index 963fba709..aa2dc80db 100644 --- a/src/Core/Cache/DefaultCacheFactory.php +++ b/src/Core/Cache/DefaultCacheFactory.php @@ -71,7 +71,8 @@ class DefaultCacheFactory implements CacheFactory } // Chain this cache with ApcuCache - $apcu = $this->createCache(ApcuCache::class, [$namespace, (int) $defaultLifetime / 5, $version]); + $apcuNamespace = $namespace . ($namespace ? '_' : '') . md5(BASE_PATH); + $apcu = $this->createCache(ApcuCache::class, [$apcuNamespace, (int) $defaultLifetime / 5, $version]); return $this->createCache(ChainCache::class, [[$apcu, $fs]]); } diff --git a/src/Core/Cache/MemcachedCacheFactory.php b/src/Core/Cache/MemcachedCacheFactory.php index 37269da02..9c161e49b 100644 --- a/src/Core/Cache/MemcachedCacheFactory.php +++ b/src/Core/Cache/MemcachedCacheFactory.php @@ -17,7 +17,7 @@ class MemcachedCacheFactory implements CacheFactory /** * @param Memcached $memcachedClient */ - public function __construct(Memcached $memcachedClient) + public function __construct(Memcached $memcachedClient = null) { $this->memcachedClient = $memcachedClient; } @@ -27,10 +27,14 @@ class MemcachedCacheFactory implements CacheFactory */ public function create($service, array $params = array()) { - return Injector::inst()->create(MemcachedCache::class, false, [ + $namespace = isset($params['namespace']) + ? $params['namespace'] . '_' . md5(BASE_PATH) + : md5(BASE_PATH); + $defaultLifetime = isset($params['defaultLifetime']) ? $params['defaultLifetime'] : 0; + return Injector::inst()->createWithArgs(MemcachedCache::class, [ $this->memcachedClient, - (isset($args['namespace'])) ? $args['namespace'] : '', - (isset($args['defaultLifetime'])) ? $args['defaultLifetime'] : 0 + $namespace, + $defaultLifetime ]); } } diff --git a/tests/php/Core/Cache/CacheTest.php b/tests/php/Core/Cache/CacheTest.php new file mode 100644 index 000000000..393870297 --- /dev/null +++ b/tests/php/Core/Cache/CacheTest.php @@ -0,0 +1,88 @@ +set( + Injector::class, + ApcuCacheFactory::class, + [ + 'constructor' => [ 'version' => 4400 ] + ] + ) + ->set( + Injector::class, + CacheInterface::class . '.TestApcuCache', + [ + 'factory' => ApcuCacheFactory::class, + 'constructor' => [ + 'namespace' => 'TestApcuCache', + 'defaultLifetime' => 2600, + ], + ] + ) + ->set( + Injector::class, + CacheInterface::class . '.TestMemcache', + [ + 'factory' => MemcachedCacheFactory::class, + 'constructor' => [ + 'namespace' => 'TestMemCache', + 'defaultLifetime' => 5600, + ], + ] + ) + ->set(Injector::class, ApcuCache::class, MockCache::class) + ->set(Injector::class, MemcachedCache::class, MockCache::class); + } + + public function testApcuCacheFactory() + { + $cache = Injector::inst()->get(CacheInterface::class .'.TestApcuCache'); + $this->assertInstanceOf( + MockCache::class, + $cache + ); + $this->assertEquals( + [ + 'TestApcuCache_'.md5(BASE_PATH), + 2600, + 4400 + ], + $cache->getArgs() + ); + } + + public function testMemCacheFactory() + { + $cache = Injector::inst()->get(CacheInterface::class .'.TestMemcache'); + $this->assertInstanceOf( + MockCache::class, + $cache + ); + $this->assertEquals( + [ + null, + 'TestMemCache_'.md5(BASE_PATH), + 5600 + ], + $cache->getArgs() + ); + } +} diff --git a/tests/php/Core/Cache/CacheTest/MockCache.php b/tests/php/Core/Cache/CacheTest/MockCache.php new file mode 100644 index 000000000..ce9cd84ae --- /dev/null +++ b/tests/php/Core/Cache/CacheTest/MockCache.php @@ -0,0 +1,25 @@ +args = func_get_args(); + } + + /** + * Get constructor args + * + * @return array + */ + public function getArgs() + { + return $this->args; + } +}