Fix bug with handling of optional expirations, use driver to get multiple items if possible
Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2020-05-08 15:53:47 -04:00
parent fa0fb9ce1c
commit 7c121934a2
8 changed files with 76 additions and 12 deletions

View File

@ -60,4 +60,23 @@ abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface {
return $output; 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);
}
} }

View File

@ -86,13 +86,29 @@ class ApcuDriver extends AbstractDriver {
* @param int $expires * @param int $expires
* @return bool * @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); $ttl = $this->getTTLFromExpiration($expires);
return apcu_store($key, $value, $ttl); 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 * Remove an item from the cache
* *
@ -153,9 +169,9 @@ class ApcuDriver extends AbstractDriver {
* @param int $expires * @param int $expires
* @return int * @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; return ($ttl < 0) ? 0 : $ttl;
} }

View File

@ -36,7 +36,7 @@ interface DriverInterface {
* @param int $expires * @param int $expires
* @return bool * @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 * Get the value for the selected cache key
@ -54,6 +54,15 @@ interface DriverInterface {
*/ */
public function getMultiple(array $keys = []): array; 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 * Remove an item from the cache
* *

View File

@ -121,9 +121,25 @@ class MemcachedDriver extends AbstractDriver {
* @param int $expires * @param int $expires
* @return bool * @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);
} }
/** /**

View File

@ -72,7 +72,7 @@ class NullDriver extends AbstractDriver {
* @param int $expires * @param int $expires
* @return bool * @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; $this->store[$key] = $value;
return $this->store[$key] === $value; return $this->store[$key] === $value;

View File

@ -88,16 +88,16 @@ class RedisDriver extends AbstractDriver {
* @param int $expires * @param int $expires
* @return bool * @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); $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';
} }
/** /**

View File

@ -33,7 +33,7 @@ class ItemCollection extends ArrayIterator implements JsonSerializable {
* *
* @var CacheItemInterface[] * @var CacheItemInterface[]
*/ */
protected $items = []; protected array $items = [];
/** /**
* Create the collection object from the raw * Create the collection object from the raw

View File

@ -145,6 +145,10 @@ class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
{ {
$this->validateKeys($values, TRUE); $this->validateKeys($values, TRUE);
return ($ttl === NULL)
? $this->driver->setMultiple((array)$values)
: $this->driver->setMultiple((array)$values, $ttl);
$setResults = []; $setResults = [];
foreach ($values as $k => $v) foreach ($values as $k => $v)
{ {