Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

also cache backend for user in memory instead of always going to redis #36639

Merged
merged 1 commit into from
Feb 15, 2023
Merged
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
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
54 changes: 54 additions & 0 deletions lib/private/Memcache/WithLocalCache.php
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yo dawg

*/
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 = '') {
Fixed Show fixed Hide fixed
$this->cached->clear();
return $this->inner->clear($prefix);
}

public static function isAvailable(): bool {
return false;
}
}
3 changes: 2 additions & 1 deletion lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +105,7 @@ public function __construct(IConfig $config,
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 */
Expand Down