diff --git a/lib/private/Memcache/Factory.php b/lib/private/Memcache/Factory.php index 788a7c2e8c91a..8f11a11c6f862 100644 --- a/lib/private/Memcache/Factory.php +++ b/lib/private/Memcache/Factory.php @@ -31,6 +31,7 @@ */ namespace OC\Memcache; +use OCP\Cache\CappedMemoryCache; use OCP\Profiler\IProfiler; use OCP\ICache; use OCP\ICacheFactory; @@ -115,7 +116,7 @@ public function __construct(string $globalPrefix, LoggerInterface $logger, IProf * @param string $prefix * @return IMemcache */ - public function createLocking(string $prefix = ''): IMemcache { + public function createLocking(string $prefix = ''): ?IMemcache { assert($this->lockingCacheClass !== null); $cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) { @@ -137,7 +138,7 @@ public function createLocking(string $prefix = ''): IMemcache { * @param string $prefix * @return ICache */ - public function createDistributed(string $prefix = ''): ICache { + public function createDistributed(string $prefix = ''): ?ICache { assert($this->distributedCacheClass !== null); $cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) { @@ -159,7 +160,7 @@ public function createDistributed(string $prefix = ''): ICache { * @param string $prefix * @return ICache */ - public function createLocal(string $prefix = ''): ICache { + public function createLocal(string $prefix = ''): ?ICache { assert($this->localCacheClass !== null); $cache = new $this->localCacheClass($this->globalPrefix . '/' . $prefix); if ($this->localCacheClass === Redis::class && $this->profiler->isEnabled()) { @@ -184,6 +185,10 @@ public function isAvailable(): bool { return $this->distributedCacheClass !== self::NULL_CACHE; } + public function createInMemory(int $capacity = 512): ICache { + return new CappedMemoryCache($capacity); + } + /** * @see \OC\Memcache\Factory::createLocal() * @param string $prefix diff --git a/lib/public/ICacheFactory.php b/lib/public/ICacheFactory.php index d70a836aa5255..a92389711132b 100644 --- a/lib/public/ICacheFactory.php +++ b/lib/public/ICacheFactory.php @@ -53,26 +53,43 @@ public function isLocalCacheAvailable(): bool; * create a cache instance for storing locks * * @param string $prefix - * @return IMemcache + * @return IMemcache|null * @since 13.0.0 + * @since 28.0.0 return type is nullable but the method will continue to return an object for backwards compatibility. Future versions will only return an object if distributed cache is available. */ - public function createLocking(string $prefix = ''): IMemcache; + public function createLocking(string $prefix = ''): ?IMemcache; /** * create a distributed cache instance * * @param string $prefix - * @return ICache + * @return ICache|null a cache implementation * @since 13.0.0 + * @since 28.0.0 return type is nullable but the method will continue to return an object for backwards compatibility. Future versions will only return an object if distributed cache is available. */ - public function createDistributed(string $prefix = ''): ICache; + public function createDistributed(string $prefix = ''): ?ICache; /** - * create a local cache instance + * Create a local cache instance * * @param string $prefix - * @return ICache + * + * @return ICache|null a cache implementation * @since 13.0.0 + * @since 28.0.0 return type is nullable but the method will continue to return an object for backwards compatibility. Future versions will only return an object if local cache is available. + */ + public function createLocal(string $prefix = ''): ?ICache; + + /** + * Create an in-memory cache instance + * + * Useful for remembering values inside one process. Cache memory is cleared + * when the object is garbage-collected. Implementation may also expire keys + * earlier when the TTL is reached or too much memory is consumed. + * + * @param int $capacity + * @return ICache + * @since 28.0.0 */ - public function createLocal(string $prefix = ''): ICache; + public function createInMemory(int $capacity = 512): ICache; }