Browse Source

Update to psr/simple-cache v2 & v3.

ADmad 4 years ago
parent
commit
7ce7fda7ab

+ 1 - 1
composer.json

@@ -35,7 +35,7 @@
         "psr/http-server-handler": "^1.0",
         "psr/http-server-middleware": "^1.0",
         "psr/log": "^3.0",
-        "psr/simple-cache": "^1.0.0"
+        "psr/simple-cache": "^2.0 || ^3.0"
     },
     "replace": {
         "cakephp/cache": "self.version",

+ 8 - 8
src/Cache/CacheEngine.php

@@ -135,11 +135,11 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      *
      * @param iterable $keys A list of keys that can obtained in a single operation.
      * @param mixed $default Default value to return for keys that do not exist.
-     * @return iterable A list of key value pairs. Cache keys that do not exist or are stale will have $default as value.
+     * @return iterable<string, mixed> A list of key value pairs. Cache keys that do not exist or are stale will have $default as value.
      * @throws \Cake\Cache\InvalidArgumentException If $keys is neither an array nor a Traversable,
      *   or if any of the $keys are not a legal value.
      */
-    public function getMultiple($keys, $default = null): iterable
+    public function getMultiple(iterable $keys, mixed $default = null): iterable
     {
         $this->ensureValidType($keys);
 
@@ -162,7 +162,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @throws \Cake\Cache\InvalidArgumentException If $values is neither an array nor a Traversable,
      *   or if any of the $values are not a legal value.
      */
-    public function setMultiple($values, $ttl = null): bool
+    public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
     {
         $this->ensureValidType($values, self::CHECK_KEY);
 
@@ -194,7 +194,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @throws \Cake\Cache\InvalidArgumentException If $keys is neither an array nor a Traversable,
      *   or if any of the $keys are not a legal value.
      */
-    public function deleteMultiple($keys): bool
+    public function deleteMultiple(iterable $keys): bool
     {
         $this->ensureValidType($keys);
 
@@ -220,7 +220,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @return bool
      * @throws \Cake\Cache\InvalidArgumentException If the $key string is not a legal value.
      */
-    public function has($key): bool
+    public function has(string $key): bool
     {
         return $this->get($key) !== null;
     }
@@ -233,7 +233,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @return mixed The value of the item from the cache, or $default in case of cache miss.
      * @throws \Cake\Cache\InvalidArgumentException If the $key string is not a legal value.
      */
-    abstract public function get($key, $default = null): mixed;
+    abstract public function get(string $key, mixed $default = null): mixed;
 
     /**
      * Persists data in the cache, uniquely referenced by the given key with an optional expiration TTL time.
@@ -247,7 +247,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @throws \Cake\Cache\InvalidArgumentException
      *   MUST be thrown if the $key string is not a legal value.
      */
-    abstract public function set($key, $value, $ttl = null): bool;
+    abstract public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool;
 
     /**
      * Increment a number under the key and return incremented value
@@ -273,7 +273,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @param string $key Identifier for the data
      * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
      */
-    abstract public function delete($key): bool;
+    abstract public function delete(string $key): bool;
 
     /**
      * Delete all keys from the cache

+ 4 - 3
src/Cache/Engine/ApcuEngine.php

@@ -18,6 +18,7 @@ namespace Cake\Cache\Engine;
 
 use APCUIterator;
 use Cake\Cache\CacheEngine;
+use DateInterval;
 use RuntimeException;
 
 /**
@@ -61,7 +62,7 @@ class ApcuEngine extends CacheEngine
      * @return bool True on success and false on failure.
      * @link https://secure.php.net/manual/en/function.apcu-store.php
      */
-    public function set($key, $value, $ttl = null): bool
+    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
     {
         $key = $this->_key($key);
         $duration = $this->duration($ttl);
@@ -78,7 +79,7 @@ class ApcuEngine extends CacheEngine
      *   has expired, or if there was an error fetching it
      * @link https://secure.php.net/manual/en/function.apcu-fetch.php
      */
-    public function get($key, $default = null): mixed
+    public function get(string $key, mixed $default = null): mixed
     {
         $value = apcu_fetch($this->_key($key), $success);
         if ($success === false) {
@@ -125,7 +126,7 @@ class ApcuEngine extends CacheEngine
      * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
      * @link https://secure.php.net/manual/en/function.apcu-delete.php
      */
-    public function delete($key): bool
+    public function delete(string $key): bool
     {
         $key = $this->_key($key);
 

+ 4 - 3
src/Cache/Engine/ArrayEngine.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\Cache\Engine;
 
 use Cake\Cache\CacheEngine;
+use DateInterval;
 
 /**
  * Array storage engine for cache.
@@ -49,7 +50,7 @@ class ArrayEngine extends CacheEngine
      *   for it or let the driver take care of that.
      * @return bool True on success and false on failure.
      */
-    public function set($key, $value, $ttl = null): bool
+    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
     {
         $key = $this->_key($key);
         $expires = time() + $this->duration($ttl);
@@ -66,7 +67,7 @@ class ArrayEngine extends CacheEngine
      * @return mixed The cached data, or default value if the data doesn't exist, has
      * expired, or if there was an error fetching it.
      */
-    public function get($key, $default = null): mixed
+    public function get(string $key, mixed $default = null): mixed
     {
         $key = $this->_key($key);
         if (!isset($this->data[$key])) {
@@ -127,7 +128,7 @@ class ArrayEngine extends CacheEngine
      * @param string $key Identifier for the data
      * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
      */
-    public function delete($key): bool
+    public function delete(string $key): bool
     {
         $key = $this->_key($key);
         unset($this->data[$key]);

+ 4 - 3
src/Cache/Engine/FileEngine.php

@@ -19,6 +19,7 @@ namespace Cake\Cache\Engine;
 use Cake\Cache\CacheEngine;
 use Cake\Cache\InvalidArgumentException;
 use CallbackFilterIterator;
+use DateInterval;
 use Exception;
 use FilesystemIterator;
 use LogicException;
@@ -111,7 +112,7 @@ class FileEngine extends CacheEngine
      *   for it or let the driver take care of that.
      * @return bool True on success and false on failure.
      */
-    public function set($key, $value, $ttl = null): bool
+    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
     {
         if ($value === '' || !$this->_init) {
             return false;
@@ -157,7 +158,7 @@ class FileEngine extends CacheEngine
      * @return mixed The cached data, or default value if the data doesn't exist, has
      *   expired, or if there was an error fetching it
      */
-    public function get($key, $default = null): mixed
+    public function get(string $key, mixed $default = null): mixed
     {
         $key = $this->_key($key);
 
@@ -211,7 +212,7 @@ class FileEngine extends CacheEngine
      * @return bool True if the value was successfully deleted, false if it didn't
      *   exist or couldn't be removed
      */
-    public function delete($key): bool
+    public function delete(string $key): bool
     {
         $key = $this->_key($key);
 

+ 8 - 7
src/Cache/Engine/MemcachedEngine.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\Cache\Engine;
 
 use Cake\Cache\CacheEngine;
+use DateInterval;
 use InvalidArgumentException;
 use Memcached;
 use RuntimeException;
@@ -306,7 +307,7 @@ class MemcachedEngine extends CacheEngine
      * @return bool True if the data was successfully cached, false on failure
      * @see https://www.php.net/manual/en/memcached.set.php
      */
-    public function set($key, $value, $ttl = null): bool
+    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
     {
         $duration = $this->duration($ttl);
 
@@ -322,7 +323,7 @@ class MemcachedEngine extends CacheEngine
      *   for it or let the driver take care of that.
      * @return bool Whether the write was successful or not.
      */
-    public function setMultiple($values, $ttl = null): bool
+    public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
     {
         $cacheData = [];
         foreach ($values as $key => $value) {
@@ -341,7 +342,7 @@ class MemcachedEngine extends CacheEngine
      * @return mixed The cached data, or default value if the data doesn't exist, has
      * expired, or if there was an error fetching it.
      */
-    public function get($key, $default = null): mixed
+    public function get(string $key, mixed $default = null): mixed
     {
         $key = $this->_key($key);
         $value = $this->_Memcached->get($key);
@@ -357,10 +358,10 @@ class MemcachedEngine extends CacheEngine
      *
      * @param iterable $keys An array of identifiers for the data
      * @param mixed $default Default value to return for keys that do not exist.
-     * @return array An array containing, for each of the given $keys, the cached data or
+     * @return iterable<string, mixed> An array containing, for each of the given $keys, the cached data or
      *   false if cached data could not be retrieved.
      */
-    public function getMultiple($keys, $default = null): array
+    public function getMultiple(iterable $keys, mixed $default = null): iterable
     {
         $cacheKeys = [];
         foreach ($keys as $key) {
@@ -407,7 +408,7 @@ class MemcachedEngine extends CacheEngine
      * @return bool True if the value was successfully deleted, false if it didn't
      *   exist or couldn't be removed.
      */
-    public function delete($key): bool
+    public function delete(string $key): bool
     {
         return $this->_Memcached->delete($this->_key($key));
     }
@@ -419,7 +420,7 @@ class MemcachedEngine extends CacheEngine
      * @return bool of boolean values that are true if the key was successfully
      *   deleted, false if it didn't exist or couldn't be removed.
      */
-    public function deleteMultiple($keys): bool
+    public function deleteMultiple(iterable $keys): bool
     {
         $cacheKeys = [];
         foreach ($keys as $key) {

+ 7 - 6
src/Cache/Engine/NullEngine.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\Cache\Engine;
 
 use Cake\Cache\CacheEngine;
+use DateInterval;
 
 /**
  * Null cache engine, all operations appear to work, but do nothing.
@@ -36,7 +37,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function set($key, $value, $ttl = null): bool
+    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
     {
         return true;
     }
@@ -44,7 +45,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function setMultiple($values, $ttl = null): bool
+    public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
     {
         return true;
     }
@@ -52,7 +53,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function get($key, $default = null): mixed
+    public function get(string $key, mixed $default = null): mixed
     {
         return $default;
     }
@@ -60,7 +61,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function getMultiple($keys, $default = null): iterable
+    public function getMultiple(iterable $keys, mixed $default = null): iterable
     {
         return [];
     }
@@ -84,7 +85,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function delete($key): bool
+    public function delete(string $key): bool
     {
         return true;
     }
@@ -92,7 +93,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function deleteMultiple($keys): bool
+    public function deleteMultiple(iterable $keys): bool
     {
         return true;
     }

+ 4 - 3
src/Cache/Engine/RedisEngine.php

@@ -19,6 +19,7 @@ namespace Cake\Cache\Engine;
 
 use Cake\Cache\CacheEngine;
 use Cake\Log\Log;
+use DateInterval;
 use Redis;
 use RedisException;
 use RuntimeException;
@@ -143,7 +144,7 @@ class RedisEngine extends CacheEngine
      *   for it or let the driver take care of that.
      * @return bool True if the data was successfully cached, false on failure
      */
-    public function set($key, $value, $ttl = null): bool
+    public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
     {
         $key = $this->_key($key);
         $value = $this->serialize($value);
@@ -164,7 +165,7 @@ class RedisEngine extends CacheEngine
      * @return mixed The cached data, or the default if the data doesn't exist, has
      *   expired, or if there was an error fetching it
      */
-    public function get($key, $default = null): mixed
+    public function get(string $key, mixed $default = null): mixed
     {
         $value = $this->_Redis->get($this->_key($key));
         if ($value === false) {
@@ -220,7 +221,7 @@ class RedisEngine extends CacheEngine
      * @param string $key Identifier for the data
      * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
      */
-    public function delete($key): bool
+    public function delete(string $key): bool
     {
         $key = $this->_key($key);
 

+ 2 - 2
src/Cache/composer.json

@@ -24,10 +24,10 @@
     "require": {
         "php": ">=8.0",
         "cakephp/core": "^5.0",
-        "psr/simple-cache": "^1.0.0"
+        "psr/simple-cache": "^2.0 || ^3.0"
     },
     "provide": {
-        "psr/simple-cache-implementation": "^1.0.0"
+        "psr/simple-cache-implementation": "^3.0"
     },
     "autoload": {
         "psr-4": {

+ 0 - 38
tests/TestCase/Cache/Engine/ArrayEngineTest.php

@@ -17,8 +17,6 @@ namespace Cake\Test\TestCase\Cache\Engine;
 
 use ArrayObject;
 use Cake\Cache\Cache;
-use Cake\Cache\Engine\ArrayEngine;
-use Cake\Cache\InvalidArgumentException;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -274,40 +272,4 @@ class ArrayEngineTest extends TestCase
         $this->assertSame(1, Cache::read('a', 'array'));
         $this->assertSame('foo', Cache::read('b', 'array'));
     }
-
-    /**
-     * Test that passing a non iterable argument to setMultiple() throws exception.
-     */
-    public function testSetMultipleException(): void
-    {
-        $this->expectException(InvalidArgumentException::class);
-        $this->expectExceptionMessage('A cache set must be either an array or a Traversable.');
-
-        $engine = new ArrayEngine();
-        $engine->setMultiple('foo');
-    }
-
-    /**
-     * Test that passing a non iterable argument to getMultiple() throws exception.
-     */
-    public function testGetMultipleException(): void
-    {
-        $this->expectException(InvalidArgumentException::class);
-        $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
-
-        $engine = new ArrayEngine();
-        $engine->getMultiple('foo');
-    }
-
-    /**
-     * Test that passing a non iterable argument to deleteMultiple() throws exception.
-     */
-    public function testDeleteMultipleException(): void
-    {
-        $this->expectException(InvalidArgumentException::class);
-        $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
-
-        $engine = new ArrayEngine();
-        $engine->deleteMultiple('foo');
-    }
 }