]> source.dussan.org Git - nextcloud-server.git/commitdiff
cache display names in local memory before external memcache 32065/head
authorRobin Appelman <robin@icewind.nl>
Fri, 22 Apr 2022 10:50:42 +0000 (12:50 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Fri, 22 Apr 2022 13:22:16 +0000 (15:22 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/User/DisplayNameCache.php

index ed0c723ef37d2aaf46dd85b2f7ced663a44f90f4..22a79863e4978b45fa6b14ee4a9dd56dd15e027b 100644 (file)
@@ -38,17 +38,22 @@ use OCP\User\Events\UserChangedEvent;
  * 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;
                }
 
@@ -58,20 +63,23 @@ class DisplayNameCache implements IEventListener {
                } 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
                }
        }
 }