Skip to content

Commit 7546ee0

Browse files
authored
Merge pull request #4638 from MGatner/mock-cache
Update MockCache
2 parents 2e0e020 + 5cd8181 commit 7546ee0

File tree

7 files changed

+62
-30
lines changed

7 files changed

+62
-30
lines changed

system/Cache/CacheInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getCacheInfo();
110110
*
111111
* @return array|false|null
112112
* Returns null if the item does not exist, otherwise array<string, mixed>
113-
* with at least the 'expires' key for absolute epoch expiry (or null).
113+
* with at least the 'expire' key for absolute epoch expiry (or null).
114114
* Some handlers may return false when an item does not exist, which is deprecated.
115115
*/
116116
public function getMetaData(string $key);

system/Cache/Handlers/FileHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function getCacheInfo()
278278
*
279279
* @return array|false|null
280280
* Returns null if the item does not exist, otherwise array<string, mixed>
281-
* with at least the 'expires' key for absolute epoch expiry (or null).
281+
* with at least the 'expire' key for absolute epoch expiry (or null).
282282
* Some handlers may return false when an item does not exist, which is deprecated.
283283
*/
284284
public function getMetaData(string $key)

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function getCacheInfo()
337337
*
338338
* @return array|false|null
339339
* Returns null if the item does not exist, otherwise array<string, mixed>
340-
* with at least the 'expires' key for absolute epoch expiry (or null).
340+
* with at least the 'expire' key for absolute epoch expiry (or null).
341341
* Some handlers may return false when an item does not exist, which is deprecated.
342342
*/
343343
public function getMetaData(string $key)

system/Cache/Handlers/PredisHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function getCacheInfo()
274274
*
275275
* @return array|false|null
276276
* Returns null if the item does not exist, otherwise array<string, mixed>
277-
* with at least the 'expires' key for absolute epoch expiry (or null).
277+
* with at least the 'expire' key for absolute epoch expiry (or null).
278278
*/
279279
public function getMetaData(string $key)
280280
{

system/Cache/Handlers/RedisHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public function getCacheInfo()
316316
*
317317
* @return array|null
318318
* Returns null if the item does not exist, otherwise array<string, mixed>
319-
* with at least the 'expires' key for absolute epoch expiry (or null).
319+
* with at least the 'expire' key for absolute epoch expiry (or null).
320320
*/
321321
public function getMetaData(string $key)
322322
{

system/Cache/Handlers/WincacheHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function getCacheInfo()
178178
*
179179
* @return array|false|null
180180
* Returns null if the item does not exist, otherwise array<string, mixed>
181-
* with at least the 'expires' key for absolute epoch expiry (or null).
181+
* with at least the 'expire' key for absolute epoch expiry (or null).
182182
* Some handlers may return false when an item does not exist, which is deprecated.
183183
*/
184184
public function getMetaData(string $key)

system/Test/Mock/MockCache.php

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@
1212
namespace CodeIgniter\Test\Mock;
1313

1414
use CodeIgniter\Cache\CacheInterface;
15+
use CodeIgniter\Cache\Handlers\BaseHandler;
1516
use Closure;
1617

17-
class MockCache implements CacheInterface
18+
class MockCache extends BaseHandler implements CacheInterface
1819
{
1920
/**
20-
* Prefixed to all cache names.
21+
* Mock cache storage.
2122
*
22-
* @var string
23+
* @var array
2324
*/
24-
protected $prefix;
25+
protected $cache = [];
2526

2627
/**
27-
* Mock cache storage.
28+
* Expiration times.
2829
*
29-
* @var array
30+
* @var ?int[]
3031
*/
31-
protected $cache = [];
32+
protected $expirations = [];
3233

3334
//--------------------------------------------------------------------
3435

@@ -37,7 +38,6 @@ class MockCache implements CacheInterface
3738
*/
3839
public function initialize()
3940
{
40-
// Not to see here...
4141
}
4242

4343
//--------------------------------------------------------------------
@@ -51,7 +51,7 @@ public function initialize()
5151
*/
5252
public function get(string $key)
5353
{
54-
$key = $this->prefix . $key;
54+
$key = static::validateKey($key, $this->prefix);
5555

5656
return array_key_exists($key, $this->cache)
5757
? $this->cache[$key]
@@ -96,13 +96,14 @@ public function remember(string $key, int $ttl, Closure $callback)
9696
* @param integer $ttl Time To Live, in seconds (default 60)
9797
* @param boolean $raw Whether to store the raw value.
9898
*
99-
* @return mixed
99+
* @return boolean
100100
*/
101101
public function save(string $key, $value, int $ttl = 60, bool $raw = false)
102102
{
103-
$key = $this->prefix . $key;
103+
$key = static::validateKey($key, $this->prefix);
104104

105-
$this->cache[$key] = $value;
105+
$this->cache[$key] = $value;
106+
$this->expirations[$key] = $ttl > 0 ? time() + $ttl : null;
106107

107108
return true;
108109
}
@@ -114,11 +115,21 @@ public function save(string $key, $value, int $ttl = 60, bool $raw = false)
114115
*
115116
* @param string $key Cache item name
116117
*
117-
* @return mixed
118+
* @return boolean
118119
*/
119120
public function delete(string $key)
120121
{
122+
$key = static::validateKey($key, $this->prefix);
123+
124+
if (! isset($this->cache[$key]))
125+
{
126+
return false;
127+
}
128+
121129
unset($this->cache[$key]);
130+
unset($this->expirations[$key]);
131+
132+
return true;
122133
}
123134

124135
//--------------------------------------------------------------------
@@ -128,19 +139,22 @@ public function delete(string $key)
128139
*
129140
* @param string $pattern Cache items glob-style pattern
130141
*
131-
* @return boolean
142+
* @return integer
132143
*/
133144
public function deleteMatching(string $pattern)
134145
{
146+
$count = 0;
135147
foreach (array_keys($this->cache) as $key)
136148
{
137149
if (fnmatch($pattern, $key))
138150
{
151+
$count++;
139152
unset($this->cache[$key]);
153+
unset($this->expirations[$key]);
140154
}
141155
}
142156

143-
return true;
157+
return $count;
144158
}
145159

146160
//--------------------------------------------------------------------
@@ -151,12 +165,11 @@ public function deleteMatching(string $pattern)
151165
* @param string $key Cache ID
152166
* @param integer $offset Step/value to increase by
153167
*
154-
* @return mixed
168+
* @return boolean
155169
*/
156170
public function increment(string $key, int $offset = 1)
157171
{
158-
$key = $this->prefix . $key;
159-
172+
$key = static::validateKey($key, $this->prefix);
160173
$data = $this->cache[$key] ?: null;
161174

162175
if (empty($data))
@@ -179,11 +192,11 @@ public function increment(string $key, int $offset = 1)
179192
* @param string $key Cache ID
180193
* @param integer $offset Step/value to increase by
181194
*
182-
* @return mixed
195+
* @return boolean
183196
*/
184197
public function decrement(string $key, int $offset = 1)
185198
{
186-
$key = $this->prefix . $key;
199+
$key = static::validateKey($key, $this->prefix);
187200

188201
$data = $this->cache[$key] ?: null;
189202

@@ -204,11 +217,14 @@ public function decrement(string $key, int $offset = 1)
204217
/**
205218
* Will delete all items in the entire cache.
206219
*
207-
* @return mixed
220+
* @return boolean
208221
*/
209222
public function clean()
210223
{
211-
$this->cache = [];
224+
$this->cache = [];
225+
$this->expirations = [];
226+
227+
return true;
212228
}
213229

214230
//--------------------------------------------------------------------
@@ -233,11 +249,27 @@ public function getCacheInfo()
233249
*
234250
* @param string $key Cache item name.
235251
*
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).
237255
*/
238256
public function getMetaData(string $key)
239257
{
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+
];
241273
}
242274

243275
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)