13
13
14
14
use Cache \Adapter \Common \AbstractCachePool ;
15
15
use Cache \Adapter \Common \Exception \InvalidArgumentException ;
16
+ use Cache \Taggable \TaggableItemInterface ;
17
+ use Cache \Taggable \TaggablePoolInterface ;
18
+ use Cache \Taggable \TaggablePoolTrait ;
16
19
use League \Flysystem \FileNotFoundException ;
17
20
use League \Flysystem \Filesystem ;
18
21
use Psr \Cache \CacheItemInterface ;
19
22
20
23
/**
21
24
* @author Tobias Nyholm <[email protected] >
22
25
*/
23
- class FilesystemCachePool extends AbstractCachePool
26
+ class FilesystemCachePool extends AbstractCachePool implements TaggablePoolInterface
24
27
{
25
28
const CACHE_PATH = 'cache ' ;
29
+
30
+ use TaggablePoolTrait;
31
+
26
32
/**
27
33
* @type Filesystem
28
34
*/
@@ -41,17 +47,20 @@ protected function fetchObjectFromCache($key)
41
47
{
42
48
$ file = $ this ->getFilePath ($ key );
43
49
if (!$ this ->filesystem ->has ($ file )) {
44
- return [false , null ];
50
+ return [false , null , [] ];
45
51
}
46
52
47
53
$ data = unserialize ($ this ->filesystem ->read ($ file ));
48
54
if ($ data [0 ] !== null && time () > $ data [0 ]) {
49
- $ this ->clearOneObjectFromCache ($ key );
55
+ foreach ($ data [2 ] as $ tag ) {
56
+ $ this ->removeListItem ($ this ->getTagKey ($ tag ), $ key );
57
+ }
58
+ $ this ->forceClear ($ key );
50
59
51
- return [false , null ];
60
+ return [false , null , [] ];
52
61
}
53
62
54
- return [true , $ data [1 ]];
63
+ return [true , $ data [1 ], $ data [ 2 ] ];
55
64
}
56
65
57
66
protected function clearAllObjectsFromCache ()
@@ -64,23 +73,27 @@ protected function clearAllObjectsFromCache()
64
73
65
74
protected function clearOneObjectFromCache ($ key )
66
75
{
67
- try {
68
- return $ this ->filesystem ->delete ($ this ->getFilePath ($ key ));
69
- } catch (FileNotFoundException $ e ) {
70
- return true ;
71
- }
76
+ $ this ->preRemoveItem ($ key );
77
+
78
+ return $ this ->forceClear ($ key );
72
79
}
73
80
74
- protected function storeItemInCache ($ key , CacheItemInterface $ item , $ ttl )
81
+ protected function storeItemInCache (CacheItemInterface $ item , $ ttl )
75
82
{
76
- $ file = $ this ->getFilePath ($ key );
83
+ $ file = $ this ->getFilePath ($ item -> getKey () );
77
84
if ($ this ->filesystem ->has ($ file )) {
78
85
$ this ->filesystem ->delete ($ file );
79
86
}
80
87
88
+ $ tags = [];
89
+ if ($ item instanceof TaggableItemInterface) {
90
+ $ tags = $ item ->getTags ();
91
+ }
92
+
81
93
return $ this ->filesystem ->write ($ file , serialize ([
82
94
($ ttl === null ? null : time () + $ ttl ),
83
95
$ item ->get (),
96
+ $ tags ,
84
97
]));
85
98
}
86
99
@@ -99,4 +112,64 @@ private function getFilePath($key)
99
112
100
113
return sprintf ('%s/%s ' , self ::CACHE_PATH , $ key );
101
114
}
115
+
116
+ public function save (CacheItemInterface $ item )
117
+ {
118
+ if ($ item instanceof TaggableItemInterface) {
119
+ $ this ->saveTags ($ item );
120
+ }
121
+
122
+ return parent ::save ($ item );
123
+ }
124
+
125
+ protected function getList ($ name )
126
+ {
127
+ $ file = $ this ->getFilePath ($ name );
128
+
129
+ if (!$ this ->filesystem ->has ($ file )) {
130
+ $ this ->filesystem ->write ($ file , serialize ([]));
131
+ }
132
+
133
+ return unserialize ($ this ->filesystem ->read ($ file ));
134
+ }
135
+
136
+ protected function removeList ($ name )
137
+ {
138
+ $ file = $ this ->getFilePath ($ name );
139
+ $ this ->filesystem ->delete ($ file );
140
+ }
141
+
142
+ protected function appendListItem ($ name , $ key )
143
+ {
144
+ $ list = $ this ->getList ($ name );
145
+ $ list [] = $ key ;
146
+
147
+ return $ this ->filesystem ->update ($ this ->getFilePath ($ name ), serialize ($ list ));
148
+ }
149
+
150
+ protected function removeListItem ($ name , $ key )
151
+ {
152
+ $ list = $ this ->getList ($ name );
153
+ foreach ($ list as $ i => $ item ) {
154
+ if ($ item === $ key ) {
155
+ unset($ list [$ i ]);
156
+ }
157
+ }
158
+
159
+ return $ this ->filesystem ->update ($ this ->getFilePath ($ name ), serialize ($ list ));
160
+ }
161
+
162
+ /**
163
+ * @param $key
164
+ *
165
+ * @return bool
166
+ */
167
+ private function forceClear ($ key )
168
+ {
169
+ try {
170
+ return $ this ->filesystem ->delete ($ this ->getFilePath ($ key ));
171
+ } catch (FileNotFoundException $ e ) {
172
+ return true ;
173
+ }
174
+ }
102
175
}
0 commit comments