diff options
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Memcache/WithLocalCache.php | 54 | ||||
-rw-r--r-- | lib/private/User/Manager.php | 3 |
4 files changed, 58 insertions, 1 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 6e0934c8fa1..69d1f92c98c 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1353,6 +1353,7 @@ return array( 'OC\\Memcache\\NullCache' => $baseDir . '/lib/private/Memcache/NullCache.php', 'OC\\Memcache\\ProfilerWrapperCache' => $baseDir . '/lib/private/Memcache/ProfilerWrapperCache.php', 'OC\\Memcache\\Redis' => $baseDir . '/lib/private/Memcache/Redis.php', + 'OC\\Memcache\\WithLocalCache' => $baseDir . '/lib/private/Memcache/WithLocalCache.php', 'OC\\MemoryInfo' => $baseDir . '/lib/private/MemoryInfo.php', 'OC\\Metadata\\Capabilities' => $baseDir . '/lib/private/Metadata/Capabilities.php', 'OC\\Metadata\\FileEventListener' => $baseDir . '/lib/private/Metadata/FileEventListener.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 50aaa84ae77..1191a3bfa53 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1386,6 +1386,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Memcache\\NullCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/NullCache.php', 'OC\\Memcache\\ProfilerWrapperCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/ProfilerWrapperCache.php', 'OC\\Memcache\\Redis' => __DIR__ . '/../../..' . '/lib/private/Memcache/Redis.php', + 'OC\\Memcache\\WithLocalCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/WithLocalCache.php', 'OC\\MemoryInfo' => __DIR__ . '/../../..' . '/lib/private/MemoryInfo.php', 'OC\\Metadata\\Capabilities' => __DIR__ . '/../../..' . '/lib/private/Metadata/Capabilities.php', 'OC\\Metadata\\FileEventListener' => __DIR__ . '/../../..' . '/lib/private/Metadata/FileEventListener.php', diff --git a/lib/private/Memcache/WithLocalCache.php b/lib/private/Memcache/WithLocalCache.php new file mode 100644 index 00000000000..5b7ccc10e39 --- /dev/null +++ b/lib/private/Memcache/WithLocalCache.php @@ -0,0 +1,54 @@ +<?php + +namespace OC\Memcache; + +use OCP\Cache\CappedMemoryCache; +use OCP\ICache; + +/** + * Wrap a cache instance with an extra later of local, in-memory caching + */ +class WithLocalCache implements ICache { + private ICache $inner; + private CappedMemoryCache $cached; + + public function __construct(ICache $inner, int $localCapacity = 512) { + $this->inner = $inner; + $this->cached = new CappedMemoryCache($localCapacity); + } + + public function get($key) { + if (isset($this->cached[$key])) { + return $this->cached[$key]; + } else { + $value = $this->inner->get($key); + if (!is_null($value)) { + $this->cached[$key] = $value; + } + return $value; + } + } + + public function set($key, $value, $ttl = 0) { + $this->cached[$key] = $value; + return $this->inner->set($key, $value, $ttl); + } + + public function hasKey($key) { + return isset($this->cached[$key]) || $this->inner->hasKey($key); + } + + public function remove($key) { + unset($this->cached[$key]); + return $this->inner->remove($key); + } + + public function clear($prefix = '') { + $this->cached->clear(); + return $this->inner->clear($prefix); + } + + public static function isAvailable(): bool { + return false; + } +} diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index 937d825ed77..859ebd2a604 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -34,6 +34,7 @@ namespace OC\User; use OC\Hooks\PublicEmitter; +use OC\Memcache\WithLocalCache; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\IEventDispatcher; use OCP\HintException; @@ -104,7 +105,7 @@ class Manager extends PublicEmitter implements IUserManager { IEventDispatcher $eventDispatcher) { $this->config = $config; $this->dispatcher = $oldDispatcher; - $this->cache = $cacheFactory->createDistributed('user_backend_map'); + $this->cache = new WithLocalCache($cacheFactory->createDistributed('user_backend_map')); $cachedUsers = &$this->cachedUsers; $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { /** @var \OC\User\User $user */ |