Browse Source

Complete first draft of simple cache adapter implementation.

Mark Story 7 years ago
parent
commit
073ed9c313

+ 25 - 0
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
+{
+}

+ 28 - 1
src/Cache/SimpleCacheEngine.php

@@ -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
@@ -24,6 +24,20 @@ class SimpleCacheEngine implements CacheInterface
     }
 
     /**
+     * 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.
      *
      * @param string $key The unique key of this item in the cache.
@@ -34,6 +48,7 @@ class SimpleCacheEngine implements CacheInterface
      */
     public function get($key, $default = null)
     {
+        $this->checkKey($key);
         $result = $this->inner->read($key);
         if ($result === false) {
             return $default;
@@ -56,6 +71,7 @@ class SimpleCacheEngine implements CacheInterface
      */
     public function set($key, $value, $ttl = null)
     {
+        $this->checkKey($key);
         if ($ttl !== null) {
             $restore = $this->inner->getConfig('duration');
             $this->inner->setConfig('duration', $ttl);
@@ -81,6 +97,8 @@ class SimpleCacheEngine implements CacheInterface
      */
     public function delete($key)
     {
+        $this->checkKey($key);
+
         return $this->inner->delete($key);
     }
 
@@ -154,6 +172,14 @@ class SimpleCacheEngine implements CacheInterface
      */
     public function deleteMultiple($keys)
     {
+        $result = $this->inner->deleteMany($keys);
+        foreach ($result as $key => $success) {
+            if ($success === false) {
+                return false;
+            }
+        }
+
+        return true;
     }
 
     /**
@@ -171,5 +197,6 @@ class SimpleCacheEngine implements CacheInterface
      */
     public function has($key)
     {
+        return $this->get($key) !== null;
     }
 }

+ 46 - 5
tests/TestCase/Cache/SimpleCacheEngineTest.php

@@ -18,6 +18,7 @@ use Cake\Cache\Cache;
 use Cake\Cache\Engine\FileEngine;
 use Cake\Cache\SimpleCacheEngine;
 use Cake\TestSuite\TestCase;
+use Psr\SimpleCache\InvalidArgumentException;
 
 /**
  * SimpleCacheEngine class
@@ -59,7 +60,8 @@ class SimpleCacheEngineTest extends TestCase
 
     public function testGetInvalidKey()
     {
-        $this->markTestIncomplete();
+        $this->expectException(InvalidArgumentException::class);
+        $this->cache->get('');
     }
 
     public function testSetNoTtl()
@@ -76,12 +78,13 @@ class SimpleCacheEngineTest extends TestCase
         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()
@@ -93,7 +96,8 @@ class SimpleCacheEngineTest extends TestCase
 
     public function testDeleteInvalidKey()
     {
-        $this->markTestIncomplete();
+        $this->expectException(InvalidArgumentException::class);
+        $this->cache->delete('');
     }
 
     public function testClear()
@@ -158,6 +162,43 @@ class SimpleCacheEngineTest extends TestCase
         $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('');
     }
 }