Skip to content

Commit

Permalink
cache display names in local memory before external memcache
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and blizzz committed Apr 22, 2022
1 parent 40ac4e8 commit 6ca689a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/private/User/DisplayNameCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@
* outdated.
*/
class DisplayNameCache implements IEventListener {
private ICache $internalCache;
private array $cache = [];
private ICache $memCache;
private IUserManager $userManager;

public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) {
$this->internalCache = $cacheFactory->createDistributed('displayNameMappingCache');
$this->memCache = $cacheFactory->createDistributed('displayNameMappingCache');
$this->userManager = $userManager;
}

public function getDisplayName(string $userId) {
$displayName = $this->internalCache->get($userId);
if (isset($this->cache[$userId])) {
return $this->cache[$userId];
}
$displayName = $this->memCache->get($userId);
if ($displayName) {
$this->cache[$userId] = $displayName;
return $displayName;
}

Expand All @@ -58,20 +63,23 @@ public function getDisplayName(string $userId) {
} else {
$displayName = $userId;
}
$this->internalCache->set($userId, $displayName, 60 * 10); // 10 minutes
$this->cache[$userId] = $displayName;
$this->memCache->set($userId, $displayName, 60 * 10); // 10 minutes

return $displayName;
}

public function clear(): void {
$this->internalCache->clear();
$this->cache = [];
$this->memCache->clear();
}

public function handle(Event $event): void {
if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
$userId = $event->getUser()->getUID();
$newDisplayName = $event->getValue();
$this->internalCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
$this->cache[$userId] = $newDisplayName;
$this->memCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
}
}
}

0 comments on commit 6ca689a

Please sign in to comment.