Skip to content

Commit

Permalink
Complete first draft of simple cache adapter implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 30, 2018
1 parent ec42f97 commit 073ed9c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/Cache/InvalidArgumentException.php
@@ -0,0 +1,25 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.7.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Cache;

use Cake\Core\Exception\Exception;
use Psr\SimpleCache\InvalidArgumentException as InvalidInterface;

/**
* Exception raised when cache keys are invalid.
*/
class InvalidArgumentException extends Exception implements InvalidInterface
{
}
29 changes: 28 additions & 1 deletion src/Cache/SimpleCacheEngine.php
Expand Up @@ -2,8 +2,8 @@
namespace Cake\Cache;

use Cake\Cache\CacheEngine;
use Cake\Cache\InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;

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

/**
* Check key for validity.
*
* @param string $key Key to check.
* @return void
* @throws \Psr\SimpleCache\InvalidArgumentException when key is not valid.
*/
protected function checkKey($key)
{
if (!is_string($key) || strlen($key) === 0) {
throw new InvalidArgumentException('Cache keys must be non-empty strings.');
}
}

/**
* Fetches a value from the cache.
*
Expand All @@ -34,6 +48,7 @@ public function __construct($inner)
*/
public function get($key, $default = null)
{
$this->checkKey($key);
$result = $this->inner->read($key);
if ($result === false) {
return $default;
Expand All @@ -56,6 +71,7 @@ public function get($key, $default = null)
*/
public function set($key, $value, $ttl = null)
{
$this->checkKey($key);
if ($ttl !== null) {
$restore = $this->inner->getConfig('duration');
$this->inner->setConfig('duration', $ttl);
Expand All @@ -81,6 +97,8 @@ public function set($key, $value, $ttl = null)
*/
public function delete($key)
{
$this->checkKey($key);

return $this->inner->delete($key);
}

Expand Down Expand Up @@ -154,6 +172,14 @@ public function setMultiple($values, $ttl = null)
*/
public function deleteMultiple($keys)
{
$result = $this->inner->deleteMany($keys);
foreach ($result as $key => $success) {
if ($success === false) {
return false;
}
}

return true;
}

/**
Expand All @@ -171,5 +197,6 @@ public function deleteMultiple($keys)
*/
public function has($key)
{
return $this->get($key) !== null;
}
}
51 changes: 46 additions & 5 deletions tests/TestCase/Cache/SimpleCacheEngineTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Cache\Engine\FileEngine;
use Cake\Cache\SimpleCacheEngine;
use Cake\TestSuite\TestCase;
use Psr\SimpleCache\InvalidArgumentException;

/**
* SimpleCacheEngine class
Expand Down Expand Up @@ -59,7 +60,8 @@ public function testGetNoKey()

public function testGetInvalidKey()
{
$this->markTestIncomplete();
$this->expectException(InvalidArgumentException::class);
$this->cache->get('');
}

public function testSetNoTtl()
Expand All @@ -76,12 +78,13 @@ public function testSetWithTtl()
sleep(1);
$this->assertSame('a value', $this->cache->get('key'));
$this->assertNull($this->cache->get('expired'));
$this->assertNotEquals(0, $this->inner->getConfig('duration'));
$this->assertSame(5, $this->inner->getConfig('duration'));
}

public function testSetInvalidKey()
{
$this->markTestIncomplete();
$this->expectException(InvalidArgumentException::class);
$this->cache->set('', 'some data');
}

public function testDelete()
Expand All @@ -93,7 +96,8 @@ public function testDelete()

public function testDeleteInvalidKey()
{
$this->markTestIncomplete();
$this->expectException(InvalidArgumentException::class);
$this->cache->delete('');
}

public function testClear()
Expand Down Expand Up @@ -158,6 +162,43 @@ public function testSetMultipleWithTtl()
$results = $this->cache->getMultiple(array_keys($data));
$this->assertNull($results['key']);
$this->assertNull($results['key2']);
$this->assertGreaterThan(1, $this->inner->getConfig('duration'));
$this->assertSame(5, $this->inner->getConfig('duration'));
}

public function testDeleteMultiple()
{
$data = [
'key' => 'a value',
'key2' => 'other value',
'key3' => 'more data',
];
$this->cache->setMultiple($data);
$this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
$this->assertNull($this->cache->get('key'));
$this->assertNull($this->cache->get('key3'));
$this->assertSame('other value', $this->cache->get('key2'));
}

public function testDeleteMultipleSomeMisses()
{
$data = [
'key' => 'a value',
];
$this->cache->setMultiple($data);
$this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
}

public function testHas()
{
$this->assertFalse($this->cache->has('key'));

$this->cache->set('key', 'value');
$this->assertTrue($this->cache->has('key'));
}

public function testHasInvalidKey()
{
$this->expectException(InvalidArgumentException::class);
$this->cache->has('');
}
}

0 comments on commit 073ed9c

Please sign in to comment.