Skip to content

Commit f3b56fa

Browse files
Added a taggable adapter, to make any PSR-6 pool taggable.
1 parent b32546c commit f3b56fa

4 files changed

Lines changed: 356 additions & 0 deletions

File tree

TaggablePSR6ItemAdapter.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Cache\Taggable;
4+
5+
use Cache\Taggable\TaggableItemInterface;
6+
use Cache\Taggable\TaggablePoolInterface;
7+
use Cache\Taggable\TaggablePoolTrait;
8+
use Psr\Cache\CacheItemInterface;
9+
use Psr\Cache\CacheItemPoolInterface;
10+
11+
/**
12+
*
13+
*/
14+
class TaggablePSR6ItemAdapter implements TaggableItemInterface
15+
{
16+
private $cacheItem;
17+
private $tags = [];
18+
19+
private function __construct(CacheItemInterface $cacheItem)
20+
{
21+
$this->cacheItem = $cacheItem;
22+
if ($this->cacheItem->isHit()) {
23+
$rawItem = $this->cacheItem->get();
24+
25+
if (is_array($rawItem) && isset($rawItem['tags'])) {
26+
$this->tags = $rawItem['tags'];
27+
}
28+
}
29+
}
30+
31+
public static function makeTaggable(CacheItemInterface $cacheItem) // @TODO naming?
32+
{
33+
return new self($cacheItem);
34+
}
35+
36+
public function unwrap()
37+
{
38+
return $this->cacheItem;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getKey()
45+
{
46+
return $this->cacheItem->getKey();
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function get()
53+
{
54+
$rawItem = $this->cacheItem->get();
55+
56+
if (is_array($rawItem) && isset($rawItem['value'])) {
57+
return $rawItem['value'];
58+
}
59+
60+
return null;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function isHit()
67+
{
68+
return $this->cacheItem->isHit();
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function set($value)
75+
{
76+
$this->cacheItem->set([
77+
'value' => $value,
78+
'tags' => $this->tags,
79+
]);
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function getTags()
88+
{
89+
return $this->tags;
90+
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
public function setTags(array $tags)
96+
{
97+
$this->tags = $tags;
98+
$this->updateTags();
99+
100+
return $this;
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function addTag($tag)
107+
{
108+
$this->tags[] = $tag;
109+
$this->updateTags();
110+
111+
return $this;
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function expiresAt($expiration)
118+
{
119+
$this->cacheItem->expiresAt($expiration);
120+
return $this;
121+
}
122+
123+
/**
124+
* {@inheritdoc}
125+
*/
126+
public function expiresAfter($time)
127+
{
128+
$this->cacheItem->expiresAfter($time);
129+
return $this;
130+
}
131+
132+
private function updateTags()
133+
{
134+
$this->cacheItem->set([
135+
'value' => $this->get(),
136+
'tags' => $this->tags,
137+
]);
138+
}
139+
}

TaggablePSR6PoolAdapter.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace Cache\Taggable;
4+
5+
use Cache\Taggable\TaggableItemInterface;
6+
use Cache\Taggable\TaggablePoolInterface;
7+
use Cache\Taggable\TaggablePoolTrait;
8+
use Psr\Cache\CacheItemInterface;
9+
use Psr\Cache\CacheItemPoolInterface;
10+
11+
/**
12+
*
13+
*/
14+
class TaggablePSR6PoolAdapter implements TaggablePoolInterface
15+
{
16+
use TaggablePoolTrait;
17+
18+
private $cachePool;
19+
private $tagStorePool;
20+
21+
private function __construct(CacheItemPoolInterface $cachePool, CacheItemPoolInterface $tagStorePool = null)
22+
{
23+
$this->cachePool = $cachePool;
24+
if ($tagStorePool) {
25+
$this->tagStorePool = $tagStorePool;
26+
} else {
27+
$this->tagStorePool = $cachePool;
28+
}
29+
}
30+
31+
public static function makeTaggable(CacheItemPoolInterface $cachePool, CacheItemPoolInterface $tagStorePool = null) // @TODO naming?
32+
{
33+
if ($cachePool instanceOf TaggablePoolInterface && $tagStorePool === null) {
34+
return $cachePool;
35+
}
36+
37+
return new self($cachePool, $tagStorePool);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getItem($key)
44+
{
45+
return TaggablePSR6ItemAdapter::makeTaggable($this->cachePool->getItem($key));
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getItems(array $keys = array())
52+
{
53+
$items = $this->cachePool->getItems($keys);
54+
55+
$wrappedItems = [];
56+
foreach ($items as $key => $item) {
57+
$wrappedItems[$key] = TaggablePSR6ItemAdapter::makeTaggable($item);
58+
}
59+
60+
return $wrappedItems;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function hasItem($key)
67+
{
68+
return $this->cachePool->hasItem($key);
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function clear()
75+
{
76+
$ret = $this->cachePool->clear();
77+
return $this->tagStorePool->clear() && $ret; // Is this acceptable?
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function deleteItem($key)
84+
{
85+
$this->preRemoveItem($key);
86+
return $this->cachePool->deleteItem($key);
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function deleteItems(array $keys)
93+
{
94+
foreach ($keys as $key) {
95+
$this->preRemoveItem($key);
96+
}
97+
98+
return $this->cachePool->deleteItems($keys);
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function save(CacheItemInterface $item)
105+
{
106+
$this->saveTags($item);
107+
return $this->cachePool->save($item->unwrap());
108+
}
109+
110+
/**
111+
* {@inheritdoc}
112+
*/
113+
public function saveDeferred(CacheItemInterface $item)
114+
{
115+
$this->saveTags($item);
116+
return $this->cachePool->saveDeferred($item->unwrap());
117+
}
118+
119+
/**
120+
* {@inheritdoc}
121+
*/
122+
public function commit()
123+
{
124+
$this->tagStorePool->commit();
125+
$this->cachePool->commit();
126+
}
127+
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
protected function appendListItem($name, $value)
132+
{
133+
$listItem = $this->tagStorePool->getItem($name);
134+
if (!is_array($list = $listItem->get())) {
135+
$list = [];
136+
}
137+
138+
$list[] = $value;
139+
$listItem->set($list);
140+
$this->tagStorePool->save($listItem);
141+
}
142+
143+
/**
144+
* {@inheritdoc}
145+
*/
146+
protected function removeList($name)
147+
{
148+
return $this->tagStorePool->deleteItem($name);
149+
}
150+
151+
/**
152+
* {@inheritdoc}
153+
*/
154+
protected function removeListItem($name, $key)
155+
{
156+
$listItem = $this->tagStorePool->getItem($name);
157+
if (!is_array($list = $listItem->get())) {
158+
$list = [];
159+
}
160+
161+
$list = array_filter($list, function ($value) use ($key) { return $value !== $key; });
162+
163+
$listItem->set($list);
164+
$this->tagStorePool->save($listItem);
165+
}
166+
167+
/**
168+
* {@inheritdoc}
169+
*/
170+
protected function getList($name)
171+
{
172+
$listItem = $this->tagStorePool->getItem($name);
173+
if (!is_array($list = $listItem->get())) {
174+
$list = [];
175+
}
176+
177+
return $list;
178+
}
179+
180+
/**
181+
* {@inheritdoc}
182+
*/
183+
private function getTagKey($tag)
184+
{
185+
return '__tag.'.$tag;
186+
}
187+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Cache\Taggable\Tests;
4+
5+
use Cache\IntegrationTests\TaggableCachePoolTest;
6+
use Cache\Taggable\TaggablePSR6PoolAdapter;
7+
use Symfony\Component\Cache\Adapter\ArrayAdapter as SymfonyArrayAdapter;
8+
9+
class SameTagPoolTaggablePSR6AdapterTest extends TaggableCachePoolTest
10+
{
11+
public function createCachePool()
12+
{
13+
return TaggablePSR6PoolAdapter::makeTaggable(new SymfonyArrayAdapter);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Cache\Taggable\Tests;
4+
5+
use Cache\IntegrationTests\TaggableCachePoolTest;
6+
use Cache\Taggable\TaggablePSR6PoolAdapter;
7+
use Symfony\Component\Cache\Adapter\ArrayAdapter as SymfonyArrayAdapter;
8+
9+
class SeparateTagPoolTaggablePSR6AdapterTest extends TaggableCachePoolTest
10+
{
11+
public function createCachePool()
12+
{
13+
return TaggablePSR6PoolAdapter::makeTaggable(new SymfonyArrayAdapter, new SymfonyArrayAdapter);
14+
}
15+
}

0 commit comments

Comments
 (0)