diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index 21368a4..0bbacb1 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -60,4 +60,23 @@ abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface { return $output; } + + /** + * Set multiple cache values + * + * @param array $items + * @param int|null $expires + * @return bool + */ + public function setMultiple(array $items, ?int $expires = NULL): bool + { + $setResults = []; + foreach ($items as $k => $v) + { + $setResults[] = $this->set($k, $v, $expires); + } + + // Only return true if all the results are true + return array_reduce($setResults, fn ($carry, $item) => $item && $carry, TRUE); + } } \ No newline at end of file diff --git a/src/Driver/ApcuDriver.php b/src/Driver/ApcuDriver.php index 4ed9dbd..616417d 100644 --- a/src/Driver/ApcuDriver.php +++ b/src/Driver/ApcuDriver.php @@ -86,13 +86,29 @@ class ApcuDriver extends AbstractDriver { * @param int $expires * @return bool */ - public function set(string $key, $value, ?int $expires = 0): bool + public function set(string $key, $value, ?int $expires = NULL): bool { $ttl = $this->getTTLFromExpiration($expires); return apcu_store($key, $value, $ttl); } + /** + * Set multiple cache values + * + * @param array $items + * @param int|null $expires + * @return bool + */ + public function setMultiple(array $items, ?int $expires = NULL): bool + { + $ttl = $this->getTTLFromExpiration((int)$expires); + + return ($expires === NULL) + ? apcu_store($items) + : apcu_store($items, NULL, $ttl); + } + /** * Remove an item from the cache * @@ -153,9 +169,9 @@ class ApcuDriver extends AbstractDriver { * @param int $expires * @return int */ - protected function getTTLFromExpiration(int $expires): int + protected function getTTLFromExpiration($expires): int { - $ttl = $expires - time(); + $ttl = (int)$expires - time(); return ($ttl < 0) ? 0 : $ttl; } diff --git a/src/Driver/DriverInterface.php b/src/Driver/DriverInterface.php index 4dd6356..61263a3 100644 --- a/src/Driver/DriverInterface.php +++ b/src/Driver/DriverInterface.php @@ -36,7 +36,7 @@ interface DriverInterface { * @param int $expires * @return bool */ - public function set(string $key, $value, ?int $expires = 0): bool; + public function set(string $key, $value, ?int $expires = NULL): bool; /** * Get the value for the selected cache key @@ -54,6 +54,15 @@ interface DriverInterface { */ public function getMultiple(array $keys = []): array; + /** + * Set multiple cache values + * + * @param array $items + * @param int|null $expires + * @return bool + */ + public function setMultiple(array $items, ?int $expires = NULL): bool; + /** * Remove an item from the cache * diff --git a/src/Driver/MemcachedDriver.php b/src/Driver/MemcachedDriver.php index a244ece..036deb8 100644 --- a/src/Driver/MemcachedDriver.php +++ b/src/Driver/MemcachedDriver.php @@ -121,9 +121,25 @@ class MemcachedDriver extends AbstractDriver { * @param int $expires * @return bool */ - public function set(string $key, $value, ?int $expires = 0): bool + public function set(string $key, $value, ?int $expires = NULL): bool { - return $this->conn->set($key, $value, $expires); + return ($expires === NULL) + ? $this->conn->set($key, $value) + : $this->conn->set($key, $value, $expires); + } + + /** + * Set multiple cache values + * + * @param array $items + * @param int|null $expires + * @return bool + */ + public function setMultiple(array $items, ?int $expires = NULL): bool + { + return ($expires === NULL) + ? $this->conn->setMulti($items) + : $this->conn->setMulti($items, $expires); } /** diff --git a/src/Driver/NullDriver.php b/src/Driver/NullDriver.php index 9c8aacc..c443bc1 100644 --- a/src/Driver/NullDriver.php +++ b/src/Driver/NullDriver.php @@ -72,7 +72,7 @@ class NullDriver extends AbstractDriver { * @param int $expires * @return bool */ - public function set(string $key, $value, ?int $expires = 0): bool + public function set(string $key, $value, ?int $expires = NULL): bool { $this->store[$key] = $value; return $this->store[$key] === $value; diff --git a/src/Driver/RedisDriver.php b/src/Driver/RedisDriver.php index dfc6b69..f29838f 100644 --- a/src/Driver/RedisDriver.php +++ b/src/Driver/RedisDriver.php @@ -88,16 +88,16 @@ class RedisDriver extends AbstractDriver { * @param int $expires * @return bool */ - public function set(string $key, $value, ?int $expires = 0): bool + public function set(string $key, $value, ?int $expires = NULL): bool { $value = serialize($value); - if ($expires !== 0) + if ($expires !== NULL) { - return (bool) $this->conn->set($key, $value, 'EX', $expires); + return $this->conn->set($key, $value, 'EX', $expires) === 'OK'; } - return (bool)$this->conn->set($key, $value); + return $this->conn->set($key, $value) === 'OK'; } /** diff --git a/src/ItemCollection.php b/src/ItemCollection.php index 9b72c86..5628b34 100644 --- a/src/ItemCollection.php +++ b/src/ItemCollection.php @@ -33,7 +33,7 @@ class ItemCollection extends ArrayIterator implements JsonSerializable { * * @var CacheItemInterface[] */ - protected $items = []; + protected array $items = []; /** * Create the collection object from the raw diff --git a/src/Teller.php b/src/Teller.php index 2dfccb3..325389d 100644 --- a/src/Teller.php +++ b/src/Teller.php @@ -145,6 +145,10 @@ class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface { { $this->validateKeys($values, TRUE); + return ($ttl === NULL) + ? $this->driver->setMultiple((array)$values) + : $this->driver->setMultiple((array)$values, $ttl); + $setResults = []; foreach ($values as $k => $v) {