nextcloud/lib/public/Cache/CappedMemoryCache.php
Côme Chilliet 23e9a81477
fix(cache): Improve typing of ICache and CappedMemoryCache
For CappedMemoryCache we allow string|int to be consistent with PHP
 array keys, for ICache we strictly apply the previously stated string
 type for keys.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
2026-06-11 09:45:28 +02:00

152 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCP\Cache;
use OCP\ICache;
/**
* In-memory cache with a capacity limit to keep memory usage in check
*
* Uses a simple FIFO expiry mechanism
*
* @since 25.0.0
* @template T
* @template-implements \ArrayAccess<array-key,T>
*/
class CappedMemoryCache implements ICache, \ArrayAccess {
private int $capacity;
/** @var T[] */
private array $cache = [];
/**
* @inheritdoc
* @since 25.0.0
*/
public function __construct(int $capacity = 512) {
$this->capacity = $capacity;
}
/**
* @inheritdoc
* @since 25.0.0
*/
#[\Override]
public function hasKey(string|int $key): bool {
return isset($this->cache[$key]);
}
/**
* @return ?T
* @since 25.0.0
*/
#[\Override]
public function get(string|int $key) {
return $this->cache[$key] ?? null;
}
/**
* @inheritdoc
* @param T $value
* @since 25.0.0
*/
#[\Override]
public function set(string|int $key, $value, int $ttl = 0): bool {
$this->cache[$key] = $value;
$this->garbageCollect();
return true;
}
/**
* @since 25.0.0
*/
#[\Override]
public function remove(string|int $key): bool {
unset($this->cache[$key]);
return true;
}
/**
* @inheritdoc
* @since 25.0.0
*/
#[\Override]
public function clear(string $prefix = ''): bool {
$this->cache = [];
return true;
}
/**
* @since 25.0.0
*/
#[\Override]
public function offsetExists($offset): bool {
return $this->hasKey($offset);
}
/**
* @inheritdoc
* @return T
* @since 25.0.0
*/
#[\Override]
#[\ReturnTypeWillChange]
public function &offsetGet($offset) {
return $this->cache[$offset];
}
/**
* @inheritdoc
* @param string $offset
* @param T $value
* @since 25.0.0
*/
#[\Override]
public function offsetSet($offset, $value): void {
$this->set($offset, $value);
}
/**
* @inheritdoc
* @since 25.0.0
*/
#[\Override]
public function offsetUnset($offset): void {
$this->remove($offset);
}
/**
* @return T[]
* @since 25.0.0
*/
public function getData(): array {
return $this->cache;
}
/**
* @since 25.0.0
*/
private function garbageCollect(): void {
while (count($this->cache) > $this->capacity) {
reset($this->cache);
$key = key($this->cache);
$this->remove($key);
}
}
/**
* @inheritdoc
* @since 25.0.0
*/
#[\Override]
public static function isAvailable(): bool {
return true;
}
}