Skip to content

Commit

Permalink
Merge pull request #8 from Nyholm/updates
Browse files Browse the repository at this point in the history
Updates to adapter-common 0.2
  • Loading branch information
Nyholm committed Jan 19, 2016
2 parents ea652b1 + 3191feb commit 23bdbb4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 42 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":
{
Expand Down
27 changes: 24 additions & 3 deletions src/MemcachedCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class MemcachedCachePool extends AbstractCachePool
class MemcachedCachePool extends AbstractCachePool implements HierarchicalPoolInterface
{
use HierarchicalCachePoolTrait;

/**
* @type \Memcached
*/
Expand All @@ -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()
Expand All @@ -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;
}
Expand All @@ -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);
}
}
34 changes: 34 additions & 0 deletions tests/CreatePoolTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of php-cache\memcached-adapter package.
*
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* 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;
}
}
19 changes: 19 additions & 0 deletions tests/IntegrationHierarchyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of php-cache\memcached-adapter package.
*
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* 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;
}
20 changes: 2 additions & 18 deletions tests/IntegrationPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 2 additions & 18 deletions tests/IntegrationTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 23bdbb4

Please sign in to comment.