add template annotations to CappedMemoryCache

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2022-03-02 19:24:26 +01:00
parent 22875bf367
commit 6c1d051ecd
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB
2 changed files with 32 additions and 10 deletions

View file

@ -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;
}

View file

@ -42,7 +42,9 @@ use OCP\IUserManager;
class Manager implements IMountManager {
/** @var MountPoint[] */
private array $mounts = [];
/** @var CappedMemoryCache<IMountPoint> */
private CappedMemoryCache $pathCache;
/** @var CappedMemoryCache<IMountPoint[]> */
private CappedMemoryCache $inPathCache;
private SetupManager $setupManager;
@ -94,9 +96,9 @@ class Manager implements IMountManager {
* Find the mount for $path
*
* @param string $path
* @return MountPoint
* @return IMountPoint
*/
public function find(string $path): MountPoint {
public function find(string $path): IMountPoint {
$this->setupManager->setupForPath($path);
$path = Filesystem::normalizePath($path);
@ -127,7 +129,7 @@ class Manager implements IMountManager {
* Find all mounts in $path
*
* @param string $path
* @return MountPoint[]
* @return IMountPoint[]
*/
public function findIn(string $path): array {
$this->setupManager->setupForPath($path);
@ -160,7 +162,7 @@ class Manager implements IMountManager {
* Find mounts by storage id
*
* @param string $id
* @return MountPoint[]
* @return IMountPoint[]
*/
public function findByStorageId(string $id): array {
\OC_Util::setupFS();
@ -177,7 +179,7 @@ class Manager implements IMountManager {
}
/**
* @return MountPoint[]
* @return IMountPoint[]
*/
public function getAll(): array {
return $this->mounts;
@ -187,7 +189,7 @@ class Manager implements IMountManager {
* Find mounts by numeric storage id
*
* @param int $id
* @return MountPoint[]
* @return IMountPoint[]
*/
public function findByNumericId(int $id): array {
$storageId = \OC\Files\Cache\Storage::getStorageId($id);