Add support for getMultiple(), setMultiple(), deleteMultiple(), clear() and has() (inspired by PSR-16) by krlv · Pull Request #32 · reactphp/cache · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions README.md
57 changes: 57 additions & 0 deletions src/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Cache;

use React\Promise;
use React\Promise\PromiseInterface;

class ArrayCache implements CacheInterface
{
Expand Down Expand Up @@ -99,4 +100,60 @@ public function delete($key)

return Promise\resolve(true);
}

public function getMultiple($keys, $default = null)
{
$values = array();

foreach ($keys as $key) {
$values[$key] = $this->get($key, $default);
}

return Promise\all($values);
}

public function setMultiple($values, $ttl = null)
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
}

return Promise\resolve(true);
}

public function deleteMultiple($keys)
{
foreach ($keys as $key) {
unset($this->data[$key], $this->expires[$key]);
}

return Promise\resolve(true);
}

public function clear()
{
$this->data = array();
$this->expires = array();

return Promise\resolve(true);
}

public function has($key)
{
// delete key if it is already expired
if (isset($this->expires[$key]) && $this->expires[$key] < \microtime(true)) {
unset($this->data[$key], $this->expires[$key]);
}

if (!\array_key_exists($key, $this->data)) {
return Promise\resolve(false);
}

// remove and append to end of array to keep track of LRU info
$value = $this->data[$key];
unset($this->data[$key]);
$this->data[$key] = $value;

return Promise\resolve(true);
}
}
92 changes: 92 additions & 0 deletions src/CacheInterface.php
Loading