diff --git a/composer.json b/composer.json index 6ac4bb2..be8a1db 100644 --- a/composer.json +++ b/composer.json @@ -30,13 +30,14 @@ "php": "^5.5|^7.0", "ext-memcached": "*", "psr/cache": "1.0.0", - "cache/adapter-common": "^0.1", - "cache/taggable-cache": "^0.2" + "cache/adapter-common": "^0.2", + "cache/taggable-cache": "^0.3", + "cache/hierarchical-cache": "^0.2" }, "require-dev": { "phpunit/phpunit": "^5.1|^4.0", - "cache/integration-tests": "dev-master" + "cache/integration-tests": "^0.6" }, "provide": { diff --git a/src/MemcachedCachePool.php b/src/MemcachedCachePool.php index 41276a4..54e27af 100644 --- a/src/MemcachedCachePool.php +++ b/src/MemcachedCachePool.php @@ -12,14 +12,18 @@ namespace Cache\Adapter\Memcached; use Cache\Adapter\Common\AbstractCachePool; +use Cache\Hierarchy\HierarchicalCachePoolTrait; +use Cache\Hierarchy\HierarchicalPoolInterface; use Psr\Cache\CacheItemInterface; /** * @author Aaron Scherer * @author Tobias Nyholm */ -class MemcachedCachePool extends AbstractCachePool +class MemcachedCachePool extends AbstractCachePool implements HierarchicalPoolInterface { + use HierarchicalCachePoolTrait; + /** * @type \Memcached */ @@ -31,11 +35,16 @@ class MemcachedCachePool extends AbstractCachePool public function __construct(\Memcached $cache) { $this->cache = $cache; + $this->cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); } protected function fetchObjectFromCache($key) { - return $this->cache->get($key); + if (false === $result = unserialize($this->cache->get($this->getHierarchyKey($key)))) { + return [false, null]; + } + + return $result; } protected function clearAllObjectsFromCache() @@ -45,6 +54,11 @@ protected function clearAllObjectsFromCache() protected function clearOneObjectFromCache($key) { + $this->commit(); + $key = $this->getHierarchyKey($key, $path); + $this->cache->increment($path, 1, 0); + $this->clearHierarchyKeyCache(); + if ($this->cache->delete($key)) { return true; } @@ -59,6 +73,13 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl) $ttl = 0; } - return $this->cache->set($key, $item, $ttl); + $key = $this->getHierarchyKey($key); + + return $this->cache->set($key, serialize([true, $item->get()]), $ttl); + } + + protected function getValueFormStore($key) + { + return $this->cache->get($key); } } diff --git a/tests/CreatePoolTrait.php b/tests/CreatePoolTrait.php new file mode 100644 index 0000000..07f2ace --- /dev/null +++ b/tests/CreatePoolTrait.php @@ -0,0 +1,34 @@ +, Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Cache\Adapter\Memcached\Tests; + +use Cache\Adapter\Memcached\MemcachedCachePool; + +trait CreatePoolTrait +{ + private $client = null; + + public function createCachePool() + { + return new MemcachedCachePool($this->getClient()); + } + + private function getClient() + { + if ($this->client === null) { + $this->client = new \Memcached(); + $this->client->addServer('localhost', 11211); + } + + return $this->client; + } +} diff --git a/tests/IntegrationHierarchyTest.php b/tests/IntegrationHierarchyTest.php new file mode 100644 index 0000000..85f2270 --- /dev/null +++ b/tests/IntegrationHierarchyTest.php @@ -0,0 +1,19 @@ +, Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Cache\Adapter\Memcached\Tests; + +use Cache\IntegrationTests\HierarchicalCachePoolTest; + +class IntegrationHierarchyTest extends HierarchicalCachePoolTest +{ + use CreatePoolTrait; +} diff --git a/tests/IntegrationPoolTest.php b/tests/IntegrationPoolTest.php index 4a9186a..e311252 100644 --- a/tests/IntegrationPoolTest.php +++ b/tests/IntegrationPoolTest.php @@ -9,27 +9,11 @@ * with this source code in the file LICENSE. */ -namespace Cache\Adapter\Redis\Tests; +namespace Cache\Adapter\Memcached\Tests; -use Cache\Adapter\Memcached\MemcachedCachePool; use Cache\IntegrationTests\CachePoolTest as BaseTest; class IntegrationPoolTest extends BaseTest { - private $client = null; - - public function createCachePool() - { - return new MemcachedCachePool($this->getClient()); - } - - private function getClient() - { - if ($this->client === null) { - $this->client = new \Memcached(); - $this->client->addServer('localhost', 11211); - } - - return $this->client; - } + use CreatePoolTrait; } diff --git a/tests/IntegrationTagTest.php b/tests/IntegrationTagTest.php index 634a4c5..3e0572f 100644 --- a/tests/IntegrationTagTest.php +++ b/tests/IntegrationTagTest.php @@ -9,27 +9,11 @@ * with this source code in the file LICENSE. */ -namespace Cache\Adapter\Redis\Tests; +namespace Cache\Adapter\Memcached\Tests; -use Cache\Adapter\Memcached\MemcachedCachePool; use Cache\IntegrationTests\TaggableCachePoolTest; class IntegrationTagTest extends TaggableCachePoolTest { - private $client = null; - - public function createCachePool() - { - return new MemcachedCachePool($this->getClient()); - } - - private function getClient() - { - if ($this->client === null) { - $this->client = new \Memcached(); - $this->client->addServer('localhost', 11211); - } - - return $this->client; - } + use CreatePoolTrait; }