@@ -56,17 +56,17 @@ $pool->clear();
5656If you are writing a PSR-6 implementation you may want to use this library. The implementation is easy and will work
5757with all PSR-6 caches.
5858
59- ** Warning: All the cache keys will change because we have to generate new cache keys that
60- depends on the tags. This will include keys that do not use tags.**
61-
62- You need to do three changes on the implementation of ` CacheItemPoolInterface ` .
59+ You need to do a few changes on the implementation of ` CacheItemPoolInterface ` .
6360
6461* Implement ` TaggablePoolInterface ` and use ` TaggablePoolTrait `
62+ * Implement ` TaggableItemInterface ` and use ` TaggableItemTrait `
6563* Use ` TaggablePoolTrait::generateCacheKey($key, array $tags) `
66- * Implement ` CachePool::getTagItem($key) `
64+ * Use ` TaggableItemInterface::getTaggedKey() `
65+ * Implement ` CachePool::getItemWithoutGenerateCacheKey($key) `
66+ * Implement ` CachePool::validateTagName($key) `
6767
6868
69- ### Implement interface and use trait
69+ ### Implement interface and use trait for CacheItemPoolInterface
7070
7171The trait has two protected methods; ` generateCacheKey($key, array $tags) ` and ` flushTag($name) ` .
7272
@@ -79,6 +79,34 @@ class Pool implements CacheItemPoolInterface, TaggablePoolInterface
7979}
8080```
8181
82+ ### Implement interface and use trait for CacheItemInterface
83+
84+ The purpose of the trait is to be able to return a ` taggedKey ` and a normal ` key ` . The trait has one protected function
85+ ` getKeyFromTaggedKey() ` and one public function ` getTaggedkey() ` . You should use the trait and make sure you set values
86+ to your two keys.
87+
88+ ``` php
89+ class Pool implements CacheItemInterface, TaggableItemInterface
90+ {
91+ use TaggableItemTrait;
92+
93+ private $normalKey;
94+
95+ public function __construct($key)
96+ {
97+ $this->taggedKey = $key;
98+ $this->normalKey = $this->getKeyFromTaggedKey($key);
99+ }
100+
101+ public function getKey()
102+ {
103+ return $this->normalKey;
104+ }
105+
106+ // ...
107+ }
108+ ```
109+
82110### Generate cache key
83111
84112Your cache pool's ` getItem() ` probably look like this:
@@ -129,10 +157,24 @@ Here is the list of functions you need to change:
129157* deleteItem
130158* deleteItems
131159
132- ### Implement CachePool::getTagItem($key)
160+ ### Use TaggableItemInterface::getTaggedKey()
161+
162+ To make sure we fetch the correct item the cache pool should always use ` $item->getTaggedKey ` . This will return a
163+ cache key that depends on the tags.
164+
165+ ``` php
166+ public function save(CacheItemInterface $item)
167+ {
168+ $key = $item->getTaggedKey();
169+
170+ return $this->storage->save($key, $item);
171+ }
172+ ```
173+
174+ ### Implement CachePool::getItemWithoutGenerateCacheKey($key)
133175
134176The trait uses the cache as a key-value store. The key is the tag name and the value is a random id created by
135- ` uniqid() ` . The way to access the cache is by a protected function ` getTagItem ($key)` . This function will be very similar to your
177+ ` uniqid() ` . The way to access the cache is by a protected function ` getItemWithoutGenerateCacheKey ($key)` . This function will be very similar to your
136178` getItem($key, array $tags = []) ` . The only difference is that the latter will call ` generateCacheKey() ` .
137179
138180Consider your new ` getItem($key, array $tags = []) ` :
@@ -150,9 +192,9 @@ public function getItem($key, array $tags = [])
150192}
151193```
152194
153- You would need ` getTagItem ($key)` to look like this:
195+ You would need ` getItemWithoutGenerateCacheKey ($key)` to look like this:
154196``` php
155- protected function getTagItem ($key)
197+ protected function getItemWithoutGenerateCacheKey ($key)
156198{
157199 $item = $this->storage->fetch($key);
158200 if (false === $item) {
@@ -169,10 +211,10 @@ public function getItem($key, array $tags = [])
169211{
170212 $taggedKey = $this->generateCacheKey($key, $tags);
171213
172- return $this->getTagItem ($taggedKey);
214+ return $this->getItemWithoutGenerateCacheKey ($taggedKey);
173215}
174216
175- protected function getTagItem ($key)
217+ protected function getItemWithoutGenerateCacheKey ($key)
176218{
177219 $item = $this->storage->fetch($key);
178220 if (false === $item) {
@@ -183,6 +225,23 @@ protected function getTagItem($key)
183225}
184226```
185227
228+ ### Implement CachePool::validateTagName($key)
229+
230+ We want to make sure the user do not try to use any invalid characters in the tags. The validation could be the same
231+ as for normal keys.
232+
233+ ``` php
234+ public function validateTagName($name)
235+ {
236+ $this->validateKey($name);
237+ }
238+
239+ public function validateKey($name)
240+ {
241+ // The function you are using to verify the key name for normal items.
242+ }
243+
244+ ```
186245
187246### Deleting tagged items
188247
@@ -209,3 +268,9 @@ The `TaggablePoolTrait::flushTag($name)` changes the tag cache key so next time
209268` TaggablePoolTrait::generateCacheKey($key, array $tags) ` you will get a different cache key back. This will not remove
210269the items from the cache, which introduce a memory leak. That is why it is important to use memcached or redis, which
211270automatically purges stale records.
271+
272+
273+ ## Test your tagging cache
274+
275+ When you are happy with your implementation you should test it. We have provided some integration tests that will test
276+ your implementation for you. See this repository for more info: https://github.com/php-cache/integration-tests
0 commit comments