Skip to content

Commit 8c89807

Browse files
committed
Making cache tags more efficient by rewriting the API.
Related to php-cache/issues#21
1 parent 44259cf commit 8c89807

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

PredisCachePool.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
use Cache\Adapter\Common\AbstractCachePool;
1515
use Cache\Hierarchy\HierarchicalCachePoolTrait;
1616
use Cache\Hierarchy\HierarchicalPoolInterface;
17+
use Cache\Taggable\TaggableItemInterface;
18+
use Cache\Taggable\TaggablePoolInterface;
19+
use Cache\Taggable\TaggablePoolTrait;
1720
use Predis\Client;
1821
use Psr\Cache\CacheItemInterface;
1922

2023
/**
2124
* @author Tobias Nyholm <[email protected]>
2225
*/
23-
class PredisCachePool extends AbstractCachePool implements HierarchicalPoolInterface
26+
class PredisCachePool extends AbstractCachePool implements HierarchicalPoolInterface, TaggablePoolInterface
2427
{
28+
use TaggablePoolTrait;
2529
use HierarchicalCachePoolTrait;
2630

2731
/**
@@ -37,10 +41,19 @@ public function __construct(Client $cache)
3741
$this->cache = $cache;
3842
}
3943

44+
public function save(CacheItemInterface $item)
45+
{
46+
if ($item instanceof TaggableItemInterface) {
47+
$this->saveTags($item);
48+
}
49+
50+
return parent::save($item);
51+
}
52+
4053
protected function fetchObjectFromCache($key)
4154
{
4255
if (false === $result = unserialize($this->cache->get($this->getHierarchyKey($key)))) {
43-
return [false, null];
56+
return [false, null, []];
4457
}
4558

4659
return $result;
@@ -56,23 +69,24 @@ protected function clearOneObjectFromCache($key)
5669
// We have to commit here to be able to remove deferred hierarchy items
5770
$this->commit();
5871

72+
$this->preRemoveItem($key);
5973
$keyString = $this->getHierarchyKey($key, $path);
6074
$this->cache->incr($path);
6175
$this->clearHierarchyKeyCache();
6276

6377
return $this->cache->del($keyString) >= 0;
6478
}
6579

66-
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
80+
protected function storeItemInCache(CacheItemInterface $item, $ttl)
6781
{
6882
if ($ttl < 0) {
6983
return false;
7084
}
7185

72-
$key = $this->getHierarchyKey($key);
73-
$data = serialize([true, $item->get()]);
86+
$key = $this->getHierarchyKey($item->getKey());
87+
$data = serialize([true, $item->get(), $item->getTags()]);
7488

75-
if ($ttl === null) {
89+
if ($ttl === null || $ttl === 0) {
7690
return 'OK' === $this->cache->set($key, $data)->getPayload();
7791
}
7892

@@ -83,4 +97,24 @@ protected function getValueFormStore($key)
8397
{
8498
return $this->cache->get($key);
8599
}
100+
101+
protected function appendListItem($name, $value)
102+
{
103+
$this->cache->lpush($name, $value);
104+
}
105+
106+
protected function getList($name)
107+
{
108+
return $this->cache->lrange($name, 0, -1);
109+
}
110+
111+
protected function removeList($name)
112+
{
113+
return $this->cache->del($name);
114+
}
115+
116+
protected function removeListItem($name, $key)
117+
{
118+
return $this->cache->lrem($name, 0, $key);
119+
}
86120
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"require-dev": {
3636
"phpunit/phpunit": "^4.0|^5.1",
37-
"cache/integration-tests": "0.7.0"
37+
"cache/integration-tests": "0.9.0"
3838
},
3939
"provide": {
4040
"psr/cache-implementation": "^1.0"

0 commit comments

Comments
 (0)