14
14
use Cache \Adapter \Common \AbstractCachePool ;
15
15
use Cache \Hierarchy \HierarchicalCachePoolTrait ;
16
16
use Cache \Hierarchy \HierarchicalPoolInterface ;
17
+ use Cache \Taggable \TaggableItemInterface ;
18
+ use Cache \Taggable \TaggablePoolInterface ;
19
+ use Cache \Taggable \TaggablePoolTrait ;
17
20
use Psr \Cache \CacheItemInterface ;
18
21
19
22
/**
20
23
* @author Tobias Nyholm <[email protected] >
21
24
*/
22
- class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterface
25
+ class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterface, TaggablePoolInterface
23
26
{
24
27
use HierarchicalCachePoolTrait;
28
+ use TaggablePoolTrait;
25
29
26
30
/**
27
31
* @type \Redis
@@ -39,7 +43,7 @@ public function __construct(\Redis $cache)
39
43
protected function fetchObjectFromCache ($ key )
40
44
{
41
45
if (false === $ result = unserialize ($ this ->cache ->get ($ this ->getHierarchyKey ($ key )))) {
42
- return [false , null ];
46
+ return [false , null , [] ];
43
47
}
44
48
45
49
return $ result ;
@@ -53,26 +57,56 @@ protected function clearAllObjectsFromCache()
53
57
protected function clearOneObjectFromCache ($ key )
54
58
{
55
59
$ this ->commit ();
60
+ $ this ->preRemoveItem ($ key );
56
61
$ keyString = $ this ->getHierarchyKey ($ key , $ path );
57
62
$ this ->cache ->incr ($ path );
58
63
$ this ->clearHierarchyKeyCache ();
59
64
60
65
return $ this ->cache ->del ($ keyString ) >= 0 ;
61
66
}
62
67
63
- protected function storeItemInCache ($ key , CacheItemInterface $ item , $ ttl )
68
+ protected function storeItemInCache (CacheItemInterface $ item , $ ttl )
64
69
{
65
- $ key = $ this ->getHierarchyKey ($ key );
66
- $ data = serialize ([true , $ item ->get ()]);
67
- if ($ ttl === null ) {
70
+ $ key = $ this ->getHierarchyKey ($ item -> getKey () );
71
+ $ data = serialize ([true , $ item ->get (), $ item -> getTags () ]);
72
+ if ($ ttl === null || $ ttl === 0 ) {
68
73
return $ this ->cache ->set ($ key , $ data );
69
74
}
70
75
71
76
return $ this ->cache ->setex ($ key , $ ttl , $ data );
72
77
}
73
78
79
+ public function save (CacheItemInterface $ item )
80
+ {
81
+ if ($ item instanceof TaggableItemInterface) {
82
+ $ this ->saveTags ($ item );
83
+ }
84
+
85
+ return parent ::save ($ item );
86
+ }
87
+
74
88
protected function getValueFormStore ($ key )
75
89
{
76
90
return $ this ->cache ->get ($ key );
77
91
}
92
+
93
+ protected function appendListItem ($ name , $ value )
94
+ {
95
+ $ this ->cache ->lPush ($ name , $ value );
96
+ }
97
+
98
+ protected function getList ($ name )
99
+ {
100
+ return $ this ->cache ->lRange ($ name , 0 , -1 );
101
+ }
102
+
103
+ protected function removeList ($ name )
104
+ {
105
+ return $ this ->cache ->del ($ name );
106
+ }
107
+
108
+ protected function removeListItem ($ name , $ key )
109
+ {
110
+ return $ this ->cache ->lrem ($ name , $ key , 0 );
111
+ }
78
112
}
0 commit comments