From 6c1d051ecd8bb4d76761da2c25d274e703a2fb97 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 2 Mar 2022 19:24:26 +0100 Subject: add template annotations to CappedMemoryCache Signed-off-by: Robin Appelman --- lib/private/Cache/CappedMemoryCache.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'lib/private/Cache') diff --git a/lib/private/Cache/CappedMemoryCache.php b/lib/private/Cache/CappedMemoryCache.php index 584a53f0ff2..dd7cb32e90f 100644 --- a/lib/private/Cache/CappedMemoryCache.php +++ b/lib/private/Cache/CappedMemoryCache.php @@ -27,30 +27,42 @@ use OCP\ICache; * In-memory cache with a capacity limit to keep memory usage in check * * Uses a simple FIFO expiry mechanism + * @template T */ class CappedMemoryCache implements ICache, \ArrayAccess { private $capacity; + /** @var T[] */ private $cache = []; public function __construct($capacity = 512) { $this->capacity = $capacity; } - public function hasKey($key) { + public function hasKey($key): bool { return isset($this->cache[$key]); } + /** + * @return ?T + */ public function get($key) { - return isset($this->cache[$key]) ? $this->cache[$key] : null; + return $this->cache[$key] ?? null; } - public function set($key, $value, $ttl = 0) { + /** + * @param string $key + * @param T$value + * @param int $ttl + * @return bool + */ + public function set($key, $value, $ttl = 0): bool { if (is_null($key)) { $this->cache[] = $value; } else { $this->cache[$key] = $value; } $this->garbageCollect(); + return true; } public function remove($key) { @@ -68,13 +80,18 @@ class CappedMemoryCache implements ICache, \ArrayAccess { } /** - * @return mixed + * @return T */ #[\ReturnTypeWillChange] public function &offsetGet($offset) { return $this->cache[$offset]; } + /** + * @param string $key + * @param T$value + * @return void + */ public function offsetSet($offset, $value): void { $this->set($offset, $value); } @@ -83,6 +100,9 @@ class CappedMemoryCache implements ICache, \ArrayAccess { $this->remove($offset); } + /** + * @return T[] + */ public function getData() { return $this->cache; } -- cgit v1.2.3