Skip to content

Commit 073ed9c

Browse files
committed
Complete first draft of simple cache adapter implementation.
1 parent ec42f97 commit 073ed9c

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @since 3.7.0
13+
* @license https://opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\Cache;
16+
17+
use Cake\Core\Exception\Exception;
18+
use Psr\SimpleCache\InvalidArgumentException as InvalidInterface;
19+
20+
/**
21+
* Exception raised when cache keys are invalid.
22+
*/
23+
class InvalidArgumentException extends Exception implements InvalidInterface
24+
{
25+
}

src/Cache/SimpleCacheEngine.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
namespace Cake\Cache;
33

44
use Cake\Cache\CacheEngine;
5+
use Cake\Cache\InvalidArgumentException;
56
use Psr\SimpleCache\CacheInterface;
6-
use Psr\SimpleCache\InvalidArgumentException;
77

88
/**
99
* Wrapper for Cake engines that allow them to support
@@ -23,6 +23,20 @@ public function __construct($inner)
2323
$this->inner = $inner;
2424
}
2525

26+
/**
27+
* Check key for validity.
28+
*
29+
* @param string $key Key to check.
30+
* @return void
31+
* @throws \Psr\SimpleCache\InvalidArgumentException when key is not valid.
32+
*/
33+
protected function checkKey($key)
34+
{
35+
if (!is_string($key) || strlen($key) === 0) {
36+
throw new InvalidArgumentException('Cache keys must be non-empty strings.');
37+
}
38+
}
39+
2640
/**
2741
* Fetches a value from the cache.
2842
*
@@ -34,6 +48,7 @@ public function __construct($inner)
3448
*/
3549
public function get($key, $default = null)
3650
{
51+
$this->checkKey($key);
3752
$result = $this->inner->read($key);
3853
if ($result === false) {
3954
return $default;
@@ -56,6 +71,7 @@ public function get($key, $default = null)
5671
*/
5772
public function set($key, $value, $ttl = null)
5873
{
74+
$this->checkKey($key);
5975
if ($ttl !== null) {
6076
$restore = $this->inner->getConfig('duration');
6177
$this->inner->setConfig('duration', $ttl);
@@ -81,6 +97,8 @@ public function set($key, $value, $ttl = null)
8197
*/
8298
public function delete($key)
8399
{
100+
$this->checkKey($key);
101+
84102
return $this->inner->delete($key);
85103
}
86104

@@ -154,6 +172,14 @@ public function setMultiple($values, $ttl = null)
154172
*/
155173
public function deleteMultiple($keys)
156174
{
175+
$result = $this->inner->deleteMany($keys);
176+
foreach ($result as $key => $success) {
177+
if ($success === false) {
178+
return false;
179+
}
180+
}
181+
182+
return true;
157183
}
158184

159185
/**
@@ -171,5 +197,6 @@ public function deleteMultiple($keys)
171197
*/
172198
public function has($key)
173199
{
200+
return $this->get($key) !== null;
174201
}
175202
}

tests/TestCase/Cache/SimpleCacheEngineTest.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Cake\Cache\Engine\FileEngine;
1919
use Cake\Cache\SimpleCacheEngine;
2020
use Cake\TestSuite\TestCase;
21+
use Psr\SimpleCache\InvalidArgumentException;
2122

2223
/**
2324
* SimpleCacheEngine class
@@ -59,7 +60,8 @@ public function testGetNoKey()
5960

6061
public function testGetInvalidKey()
6162
{
62-
$this->markTestIncomplete();
63+
$this->expectException(InvalidArgumentException::class);
64+
$this->cache->get('');
6365
}
6466

6567
public function testSetNoTtl()
@@ -76,12 +78,13 @@ public function testSetWithTtl()
7678
sleep(1);
7779
$this->assertSame('a value', $this->cache->get('key'));
7880
$this->assertNull($this->cache->get('expired'));
79-
$this->assertNotEquals(0, $this->inner->getConfig('duration'));
81+
$this->assertSame(5, $this->inner->getConfig('duration'));
8082
}
8183

8284
public function testSetInvalidKey()
8385
{
84-
$this->markTestIncomplete();
86+
$this->expectException(InvalidArgumentException::class);
87+
$this->cache->set('', 'some data');
8588
}
8689

8790
public function testDelete()
@@ -93,7 +96,8 @@ public function testDelete()
9396

9497
public function testDeleteInvalidKey()
9598
{
96-
$this->markTestIncomplete();
99+
$this->expectException(InvalidArgumentException::class);
100+
$this->cache->delete('');
97101
}
98102

99103
public function testClear()
@@ -158,6 +162,43 @@ public function testSetMultipleWithTtl()
158162
$results = $this->cache->getMultiple(array_keys($data));
159163
$this->assertNull($results['key']);
160164
$this->assertNull($results['key2']);
161-
$this->assertGreaterThan(1, $this->inner->getConfig('duration'));
165+
$this->assertSame(5, $this->inner->getConfig('duration'));
166+
}
167+
168+
public function testDeleteMultiple()
169+
{
170+
$data = [
171+
'key' => 'a value',
172+
'key2' => 'other value',
173+
'key3' => 'more data',
174+
];
175+
$this->cache->setMultiple($data);
176+
$this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
177+
$this->assertNull($this->cache->get('key'));
178+
$this->assertNull($this->cache->get('key3'));
179+
$this->assertSame('other value', $this->cache->get('key2'));
180+
}
181+
182+
public function testDeleteMultipleSomeMisses()
183+
{
184+
$data = [
185+
'key' => 'a value',
186+
];
187+
$this->cache->setMultiple($data);
188+
$this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
189+
}
190+
191+
public function testHas()
192+
{
193+
$this->assertFalse($this->cache->has('key'));
194+
195+
$this->cache->set('key', 'value');
196+
$this->assertTrue($this->cache->has('key'));
197+
}
198+
199+
public function testHasInvalidKey()
200+
{
201+
$this->expectException(InvalidArgumentException::class);
202+
$this->cache->has('');
162203
}
163204
}

0 commit comments

Comments
 (0)