1414use Cache \Adapter \Common \AbstractCachePool ;
1515use Cache \Hierarchy \HierarchicalCachePoolTrait ;
1616use Cache \Hierarchy \HierarchicalPoolInterface ;
17+ use Cache \Taggable \TaggableItemInterface ;
18+ use Cache \Taggable \TaggablePoolInterface ;
19+ use Cache \Taggable \TaggablePoolTrait ;
1720use Psr \Cache \CacheItemInterface ;
1821
1922/**
2023 * @author Tobias Nyholm <[email protected] > 2124 */
22- class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterface
25+ class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterface, TaggablePoolInterface
2326{
2427 use HierarchicalCachePoolTrait;
28+ use TaggablePoolTrait;
2529
2630 /**
2731 * @type \Redis
@@ -39,7 +43,7 @@ public function __construct(\Redis $cache)
3943 protected function fetchObjectFromCache ($ key )
4044 {
4145 if (false === $ result = unserialize ($ this ->cache ->get ($ this ->getHierarchyKey ($ key )))) {
42- return [false , null ];
46+ return [false , null , [] ];
4347 }
4448
4549 return $ result ;
@@ -53,26 +57,56 @@ protected function clearAllObjectsFromCache()
5357 protected function clearOneObjectFromCache ($ key )
5458 {
5559 $ this ->commit ();
60+ $ this ->preRemoveItem ($ key );
5661 $ keyString = $ this ->getHierarchyKey ($ key , $ path );
5762 $ this ->cache ->incr ($ path );
5863 $ this ->clearHierarchyKeyCache ();
5964
6065 return $ this ->cache ->del ($ keyString ) >= 0 ;
6166 }
6267
63- protected function storeItemInCache ($ key , CacheItemInterface $ item , $ ttl )
68+ protected function storeItemInCache (CacheItemInterface $ item , $ ttl )
6469 {
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 ) {
6873 return $ this ->cache ->set ($ key , $ data );
6974 }
7075
7176 return $ this ->cache ->setex ($ key , $ ttl , $ data );
7277 }
7378
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+
7488 protected function getValueFormStore ($ key )
7589 {
7690 return $ this ->cache ->get ($ key );
7791 }
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+ }
78112}
0 commit comments