Skip to content

Commit 364e769

Browse files
Merge pull request #36639 from nextcloud/userbackend-local-cache
also cache backend for user in memory instead of always going to redis
2 parents a2422c9 + 853ec60 commit 364e769

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@
13531353
'OC\\Memcache\\NullCache' => $baseDir . '/lib/private/Memcache/NullCache.php',
13541354
'OC\\Memcache\\ProfilerWrapperCache' => $baseDir . '/lib/private/Memcache/ProfilerWrapperCache.php',
13551355
'OC\\Memcache\\Redis' => $baseDir . '/lib/private/Memcache/Redis.php',
1356+
'OC\\Memcache\\WithLocalCache' => $baseDir . '/lib/private/Memcache/WithLocalCache.php',
13561357
'OC\\MemoryInfo' => $baseDir . '/lib/private/MemoryInfo.php',
13571358
'OC\\Metadata\\Capabilities' => $baseDir . '/lib/private/Metadata/Capabilities.php',
13581359
'OC\\Metadata\\FileEventListener' => $baseDir . '/lib/private/Metadata/FileEventListener.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13861386
'OC\\Memcache\\NullCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/NullCache.php',
13871387
'OC\\Memcache\\ProfilerWrapperCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/ProfilerWrapperCache.php',
13881388
'OC\\Memcache\\Redis' => __DIR__ . '/../../..' . '/lib/private/Memcache/Redis.php',
1389+
'OC\\Memcache\\WithLocalCache' => __DIR__ . '/../../..' . '/lib/private/Memcache/WithLocalCache.php',
13891390
'OC\\MemoryInfo' => __DIR__ . '/../../..' . '/lib/private/MemoryInfo.php',
13901391
'OC\\Metadata\\Capabilities' => __DIR__ . '/../../..' . '/lib/private/Metadata/Capabilities.php',
13911392
'OC\\Metadata\\FileEventListener' => __DIR__ . '/../../..' . '/lib/private/Metadata/FileEventListener.php',
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace OC\Memcache;
4+
5+
use OCP\Cache\CappedMemoryCache;
6+
use OCP\ICache;
7+
8+
/**
9+
* Wrap a cache instance with an extra later of local, in-memory caching
10+
*/
11+
class WithLocalCache implements ICache {
12+
private ICache $inner;
13+
private CappedMemoryCache $cached;
14+
15+
public function __construct(ICache $inner, int $localCapacity = 512) {
16+
$this->inner = $inner;
17+
$this->cached = new CappedMemoryCache($localCapacity);
18+
}
19+
20+
public function get($key) {
21+
if (isset($this->cached[$key])) {
22+
return $this->cached[$key];
23+
} else {
24+
$value = $this->inner->get($key);
25+
if (!is_null($value)) {
26+
$this->cached[$key] = $value;
27+
}
28+
return $value;
29+
}
30+
}
31+
32+
public function set($key, $value, $ttl = 0) {
33+
$this->cached[$key] = $value;
34+
return $this->inner->set($key, $value, $ttl);
35+
}
36+
37+
public function hasKey($key) {
38+
return isset($this->cached[$key]) || $this->inner->hasKey($key);
39+
}
40+
41+
public function remove($key) {
42+
unset($this->cached[$key]);
43+
return $this->inner->remove($key);
44+
}
45+
46+
public function clear($prefix = '') {
47+
$this->cached->clear();
48+
return $this->inner->clear($prefix);
49+
}
50+
51+
public static function isAvailable(): bool {
52+
return false;
53+
}
54+
}

lib/private/User/Manager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
namespace OC\User;
3535

3636
use OC\Hooks\PublicEmitter;
37+
use OC\Memcache\WithLocalCache;
3738
use OCP\DB\QueryBuilder\IQueryBuilder;
3839
use OCP\EventDispatcher\IEventDispatcher;
3940
use OCP\HintException;
@@ -104,7 +105,7 @@ public function __construct(IConfig $config,
104105
IEventDispatcher $eventDispatcher) {
105106
$this->config = $config;
106107
$this->dispatcher = $oldDispatcher;
107-
$this->cache = $cacheFactory->createDistributed('user_backend_map');
108+
$this->cache = new WithLocalCache($cacheFactory->createDistributed('user_backend_map'));
108109
$cachedUsers = &$this->cachedUsers;
109110
$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
110111
/** @var \OC\User\User $user */

0 commit comments

Comments
 (0)