12
12
namespace CodeIgniter \Test \Mock ;
13
13
14
14
use CodeIgniter \Cache \CacheInterface ;
15
+ use CodeIgniter \Cache \Handlers \BaseHandler ;
15
16
use Closure ;
16
17
17
- class MockCache implements CacheInterface
18
+ class MockCache extends BaseHandler implements CacheInterface
18
19
{
19
20
/**
20
- * Prefixed to all cache names .
21
+ * Mock cache storage .
21
22
*
22
- * @var string
23
+ * @var array
23
24
*/
24
- protected $ prefix ;
25
+ protected $ cache = [] ;
25
26
26
27
/**
27
- * Mock cache storage .
28
+ * Expiration times .
28
29
*
29
- * @var array
30
+ * @var ?int[]
30
31
*/
31
- protected $ cache = [];
32
+ protected $ expirations = [];
32
33
33
34
//--------------------------------------------------------------------
34
35
@@ -37,7 +38,6 @@ class MockCache implements CacheInterface
37
38
*/
38
39
public function initialize ()
39
40
{
40
- // Not to see here...
41
41
}
42
42
43
43
//--------------------------------------------------------------------
@@ -51,7 +51,7 @@ public function initialize()
51
51
*/
52
52
public function get (string $ key )
53
53
{
54
- $ key = $ this ->prefix . $ key ;
54
+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
55
55
56
56
return array_key_exists ($ key , $ this ->cache )
57
57
? $ this ->cache [$ key ]
@@ -96,13 +96,14 @@ public function remember(string $key, int $ttl, Closure $callback)
96
96
* @param integer $ttl Time To Live, in seconds (default 60)
97
97
* @param boolean $raw Whether to store the raw value.
98
98
*
99
- * @return mixed
99
+ * @return boolean
100
100
*/
101
101
public function save (string $ key , $ value , int $ ttl = 60 , bool $ raw = false )
102
102
{
103
- $ key = $ this ->prefix . $ key ;
103
+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
104
104
105
- $ this ->cache [$ key ] = $ value ;
105
+ $ this ->cache [$ key ] = $ value ;
106
+ $ this ->expirations [$ key ] = $ ttl > 0 ? time () + $ ttl : null ;
106
107
107
108
return true ;
108
109
}
@@ -114,11 +115,21 @@ public function save(string $key, $value, int $ttl = 60, bool $raw = false)
114
115
*
115
116
* @param string $key Cache item name
116
117
*
117
- * @return mixed
118
+ * @return boolean
118
119
*/
119
120
public function delete (string $ key )
120
121
{
122
+ $ key = static ::validateKey ($ key , $ this ->prefix );
123
+
124
+ if (! isset ($ this ->cache [$ key ]))
125
+ {
126
+ return false ;
127
+ }
128
+
121
129
unset($ this ->cache [$ key ]);
130
+ unset($ this ->expirations [$ key ]);
131
+
132
+ return true ;
122
133
}
123
134
124
135
//--------------------------------------------------------------------
@@ -128,19 +139,22 @@ public function delete(string $key)
128
139
*
129
140
* @param string $pattern Cache items glob-style pattern
130
141
*
131
- * @return boolean
142
+ * @return integer
132
143
*/
133
144
public function deleteMatching (string $ pattern )
134
145
{
146
+ $ count = 0 ;
135
147
foreach (array_keys ($ this ->cache ) as $ key )
136
148
{
137
149
if (fnmatch ($ pattern , $ key ))
138
150
{
151
+ $ count ++;
139
152
unset($ this ->cache [$ key ]);
153
+ unset($ this ->expirations [$ key ]);
140
154
}
141
155
}
142
156
143
- return true ;
157
+ return $ count ;
144
158
}
145
159
146
160
//--------------------------------------------------------------------
@@ -151,12 +165,11 @@ public function deleteMatching(string $pattern)
151
165
* @param string $key Cache ID
152
166
* @param integer $offset Step/value to increase by
153
167
*
154
- * @return mixed
168
+ * @return boolean
155
169
*/
156
170
public function increment (string $ key , int $ offset = 1 )
157
171
{
158
- $ key = $ this ->prefix . $ key ;
159
-
172
+ $ key = static ::validateKey ($ key , $ this ->prefix );
160
173
$ data = $ this ->cache [$ key ] ?: null ;
161
174
162
175
if (empty ($ data ))
@@ -179,11 +192,11 @@ public function increment(string $key, int $offset = 1)
179
192
* @param string $key Cache ID
180
193
* @param integer $offset Step/value to increase by
181
194
*
182
- * @return mixed
195
+ * @return boolean
183
196
*/
184
197
public function decrement (string $ key , int $ offset = 1 )
185
198
{
186
- $ key = $ this ->prefix . $ key ;
199
+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
187
200
188
201
$ data = $ this ->cache [$ key ] ?: null ;
189
202
@@ -204,11 +217,14 @@ public function decrement(string $key, int $offset = 1)
204
217
/**
205
218
* Will delete all items in the entire cache.
206
219
*
207
- * @return mixed
220
+ * @return boolean
208
221
*/
209
222
public function clean ()
210
223
{
211
- $ this ->cache = [];
224
+ $ this ->cache = [];
225
+ $ this ->expirations = [];
226
+
227
+ return true ;
212
228
}
213
229
214
230
//--------------------------------------------------------------------
@@ -233,11 +249,27 @@ public function getCacheInfo()
233
249
*
234
250
* @param string $key Cache item name.
235
251
*
236
- * @return mixed
252
+ * @return array|null
253
+ * Returns null if the item does not exist, otherwise array<string, mixed>
254
+ * with at least the 'expire' key for absolute epoch expiry (or null).
237
255
*/
238
256
public function getMetaData (string $ key )
239
257
{
240
- return false ;
258
+ // Misses return null
259
+ if (! array_key_exists ($ key , $ this ->expirations ))
260
+ {
261
+ return null ;
262
+ }
263
+
264
+ // Count expired items as a miss
265
+ if (is_int ($ this ->expirations [$ key ]) && $ this ->expirations [$ key ] > time ())
266
+ {
267
+ return null ;
268
+ }
269
+
270
+ return [
271
+ 'expire ' => $ this ->expirations [$ key ],
272
+ ];
241
273
}
242
274
243
275
//--------------------------------------------------------------------
0 commit comments