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
24 changes: 11 additions & 13 deletions src/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Cache;

use React\Promise\PromiseInterface;
use function React\Promise\all;
use function React\Promise\resolve;

Expand Down Expand Up @@ -49,15 +50,15 @@ class ArrayCache implements CacheInterface
*
* @param int|null $limit maximum number of entries to store in the LRU cache
*/
public function __construct($limit = null)
public function __construct(?int $limit = null)
{
$this->limit = $limit;

// prefer high-resolution timer, available as of PHP 7.3+
$this->supportsHighResolution = \function_exists('hrtime');
}

public function get($key, $default = null)
public function get(string $key, $default = null): PromiseInterface
{
// delete key if it is already expired => below will detect this as a cache miss
if (isset($this->expires[$key]) && $this->now() - $this->expires[$key] > 0) {
Expand All @@ -76,7 +77,7 @@ public function get($key, $default = null)
return resolve($value);
}

public function set($key, $value, $ttl = null)
public function set(string $key, $value, ?float $ttl = null): PromiseInterface
{
// unset before setting to ensure this entry will be added to end of array (LRU info)
unset($this->data[$key]);
Expand Down Expand Up @@ -108,14 +109,14 @@ public function set($key, $value, $ttl = null)
return resolve(true);
}

public function delete($key)
public function delete(string $key): PromiseInterface
{
unset($this->data[$key], $this->expires[$key]);

return resolve(true);
}

public function getMultiple(array $keys, $default = null)
public function getMultiple(array $keys, $default = null): PromiseInterface
{
$values = [];

Expand All @@ -126,7 +127,7 @@ public function getMultiple(array $keys, $default = null)
return all($values);
}

public function setMultiple(array $values, $ttl = null)
public function setMultiple(array $values, ?float $ttl = null): PromiseInterface
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
Expand All @@ -135,7 +136,7 @@ public function setMultiple(array $values, $ttl = null)
return resolve(true);
}

public function deleteMultiple(array $keys)
public function deleteMultiple(array $keys): PromiseInterface
{
foreach ($keys as $key) {
unset($this->data[$key], $this->expires[$key]);
Expand All @@ -144,15 +145,15 @@ public function deleteMultiple(array $keys)
return resolve(true);
}

public function clear()
public function clear(): PromiseInterface
{
$this->data = [];
$this->expires = [];

return resolve(true);
}

public function has($key)
public function has(string $key): PromiseInterface
{
// delete key if it is already expired
if (isset($this->expires[$key]) && $this->now() - $this->expires[$key] > 0) {
Expand All @@ -171,10 +172,7 @@ public function has($key)
return resolve(true);
}

/**
* @return float
*/
private function now()
private function now(): float
{
return $this->supportsHighResolution ? \hrtime(true) * 1e-9 : \microtime(true);
}
Expand Down
16 changes: 8 additions & 8 deletions src/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface CacheInterface
* @param mixed $default Default value to return for cache miss or null if not given.
* @return PromiseInterface<mixed>
*/
public function get($key, $default = null);
public function get(string $key, $default = null): PromiseInterface;

/**
* Stores an item in the cache.
Expand Down Expand Up @@ -74,7 +74,7 @@ public function get($key, $default = null);
* @param ?float $ttl
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function set($key, $value, $ttl = null);
public function set(string $key, $value, ?float $ttl = null): PromiseInterface;

/**
* Deletes an item from the cache.
Expand All @@ -95,7 +95,7 @@ public function set($key, $value, $ttl = null);
* @param string $key
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function delete($key);
public function delete(string $key): PromiseInterface;

/**
* Retrieves multiple cache items by their unique keys.
Expand All @@ -122,7 +122,7 @@ public function delete($key);
* @param mixed $default Default value to return for keys that do not exist.
* @return PromiseInterface<array> Returns a promise which resolves to an `array` of cached values
*/
public function getMultiple(array $keys, $default = null);
public function getMultiple(array $keys, $default = null): PromiseInterface;

/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
Expand All @@ -148,22 +148,22 @@ public function getMultiple(array $keys, $default = null);
* @param ?float $ttl Optional. The TTL value of this item.
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function setMultiple(array $values, $ttl = null);
public function setMultiple(array $values, ?float $ttl = null): PromiseInterface;

/**
* Deletes multiple cache items in a single operation.
*
* @param string[] $keys A list of string-based keys to be deleted.
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function deleteMultiple(array $keys);
public function deleteMultiple(array $keys): PromiseInterface;

/**
* Wipes clean the entire cache.
*
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function clear();
public function clear(): PromiseInterface;

/**
* Determines whether an item is present in the cache.
Expand All @@ -190,5 +190,5 @@ public function clear();
* @param string $key The cache item key.
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function has($key);
public function has(string $key): PromiseInterface;
}