Skip to content

Commit 685889f

Browse files
committed
Merge pull request #7 from Nyholm/hierarchy
Added support for HierarchicalPoolInterface
2 parents 996cdc8 + 2673ec2 commit 685889f

6 files changed

Lines changed: 83 additions & 60 deletions

File tree

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
],
2929
"require":
3030
{
31-
"php": "^5.5|^7.0",
32-
"psr/cache": "1.0.0",
33-
"cache/adapter-common": "^0.1.2",
34-
"cache/taggable-cache": "^0.2",
35-
"predis/predis": "^1.0"
31+
"php": "^5.5|^7.0",
32+
"psr/cache": "1.0.0",
33+
"cache/adapter-common": "^0.1.2",
34+
"cache/taggable-cache": "^0.3",
35+
"cache/hierarchical-cache": "^0.1",
36+
"predis/predis": "^1.0"
3637
},
3738
"require-dev":
3839
{

src/PredisCachePool.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
namespace Cache\Adapter\Predis;
1313

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

1820
/**
19-
* @author Aaron Scherer <aequasi@gmail.com>
2021
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2122
*/
22-
class PredisCachePool extends AbstractCachePool
23+
class PredisCachePool extends AbstractCachePool implements HierarchicalPoolInterface
2324
{
25+
use HierarchicalCachePoolTrait;
26+
2427
/**
2528
* @type Client
2629
*/
@@ -34,24 +37,9 @@ public function __construct(Client $cache)
3437
$this->cache = $cache;
3538
}
3639

37-
/**
38-
* {@inheritdoc}
39-
*/
40-
public function hasItem($key, array $tags = [])
41-
{
42-
$this->validateKey($key);
43-
$taggedKey = $this->generateCacheKey($key, $tags);
44-
45-
if (isset($this->deferred[$key])) {
46-
return true;
47-
}
48-
49-
return $this->cache->exists($taggedKey);
50-
}
51-
5240
protected function fetchObjectFromCache($key)
5341
{
54-
return unserialize($this->cache->get($key));
42+
return unserialize($this->cache->get($this->getHierarchyKey($key)));
5543
}
5644

5745
protected function clearAllObjectsFromCache()
@@ -61,15 +49,28 @@ protected function clearAllObjectsFromCache()
6149

6250
protected function clearOneObjectFromCache($key)
6351
{
64-
return $this->cache->del($key) >= 0;
52+
// We have to commit here to be able to remove deferred hierarchy items
53+
$this->commit();
54+
55+
$keyString = $this->getHierarchyKey($key, $path);
56+
$this->cache->incr($path);
57+
$this->clearHierarchyKeyCache();
58+
59+
return $this->cache->del($keyString) >= 0;
6560
}
6661

6762
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
6863
{
64+
$key = $this->getHierarchyKey($key);
6965
if ($ttl === null) {
7066
return 'OK' === $this->cache->set($key, serialize($item))->getPayload();
7167
}
7268

7369
return 'OK' === $this->cache->setex($key, $ttl, serialize($item))->getPayload();
7470
}
71+
72+
protected function getValueFormStore($key)
73+
{
74+
return $this->cache->get($key);
75+
}
7576
}

tests/CreatePoolTrait.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\predis-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
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\Predis\Tests;
13+
14+
use Cache\Adapter\Predis\PredisCachePool;
15+
use Predis\Client;
16+
17+
trait CreatePoolTrait
18+
{
19+
private $client = null;
20+
21+
public function createCachePool()
22+
{
23+
return new PredisCachePool($this->getClient());
24+
}
25+
26+
private function getClient()
27+
{
28+
if ($this->client === null) {
29+
$this->client = new Client('tcp:/127.0.0.1:6379');
30+
}
31+
32+
return $this->client;
33+
}
34+
}

tests/IntegrationHierarchyTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\predis-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
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\Predis\Tests;
13+
14+
use Cache\IntegrationTests\HierarchicalCachePoolTest;
15+
16+
class IntegrationHierarchyTest extends HierarchicalCachePoolTest
17+
{
18+
use CreatePoolTrait;
19+
}

tests/IntegrationPoolTest.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,9 @@
1111

1212
namespace Cache\Adapter\Predis\Tests;
1313

14-
use Cache\Adapter\Predis\PredisCachePool;
15-
use Cache\IntegrationTests\CachePoolTest as BaseTest;
16-
use Predis\Client;
14+
use Cache\IntegrationTests\CachePoolTest;
1715

18-
class IntegrationPoolTest extends BaseTest
16+
class IntegrationPoolTest extends CachePoolTest
1917
{
20-
private $client = null;
21-
22-
public function createCachePool()
23-
{
24-
return new PredisCachePool($this->getClient());
25-
}
26-
27-
private function getClient()
28-
{
29-
if ($this->client === null) {
30-
$this->client = new Client('tcp:/127.0.0.1:6379');
31-
}
32-
33-
return $this->client;
34-
}
18+
use CreatePoolTrait;
3519
}

tests/IntegrationTagTest.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,9 @@
1111

1212
namespace Cache\Adapter\Predis\Tests;
1313

14-
use Cache\Adapter\Predis\PredisCachePool;
1514
use Cache\IntegrationTests\TaggableCachePoolTest;
16-
use Predis\Client;
1715

1816
class IntegrationTagTest extends TaggableCachePoolTest
1917
{
20-
private $client = null;
21-
22-
public function createCachePool()
23-
{
24-
return new PredisCachePool($this->getClient());
25-
}
26-
27-
private function getClient()
28-
{
29-
if ($this->client === null) {
30-
$this->client = new Client('tcp:/127.0.0.1:6379');
31-
}
32-
33-
return $this->client;
34-
}
18+
use CreatePoolTrait;
3519
}

0 commit comments

Comments
 (0)