Skip to content

Commit ecaaa2e

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

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

ArrayCachePool.php

+59-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
use Cache\Adapter\Common\CacheItem;
1616
use Cache\Hierarchy\HierarchicalCachePoolTrait;
1717
use Cache\Hierarchy\HierarchicalPoolInterface;
18+
use Cache\Taggable\TaggableItemInterface;
19+
use Cache\Taggable\TaggablePoolInterface;
20+
use Cache\Taggable\TaggablePoolTrait;
1821
use Psr\Cache\CacheItemInterface;
1922

2023
/**
2124
* Array cache pool. You could set a limit of how many items you wantt to be stored to avoid memory leaks.
2225
*
2326
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2427
*/
25-
class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface
28+
class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface, TaggablePoolInterface
2629
{
30+
use TaggablePoolTrait;
2731
use HierarchicalCachePoolTrait;
2832

2933
/**
@@ -78,6 +82,18 @@ protected function getItemWithoutGenerateCacheKey($key)
7882
*/
7983
protected function fetchObjectFromCache($key)
8084
{
85+
// Not used
86+
}
87+
88+
public function getItem($key)
89+
{
90+
$this->validateKey($key);
91+
if (isset($this->deferred[$key])) {
92+
$item = $this->deferred[$key];
93+
94+
return is_object($item) ? clone $item : $item;
95+
}
96+
8197
$storageKey = $this->getHierarchyKey($key);
8298
if (isset($this->cache[$storageKey])) {
8399
return $this->cache[$storageKey];
@@ -102,6 +118,7 @@ protected function clearAllObjectsFromCache()
102118
protected function clearOneObjectFromCache($key)
103119
{
104120
$this->commit();
121+
$this->preRemoveItem($key);
105122
$keyString = $this->getHierarchyKey($key, $path);
106123
if (isset($this->cache[$path])) {
107124
$this->cache[$path]++;
@@ -118,9 +135,9 @@ protected function clearOneObjectFromCache($key)
118135
/**
119136
* {@inheritdoc}
120137
*/
121-
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
138+
protected function storeItemInCache(CacheItemInterface $item, $ttl)
122139
{
123-
$key = $this->getHierarchyKey($key);
140+
$key = $this->getHierarchyKey($item->getKey());
124141
$this->cache[$key] = $item;
125142

126143
if ($this->limit !== null) {
@@ -139,6 +156,15 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
139156
return true;
140157
}
141158

159+
public function save(CacheItemInterface $item)
160+
{
161+
if ($item instanceof TaggableItemInterface) {
162+
$this->saveTags($item);
163+
}
164+
165+
return parent::save($item);
166+
}
167+
142168
/**
143169
* {@inheritdoc}
144170
*/
@@ -148,4 +174,34 @@ protected function getValueFormStore($key)
148174
return $this->cache[$key];
149175
}
150176
}
177+
178+
protected function getList($name)
179+
{
180+
if (!isset($this->cache[$name])) {
181+
$this->cache[$name] = [];
182+
}
183+
184+
return $this->cache[$name];
185+
}
186+
187+
protected function removeList($name)
188+
{
189+
unset($this->cache[$name]);
190+
191+
return true;
192+
}
193+
194+
protected function appendListItem($name, $key)
195+
{
196+
$this->cache[$name][] = $key;
197+
}
198+
199+
protected function removeListItem($name, $key)
200+
{
201+
foreach ($this->cache[$name] as $i => $item) {
202+
if ($item === $key) {
203+
unset($this->cache[$name][$i]);
204+
}
205+
}
206+
}
151207
}

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)