Skip to content

Commit 737e630

Browse files
committed
Making cache tags more efficient by rewriting the API.
Related to php-cache/issues#21
1 parent c6c0dd2 commit 737e630

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

RedisCachePool.php

+40-6
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 Psr\Cache\CacheItemInterface;
1821

1922
/**
2023
* @author Tobias Nyholm <[email protected]>
2124
*/
22-
class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterface
25+
class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterface, TaggablePoolInterface
2326
{
2427
use HierarchicalCachePoolTrait;
28+
use TaggablePoolTrait;
2529

2630
/**
2731
* @type \Redis
@@ -39,7 +43,7 @@ public function __construct(\Redis $cache)
3943
protected function fetchObjectFromCache($key)
4044
{
4145
if (false === $result = unserialize($this->cache->get($this->getHierarchyKey($key)))) {
42-
return [false, null];
46+
return [false, null, []];
4347
}
4448

4549
return $result;
@@ -53,26 +57,56 @@ protected function clearAllObjectsFromCache()
5357
protected function clearOneObjectFromCache($key)
5458
{
5559
$this->commit();
60+
$this->preRemoveItem($key);
5661
$keyString = $this->getHierarchyKey($key, $path);
5762
$this->cache->incr($path);
5863
$this->clearHierarchyKeyCache();
5964

6065
return $this->cache->del($keyString) >= 0;
6166
}
6267

63-
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
68+
protected function storeItemInCache(CacheItemInterface $item, $ttl)
6469
{
65-
$key = $this->getHierarchyKey($key);
66-
$data = serialize([true, $item->get()]);
67-
if ($ttl === null) {
70+
$key = $this->getHierarchyKey($item->getKey());
71+
$data = serialize([true, $item->get(), $item->getTags()]);
72+
if ($ttl === null || $ttl === 0) {
6873
return $this->cache->set($key, $data);
6974
}
7075

7176
return $this->cache->setex($key, $ttl, $data);
7277
}
7378

79+
public function save(CacheItemInterface $item)
80+
{
81+
if ($item instanceof TaggableItemInterface) {
82+
$this->saveTags($item);
83+
}
84+
85+
return parent::save($item);
86+
}
87+
7488
protected function getValueFormStore($key)
7589
{
7690
return $this->cache->get($key);
7791
}
92+
93+
protected function appendListItem($name, $value)
94+
{
95+
$this->cache->lPush($name, $value);
96+
}
97+
98+
protected function getList($name)
99+
{
100+
return $this->cache->lRange($name, 0, -1);
101+
}
102+
103+
protected function removeList($name)
104+
{
105+
return $this->cache->del($name);
106+
}
107+
108+
protected function removeListItem($name, $key)
109+
{
110+
return $this->cache->lrem($name, $key, 0);
111+
}
78112
}

composer.json

+1-1
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)