Skip to content

Commit 195ae18

Browse files
committed
Making cache tags more efficient by rewriting the API.
Related to php-cache/issues#21
1 parent 7c67f16 commit 195ae18

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed

FilesystemCachePool.php

+85-12
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@
1313

1414
use Cache\Adapter\Common\AbstractCachePool;
1515
use Cache\Adapter\Common\Exception\InvalidArgumentException;
16+
use Cache\Taggable\TaggableItemInterface;
17+
use Cache\Taggable\TaggablePoolInterface;
18+
use Cache\Taggable\TaggablePoolTrait;
1619
use League\Flysystem\FileNotFoundException;
1720
use League\Flysystem\Filesystem;
1821
use Psr\Cache\CacheItemInterface;
1922

2023
/**
2124
* @author Tobias Nyholm <[email protected]>
2225
*/
23-
class FilesystemCachePool extends AbstractCachePool
26+
class FilesystemCachePool extends AbstractCachePool implements TaggablePoolInterface
2427
{
2528
const CACHE_PATH = 'cache';
29+
30+
use TaggablePoolTrait;
31+
2632
/**
2733
* @type Filesystem
2834
*/
@@ -41,17 +47,20 @@ protected function fetchObjectFromCache($key)
4147
{
4248
$file = $this->getFilePath($key);
4349
if (!$this->filesystem->has($file)) {
44-
return [false, null];
50+
return [false, null, []];
4551
}
4652

4753
$data = unserialize($this->filesystem->read($file));
4854
if ($data[0] !== null && time() > $data[0]) {
49-
$this->clearOneObjectFromCache($key);
55+
foreach ($data[2] as $tag) {
56+
$this->removeListItem($this->getTagKey($tag), $key);
57+
}
58+
$this->forceClear($key);
5059

51-
return [false, null];
60+
return [false, null, []];
5261
}
5362

54-
return [true, $data[1]];
63+
return [true, $data[1], $data[2]];
5564
}
5665

5766
protected function clearAllObjectsFromCache()
@@ -64,23 +73,27 @@ protected function clearAllObjectsFromCache()
6473

6574
protected function clearOneObjectFromCache($key)
6675
{
67-
try {
68-
return $this->filesystem->delete($this->getFilePath($key));
69-
} catch (FileNotFoundException $e) {
70-
return true;
71-
}
76+
$this->preRemoveItem($key);
77+
78+
return $this->forceClear($key);
7279
}
7380

74-
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
81+
protected function storeItemInCache(CacheItemInterface $item, $ttl)
7582
{
76-
$file = $this->getFilePath($key);
83+
$file = $this->getFilePath($item->getKey());
7784
if ($this->filesystem->has($file)) {
7885
$this->filesystem->delete($file);
7986
}
8087

88+
$tags = [];
89+
if ($item instanceof TaggableItemInterface) {
90+
$tags = $item->getTags();
91+
}
92+
8193
return $this->filesystem->write($file, serialize([
8294
($ttl === null ? null : time() + $ttl),
8395
$item->get(),
96+
$tags,
8497
]));
8598
}
8699

@@ -99,4 +112,64 @@ private function getFilePath($key)
99112

100113
return sprintf('%s/%s', self::CACHE_PATH, $key);
101114
}
115+
116+
public function save(CacheItemInterface $item)
117+
{
118+
if ($item instanceof TaggableItemInterface) {
119+
$this->saveTags($item);
120+
}
121+
122+
return parent::save($item);
123+
}
124+
125+
protected function getList($name)
126+
{
127+
$file = $this->getFilePath($name);
128+
129+
if (!$this->filesystem->has($file)) {
130+
$this->filesystem->write($file, serialize([]));
131+
}
132+
133+
return unserialize($this->filesystem->read($file));
134+
}
135+
136+
protected function removeList($name)
137+
{
138+
$file = $this->getFilePath($name);
139+
$this->filesystem->delete($file);
140+
}
141+
142+
protected function appendListItem($name, $key)
143+
{
144+
$list = $this->getList($name);
145+
$list[] = $key;
146+
147+
return $this->filesystem->update($this->getFilePath($name), serialize($list));
148+
}
149+
150+
protected function removeListItem($name, $key)
151+
{
152+
$list = $this->getList($name);
153+
foreach ($list as $i => $item) {
154+
if ($item === $key) {
155+
unset($list[$i]);
156+
}
157+
}
158+
159+
return $this->filesystem->update($this->getFilePath($name), serialize($list));
160+
}
161+
162+
/**
163+
* @param $key
164+
*
165+
* @return bool
166+
*/
167+
private function forceClear($key)
168+
{
169+
try {
170+
return $this->filesystem->delete($this->getFilePath($key));
171+
} catch (FileNotFoundException $e) {
172+
return true;
173+
}
174+
}
102175
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^4.0|^5.1",
35-
"cache/integration-tests": "0.7.0"
35+
"cache/integration-tests": "0.9.0"
3636
},
3737
"provide": {
3838
"psr/cache-implementation": "^1.0"

0 commit comments

Comments
 (0)