15
15
use Cache \Adapter \Common \CacheItem ;
16
16
use Cache \Hierarchy \HierarchicalCachePoolTrait ;
17
17
use Cache \Hierarchy \HierarchicalPoolInterface ;
18
+ use Cache \Taggable \TaggableItemInterface ;
19
+ use Cache \Taggable \TaggablePoolInterface ;
20
+ use Cache \Taggable \TaggablePoolTrait ;
18
21
use Psr \Cache \CacheItemInterface ;
19
22
20
23
/**
21
24
* Array cache pool. You could set a limit of how many items you wantt to be stored to avoid memory leaks.
22
25
*
23
26
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
24
27
*/
25
- class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface
28
+ class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface, TaggablePoolInterface
26
29
{
30
+ use TaggablePoolTrait;
27
31
use HierarchicalCachePoolTrait;
28
32
29
33
/**
@@ -78,6 +82,18 @@ protected function getItemWithoutGenerateCacheKey($key)
78
82
*/
79
83
protected function fetchObjectFromCache ($ key )
80
84
{
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
+
81
97
$ storageKey = $ this ->getHierarchyKey ($ key );
82
98
if (isset ($ this ->cache [$ storageKey ])) {
83
99
return $ this ->cache [$ storageKey ];
@@ -102,6 +118,7 @@ protected function clearAllObjectsFromCache()
102
118
protected function clearOneObjectFromCache ($ key )
103
119
{
104
120
$ this ->commit ();
121
+ $ this ->preRemoveItem ($ key );
105
122
$ keyString = $ this ->getHierarchyKey ($ key , $ path );
106
123
if (isset ($ this ->cache [$ path ])) {
107
124
$ this ->cache [$ path ]++;
@@ -118,9 +135,9 @@ protected function clearOneObjectFromCache($key)
118
135
/**
119
136
* {@inheritdoc}
120
137
*/
121
- protected function storeItemInCache ($ key , CacheItemInterface $ item , $ ttl )
138
+ protected function storeItemInCache (CacheItemInterface $ item , $ ttl )
122
139
{
123
- $ key = $ this ->getHierarchyKey ($ key );
140
+ $ key = $ this ->getHierarchyKey ($ item -> getKey () );
124
141
$ this ->cache [$ key ] = $ item ;
125
142
126
143
if ($ this ->limit !== null ) {
@@ -139,6 +156,15 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
139
156
return true ;
140
157
}
141
158
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
+
142
168
/**
143
169
* {@inheritdoc}
144
170
*/
@@ -148,4 +174,34 @@ protected function getValueFormStore($key)
148
174
return $ this ->cache [$ key ];
149
175
}
150
176
}
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
+ }
151
207
}
0 commit comments