Skip to content

Commit ee63587

Browse files
committed
Added support for HierarchicalPoolInterface
1 parent 75eacb4 commit ee63587

File tree

6 files changed

+73
-56
lines changed

6 files changed

+73
-56
lines changed

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: 17 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 <[email protected]>
2021
* @author Tobias Nyholm <[email protected]>
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,25 @@ protected function clearAllObjectsFromCache()
6149

6250
protected function clearOneObjectFromCache($key)
6351
{
64-
return $this->cache->del($key) >= 0;
52+
$keyString = $this->getHierarchyKey($key, $path);
53+
$this->cache->incr($path);
54+
$this->clearHierarchyKeyCache();
55+
56+
return $this->cache->del($keyString) >= 0;
6557
}
6658

6759
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
6860
{
61+
$key = $this->getHierarchyKey($key);
6962
if ($ttl === null) {
7063
return 'OK' === $this->cache->set($key, serialize($item))->getPayload();
7164
}
7265

7366
return 'OK' === $this->cache->setex($key, $ttl, serialize($item))->getPayload();
7467
}
68+
69+
protected function getValueFormStore($key)
70+
{
71+
return $this->cache->get($key);
72+
}
7573
}

tests/CreatePoolTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Cache\Adapter\Predis\Tests;
4+
5+
use Cache\Adapter\Predis\PredisCachePool;
6+
use Predis\Client;
7+
8+
trait CreatePoolTrait
9+
{
10+
private $client = null;
11+
12+
public function createCachePool()
13+
{
14+
return new PredisCachePool($this->getClient());
15+
}
16+
17+
private function getClient()
18+
{
19+
if ($this->client === null) {
20+
$this->client = new Client('tcp:/127.0.0.1:6379');
21+
}
22+
23+
return $this->client;
24+
}
25+
}

tests/IntegrationHierarchyTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\predis-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\Predis\Tests;
13+
14+
use Cache\Adapter\Predis\PredisCachePool;
15+
use Cache\IntegrationTests\HierarchicalCachePoolTest;
16+
use Predis\Client;
17+
18+
class IntegrationHierarchyTest extends HierarchicalCachePoolTest
19+
{
20+
use CreatePoolTrait;
21+
}

tests/IntegrationPoolTest.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,10 @@
1212
namespace Cache\Adapter\Predis\Tests;
1313

1414
use Cache\Adapter\Predis\PredisCachePool;
15-
use Cache\IntegrationTests\CachePoolTest as BaseTest;
15+
use Cache\IntegrationTests\CachePoolTest;
1616
use Predis\Client;
1717

18-
class IntegrationPoolTest extends BaseTest
18+
class IntegrationPoolTest extends CachePoolTest
1919
{
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-
}
20+
use CreatePoolTrait;
3521
}

tests/IntegrationTagTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,5 @@
1717

1818
class IntegrationTagTest extends TaggableCachePoolTest
1919
{
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-
}
20+
use CreatePoolTrait;
3521
}

0 commit comments

Comments
 (0)