Skip to content

Commit 38f5510

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

File tree

5 files changed

+97
-111
lines changed

5 files changed

+97
-111
lines changed

AbstractCachePool.php

Lines changed: 47 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,53 @@
1212
namespace Cache\Adapter\Common;
1313

1414
use Cache\Adapter\Common\Exception\InvalidArgumentException;
15-
use Cache\Taggable\TaggableItemInterface;
16-
use Cache\Taggable\TaggablePoolInterface;
17-
use Cache\Taggable\TaggablePoolTrait;
1815
use Psr\Cache\CacheItemInterface;
1916
use Psr\Cache\CacheItemPoolInterface;
2017

2118
/**
2219
* @author Aaron Scherer <[email protected]>
2320
* @author Tobias Nyholm <[email protected]>
2421
*/
25-
abstract class AbstractCachePool implements CacheItemPoolInterface, TaggablePoolInterface
22+
abstract class AbstractCachePool implements CacheItemPoolInterface
2623
{
27-
use TaggablePoolTrait;
28-
2924
/**
3025
* @type CacheItemInterface[] deferred
3126
*/
3227
protected $deferred = [];
3328

29+
/**
30+
* @param CacheItemInterface $item
31+
* @param int|null $ttl seconds from now
32+
*
33+
* @return bool true if saved
34+
*/
35+
abstract protected function storeItemInCache(CacheItemInterface $item, $ttl);
36+
37+
/**
38+
* Fetch an object from the cache implementation.
39+
*
40+
* @param string $key
41+
*
42+
* @return array with [isHit, value]
43+
*/
44+
abstract protected function fetchObjectFromCache($key);
45+
46+
/**
47+
* Clear all objects from cache.
48+
*
49+
* @return bool false if error
50+
*/
51+
abstract protected function clearAllObjectsFromCache();
52+
53+
/**
54+
* Remove one object from cache.
55+
*
56+
* @param string $key
57+
*
58+
* @return bool
59+
*/
60+
abstract protected function clearOneObjectFromCache($key);
61+
3462
/**
3563
* Make sure to commit before we destruct.
3664
*/
@@ -42,19 +70,9 @@ public function __destruct()
4270
/**
4371
* {@inheritdoc}
4472
*/
45-
public function getItem($key, array $tags = [])
73+
public function getItem($key)
4674
{
4775
$this->validateKey($key);
48-
$taggedKey = $this->generateCacheKey($key, $tags);
49-
50-
return $this->getItemWithoutGenerateCacheKey($taggedKey);
51-
}
52-
53-
/**
54-
* {@inheritdoc}
55-
*/
56-
protected function getItemWithoutGenerateCacheKey($key)
57-
{
5876
if (isset($this->deferred[$key])) {
5977
$item = $this->deferred[$key];
6078

@@ -68,23 +86,14 @@ protected function getItemWithoutGenerateCacheKey($key)
6886
return new CacheItem($key, $func);
6987
}
7088

71-
/**
72-
* Fetch an object from the cache implementation.
73-
*
74-
* @param string $key
75-
*
76-
* @return array with [isHit, value]
77-
*/
78-
abstract protected function fetchObjectFromCache($key);
79-
8089
/**
8190
* {@inheritdoc}
8291
*/
83-
public function getItems(array $keys = [], array $tags = [])
92+
public function getItems(array $keys = [])
8493
{
8594
$items = [];
8695
foreach ($keys as $key) {
87-
$items[$key] = $this->getItem($key, $tags);
96+
$items[$key] = $this->getItem($key);
8897
}
8998

9099
return $items;
@@ -93,117 +102,71 @@ public function getItems(array $keys = [], array $tags = [])
93102
/**
94103
* {@inheritdoc}
95104
*/
96-
public function hasItem($key, array $tags = [])
105+
public function hasItem($key)
97106
{
98-
return $this->getItem($key, $tags)->isHit();
107+
return $this->getItem($key)->isHit();
99108
}
100109

101110
/**
102111
* {@inheritdoc}
103112
*/
104-
public function clear(array $tags = [])
113+
public function clear()
105114
{
106-
if (!empty($tags)) {
107-
foreach ($tags as $tag) {
108-
$this->flushTag($tag);
109-
}
110-
111-
return true;
112-
}
113-
114115
// Clear the deferred items
115116
$this->deferred = [];
116117

117118
return $this->clearAllObjectsFromCache();
118119
}
119120

120-
/**
121-
* Clear all objects from cache.
122-
*
123-
* @return bool false if error
124-
*/
125-
abstract protected function clearAllObjectsFromCache();
126-
127121
/**
128122
* {@inheritdoc}
129123
*/
130-
public function deleteItem($key, array $tags = [])
124+
public function deleteItem($key)
131125
{
132-
return $this->deleteItems([$key], $tags);
126+
return $this->deleteItems([$key]);
133127
}
134128

135129
/**
136130
* {@inheritdoc}
137131
*/
138-
public function deleteItems(array $keys, array $tags = [])
132+
public function deleteItems(array $keys)
139133
{
140134
$deleted = true;
141135
foreach ($keys as $key) {
142136
$this->validateKey($key);
143-
$taggedKey = $this->generateCacheKey($key, $tags);
144137

145138
// Delete form deferred
146-
unset($this->deferred[$taggedKey]);
139+
unset($this->deferred[$key]);
147140

148-
if (!$this->clearOneObjectFromCache($taggedKey)) {
141+
if (!$this->clearOneObjectFromCache($key)) {
149142
$deleted = false;
150143
}
151144
}
152145

153146
return $deleted;
154147
}
155148

156-
/**
157-
* Remove one object from cache.
158-
*
159-
* @param string $key
160-
*
161-
* @return bool
162-
*/
163-
abstract protected function clearOneObjectFromCache($key);
164-
165149
/**
166150
* {@inheritdoc}
167151
*/
168152
public function save(CacheItemInterface $item)
169153
{
170-
if ($item instanceof TaggableItemInterface) {
171-
$key = $item->getTaggedKey();
172-
} else {
173-
$key = $item->getKey();
174-
}
175-
176154
$timeToLive = null;
177155
if ($item instanceof HasExpirationDateInterface) {
178156
if (null !== $expirationDate = $item->getExpirationDate()) {
179157
$timeToLive = $expirationDate->getTimestamp() - time();
180158
}
181159
}
182160

183-
return $this->storeItemInCache($key, $item, $timeToLive);
161+
return $this->storeItemInCache($item, $timeToLive);
184162
}
185163

186-
/**
187-
* @param string $key
188-
* @param CacheItemInterface $item
189-
* @param int|null $ttl seconds from now
190-
*
191-
* @return bool true if saved
192-
*/
193-
abstract protected function storeItemInCache($key, CacheItemInterface $item, $ttl);
194-
195164
/**
196165
* {@inheritdoc}
197166
*/
198167
public function saveDeferred(CacheItemInterface $item)
199168
{
200-
if ($item instanceof TaggableItemInterface) {
201-
$key = $item->getTaggedKey();
202-
} else {
203-
$key = $item->getKey();
204-
}
205-
206-
$this->deferred[$key] = $item;
169+
$this->deferred[$item->getKey()] = $item;
207170

208171
return true;
209172
}
@@ -244,14 +207,4 @@ protected function validateKey($key)
244207
));
245208
}
246209
}
247-
248-
/**
249-
* @param string $name
250-
*
251-
* @throws InvalidArgumentException
252-
*/
253-
protected function validateTagName($name)
254-
{
255-
$this->validateKey($name);
256-
}
257210
}

CacheItem.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Cache\Adapter\Common;
1313

1414
use Cache\Taggable\TaggableItemInterface;
15-
use Cache\Taggable\TaggableItemTrait;
1615
use Psr\Cache\CacheItemInterface;
1716

1817
/**
@@ -21,7 +20,10 @@
2120
*/
2221
class CacheItem implements HasExpirationDateInterface, CacheItemInterface, TaggableItemInterface
2322
{
24-
use TaggableItemTrait;
23+
/**
24+
* @type array
25+
*/
26+
private $tags = [];
2527

2628
/**
2729
* @type \Closure
@@ -54,8 +56,7 @@ class CacheItem implements HasExpirationDateInterface, CacheItemInterface, Tagga
5456
*/
5557
public function __construct($key, $callable = null, $value = null)
5658
{
57-
$this->taggedKey = $key;
58-
$this->key = $this->getKeyFromTaggedKey($key);
59+
$this->key = $key;
5960

6061
if ($callable === true) {
6162
$this->hasValue = true;
@@ -103,12 +104,7 @@ public function get()
103104
*/
104105
public function isHit()
105106
{
106-
if ($this->callable !== null) {
107-
// Initialize
108-
$f = $this->callable;
109-
list($this->hasValue, $this->value) = $f();
110-
$this->callable = null;
111-
}
107+
$this->initialize();
112108

113109
if (!$this->hasValue) {
114110
return false;
@@ -163,4 +159,41 @@ public function expiresAfter($time)
163159

164160
return $this;
165161
}
162+
163+
public function getTags()
164+
{
165+
$this->initialize();
166+
167+
return $this->tags;
168+
}
169+
170+
public function addTag($tag)
171+
{
172+
$this->initialize();
173+
174+
$this->tags[] = $tag;
175+
176+
return $this;
177+
}
178+
179+
public function setTags(array $tags)
180+
{
181+
$this->initialize();
182+
183+
$this->tags = $tags;
184+
185+
return $this;
186+
}
187+
188+
/**
189+
* If callable is not null, execute it an populate this object with values.
190+
*/
191+
private function initialize()
192+
{
193+
if ($this->callable !== null) {
194+
$f = $this->callable;
195+
list($this->hasValue, $this->value, $this->tags) = $f();
196+
$this->callable = null;
197+
}
198+
}
166199
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Common PSR-6 Cache pool
22
[![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3-
[![Latest Stable Version](https://poser.pugx.org/cache/common-adapter/v/stable)](https://packagist.org/packages/cache/common-adapter)
4-
[![Total Downloads](https://poser.pugx.org/cache/common-adapter/downloads)](https://packagist.org/packages/cache/common-adapter)
5-
[![Monthly Downloads](https://poser.pugx.org/cache/common-adapter/d/monthly.png)](https://packagist.org/packages/cache/common-adapter)
3+
[![Latest Stable Version](https://poser.pugx.org/cache/adapter-common/v/stable)](https://packagist.org/packages/cache/adapter-common)
4+
[![Total Downloads](https://poser.pugx.org/cache/adapter-common/downloads)](https://packagist.org/packages/cache/adapter-common)
5+
[![Monthly Downloads](https://poser.pugx.org/cache/adapter-common/d/monthly.png)](https://packagist.org/packages/cache/adapter-common)
66
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
77

88
This repository contains shared classes and interfaces used by the PHP Cache organisation. To read about

Tests/CacheItemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ public function testHit()
6767
$this->assertFalse($item->isHit());
6868

6969
$closure = function () {
70-
return [true, 'value'];
70+
return [true, 'value', []];
7171
};
7272
$item = new CacheItem('test_key', $closure);
7373
$this->assertTrue($item->isHit());
7474

7575
$closure = function () {
76-
return [false, null];
76+
return [false, null, []];
7777
};
7878
$item = new CacheItem('test_key', $closure);
7979
$this->assertFalse($item->isHit());

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
"require": {
2626
"php": "^5.5|^7.0",
2727
"psr/cache": "^1.0",
28-
"cache/taggable-cache": "^0.2|^0.3"
28+
"cache/taggable-cache": "^0.4"
2929
},
3030
"require-dev": {
3131
"phpunit/phpunit": "^4.0|^5.1",
32-
"cache/integration-tests": "0.7.0"
32+
"cache/integration-tests": "0.9.0"
3333
},
3434
"autoload": {
3535
"psr-4": {

0 commit comments

Comments
 (0)