aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Wurst <ChristophWurst@users.noreply.github.com>2023-10-13 13:52:14 +0200
committerGitHub <noreply@github.com>2023-10-13 13:52:14 +0200
commitcdc2f38a511a2665951f6eb8056d6077f70151ed (patch)
tree6d0e11d4cbcd73e34d101ba886037b0281d92542
parent160a2b71209fa4ec9f9513ba441335757a31b198 (diff)
parentff672b11e2e9628fd721b097e6712f99cb9883f8 (diff)
downloadnextcloud-server-cdc2f38a511a2665951f6eb8056d6077f70151ed.tar.gz
nextcloud-server-cdc2f38a511a2665951f6eb8056d6077f70151ed.zip
Merge pull request #40868 from nextcloud/feat/in-memory-cache-factory
feat: Add factory method for in-memory caches
-rw-r--r--lib/private/Memcache/Factory.php5
-rw-r--r--lib/public/ICacheFactory.php16
-rw-r--r--tests/lib/Memcache/FactoryTest.php11
3 files changed, 32 insertions, 0 deletions
diff --git a/lib/private/Memcache/Factory.php b/lib/private/Memcache/Factory.php
index fa2dfeb75dd..16d6ae32f72 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;
@@ -184,6 +185,10 @@ class Factory implements ICacheFactory {
return $this->distributedCacheClass !== self::NULL_CACHE;
}
+ public function createInMemory(int $capacity = 512): ICache {
+ return new CappedMemoryCache($capacity);
+ }
+
/**
* Check if a local memory cache backend is available
*
diff --git a/lib/public/ICacheFactory.php b/lib/public/ICacheFactory.php
index d70a836aa52..70ad955849d 100644
--- a/lib/public/ICacheFactory.php
+++ b/lib/public/ICacheFactory.php
@@ -75,4 +75,20 @@ interface ICacheFactory {
* @since 13.0.0
*/
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.
+ *
+ * Cache keys are local to the cache object. When building two in-memory
+ * caches, there is no data exchange between the instances.
+ *
+ * @param int $capacity maximum number of cache keys
+ * @return ICache
+ * @since 28.0.0
+ */
+ public function createInMemory(int $capacity = 512): ICache;
}
diff --git a/tests/lib/Memcache/FactoryTest.php b/tests/lib/Memcache/FactoryTest.php
index 9cdd7058ffa..5f13a94eacd 100644
--- a/tests/lib/Memcache/FactoryTest.php
+++ b/tests/lib/Memcache/FactoryTest.php
@@ -140,4 +140,15 @@ class FactoryTest extends \Test\TestCase {
$profiler = $this->getMockBuilder(IProfiler::class)->getMock();
new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache);
}
+
+ public function testCreateInMemory(): void {
+ $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
+ $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
+ $factory = new \OC\Memcache\Factory('abc', $logger, $profiler, null, null, null);
+
+ $cache = $factory->createInMemory();
+ $cache->set('test', 48);
+
+ self::assertSame(48, $cache->get('test'));
+ }
}