Skip to content

Commit

Permalink
also cache backend for user in memory instead of always going to redis
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Feb 9, 2023
1 parent 7341d33 commit b04d442
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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
*/
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;
}
}
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

0 comments on commit b04d442

Please sign in to comment.