@@ -25,60 +25,60 @@ |
||
25 | 25 | * @template-implements IEventListener<UserChangedEvent|UserDeletedEvent> |
26 | 26 | */ |
27 | 27 | class DisplayNameCache implements IEventListener { |
28 | - private const CACHE_TTL = 24 * 60 * 60; // 1 day |
|
28 | + private const CACHE_TTL = 24 * 60 * 60; // 1 day |
|
29 | 29 | |
30 | - private array $cache = []; |
|
31 | - private ICache $memCache; |
|
32 | - private IUserManager $userManager; |
|
30 | + private array $cache = []; |
|
31 | + private ICache $memCache; |
|
32 | + private IUserManager $userManager; |
|
33 | 33 | |
34 | - public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) { |
|
35 | - $this->memCache = $cacheFactory->createDistributed('displayNameMappingCache'); |
|
36 | - $this->userManager = $userManager; |
|
37 | - } |
|
34 | + public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) { |
|
35 | + $this->memCache = $cacheFactory->createDistributed('displayNameMappingCache'); |
|
36 | + $this->userManager = $userManager; |
|
37 | + } |
|
38 | 38 | |
39 | - public function getDisplayName(string $userId): ?string { |
|
40 | - if (isset($this->cache[$userId])) { |
|
41 | - return $this->cache[$userId]; |
|
42 | - } |
|
39 | + public function getDisplayName(string $userId): ?string { |
|
40 | + if (isset($this->cache[$userId])) { |
|
41 | + return $this->cache[$userId]; |
|
42 | + } |
|
43 | 43 | |
44 | - if (strlen($userId) > IUser::MAX_USERID_LENGTH) { |
|
45 | - return null; |
|
46 | - } |
|
44 | + if (strlen($userId) > IUser::MAX_USERID_LENGTH) { |
|
45 | + return null; |
|
46 | + } |
|
47 | 47 | |
48 | - $displayName = $this->memCache->get($userId); |
|
49 | - if ($displayName) { |
|
50 | - $this->cache[$userId] = $displayName; |
|
51 | - return $displayName; |
|
52 | - } |
|
48 | + $displayName = $this->memCache->get($userId); |
|
49 | + if ($displayName) { |
|
50 | + $this->cache[$userId] = $displayName; |
|
51 | + return $displayName; |
|
52 | + } |
|
53 | 53 | |
54 | - $user = $this->userManager->get($userId); |
|
55 | - if ($user) { |
|
56 | - $displayName = $user->getDisplayName(); |
|
57 | - } else { |
|
58 | - $displayName = null; |
|
59 | - } |
|
60 | - $this->cache[$userId] = $displayName; |
|
61 | - $this->memCache->set($userId, $displayName, self::CACHE_TTL); |
|
54 | + $user = $this->userManager->get($userId); |
|
55 | + if ($user) { |
|
56 | + $displayName = $user->getDisplayName(); |
|
57 | + } else { |
|
58 | + $displayName = null; |
|
59 | + } |
|
60 | + $this->cache[$userId] = $displayName; |
|
61 | + $this->memCache->set($userId, $displayName, self::CACHE_TTL); |
|
62 | 62 | |
63 | - return $displayName; |
|
64 | - } |
|
63 | + return $displayName; |
|
64 | + } |
|
65 | 65 | |
66 | - public function clear(): void { |
|
67 | - $this->cache = []; |
|
68 | - $this->memCache->clear(); |
|
69 | - } |
|
66 | + public function clear(): void { |
|
67 | + $this->cache = []; |
|
68 | + $this->memCache->clear(); |
|
69 | + } |
|
70 | 70 | |
71 | - public function handle(Event $event): void { |
|
72 | - if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') { |
|
73 | - $userId = $event->getUser()->getUID(); |
|
74 | - $newDisplayName = $event->getValue(); |
|
75 | - $this->cache[$userId] = $newDisplayName; |
|
76 | - $this->memCache->set($userId, $newDisplayName, self::CACHE_TTL); |
|
77 | - } |
|
78 | - if ($event instanceof UserDeletedEvent) { |
|
79 | - $userId = $event->getUser()->getUID(); |
|
80 | - unset($this->cache[$userId]); |
|
81 | - $this->memCache->remove($userId); |
|
82 | - } |
|
83 | - } |
|
71 | + public function handle(Event $event): void { |
|
72 | + if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') { |
|
73 | + $userId = $event->getUser()->getUID(); |
|
74 | + $newDisplayName = $event->getValue(); |
|
75 | + $this->cache[$userId] = $newDisplayName; |
|
76 | + $this->memCache->set($userId, $newDisplayName, self::CACHE_TTL); |
|
77 | + } |
|
78 | + if ($event instanceof UserDeletedEvent) { |
|
79 | + $userId = $event->getUser()->getUID(); |
|
80 | + unset($this->cache[$userId]); |
|
81 | + $this->memCache->remove($userId); |
|
82 | + } |
|
83 | + } |
|
84 | 84 | } |