Skip to content

Commit 23bdbb4

Browse files
committed
Merge pull request #8 from Nyholm/updates
Updates to adapter-common 0.2
2 parents ea652b1 + 3191feb commit 23bdbb4

6 files changed

+85
-42
lines changed

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
"php": "^5.5|^7.0",
3131
"ext-memcached": "*",
3232
"psr/cache": "1.0.0",
33-
"cache/adapter-common": "^0.1",
34-
"cache/taggable-cache": "^0.2"
33+
"cache/adapter-common": "^0.2",
34+
"cache/taggable-cache": "^0.3",
35+
"cache/hierarchical-cache": "^0.2"
3536
},
3637
"require-dev":
3738
{
3839
"phpunit/phpunit": "^5.1|^4.0",
39-
"cache/integration-tests": "dev-master"
40+
"cache/integration-tests": "^0.6"
4041
},
4142
"provide":
4243
{

src/MemcachedCachePool.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
namespace Cache\Adapter\Memcached;
1313

1414
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Hierarchy\HierarchicalCachePoolTrait;
16+
use Cache\Hierarchy\HierarchicalPoolInterface;
1517
use Psr\Cache\CacheItemInterface;
1618

1719
/**
1820
* @author Aaron Scherer <[email protected]>
1921
* @author Tobias Nyholm <[email protected]>
2022
*/
21-
class MemcachedCachePool extends AbstractCachePool
23+
class MemcachedCachePool extends AbstractCachePool implements HierarchicalPoolInterface
2224
{
25+
use HierarchicalCachePoolTrait;
26+
2327
/**
2428
* @type \Memcached
2529
*/
@@ -31,11 +35,16 @@ class MemcachedCachePool extends AbstractCachePool
3135
public function __construct(\Memcached $cache)
3236
{
3337
$this->cache = $cache;
38+
$this->cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
3439
}
3540

3641
protected function fetchObjectFromCache($key)
3742
{
38-
return $this->cache->get($key);
43+
if (false === $result = unserialize($this->cache->get($this->getHierarchyKey($key)))) {
44+
return [false, null];
45+
}
46+
47+
return $result;
3948
}
4049

4150
protected function clearAllObjectsFromCache()
@@ -45,6 +54,11 @@ protected function clearAllObjectsFromCache()
4554

4655
protected function clearOneObjectFromCache($key)
4756
{
57+
$this->commit();
58+
$key = $this->getHierarchyKey($key, $path);
59+
$this->cache->increment($path, 1, 0);
60+
$this->clearHierarchyKeyCache();
61+
4862
if ($this->cache->delete($key)) {
4963
return true;
5064
}
@@ -59,6 +73,13 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
5973
$ttl = 0;
6074
}
6175

62-
return $this->cache->set($key, $item, $ttl);
76+
$key = $this->getHierarchyKey($key);
77+
78+
return $this->cache->set($key, serialize([true, $item->get()]), $ttl);
79+
}
80+
81+
protected function getValueFormStore($key)
82+
{
83+
return $this->cache->get($key);
6384
}
6485
}

tests/CreatePoolTrait.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\memcached-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Memcached\Tests;
13+
14+
use Cache\Adapter\Memcached\MemcachedCachePool;
15+
16+
trait CreatePoolTrait
17+
{
18+
private $client = null;
19+
20+
public function createCachePool()
21+
{
22+
return new MemcachedCachePool($this->getClient());
23+
}
24+
25+
private function getClient()
26+
{
27+
if ($this->client === null) {
28+
$this->client = new \Memcached();
29+
$this->client->addServer('localhost', 11211);
30+
}
31+
32+
return $this->client;
33+
}
34+
}

tests/IntegrationHierarchyTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\memcached-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Memcached\Tests;
13+
14+
use Cache\IntegrationTests\HierarchicalCachePoolTest;
15+
16+
class IntegrationHierarchyTest extends HierarchicalCachePoolTest
17+
{
18+
use CreatePoolTrait;
19+
}

tests/IntegrationPoolTest.php

+2-18
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,11 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\Adapter\Redis\Tests;
12+
namespace Cache\Adapter\Memcached\Tests;
1313

14-
use Cache\Adapter\Memcached\MemcachedCachePool;
1514
use Cache\IntegrationTests\CachePoolTest as BaseTest;
1615

1716
class IntegrationPoolTest extends BaseTest
1817
{
19-
private $client = null;
20-
21-
public function createCachePool()
22-
{
23-
return new MemcachedCachePool($this->getClient());
24-
}
25-
26-
private function getClient()
27-
{
28-
if ($this->client === null) {
29-
$this->client = new \Memcached();
30-
$this->client->addServer('localhost', 11211);
31-
}
32-
33-
return $this->client;
34-
}
18+
use CreatePoolTrait;
3519
}

tests/IntegrationTagTest.php

+2-18
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,11 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\Adapter\Redis\Tests;
12+
namespace Cache\Adapter\Memcached\Tests;
1313

14-
use Cache\Adapter\Memcached\MemcachedCachePool;
1514
use Cache\IntegrationTests\TaggableCachePoolTest;
1615

1716
class IntegrationTagTest extends TaggableCachePoolTest
1817
{
19-
private $client = null;
20-
21-
public function createCachePool()
22-
{
23-
return new MemcachedCachePool($this->getClient());
24-
}
25-
26-
private function getClient()
27-
{
28-
if ($this->client === null) {
29-
$this->client = new \Memcached();
30-
$this->client->addServer('localhost', 11211);
31-
}
32-
33-
return $this->client;
34-
}
18+
use CreatePoolTrait;
3519
}

0 commit comments

Comments
 (0)