Skip to content
Closed
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
11 changes: 8 additions & 3 deletions lib/private/Memcache/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
namespace OC\Memcache;

use OCP\Cache\CappedMemoryCache;
use OCP\Profiler\IProfiler;
use OCP\ICache;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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
Expand Down
31 changes: 24 additions & 7 deletions lib/public/ICacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}