DisplayNameCache.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\User;
  8. use OCP\EventDispatcher\Event;
  9. use OCP\EventDispatcher\IEventListener;
  10. use OCP\ICache;
  11. use OCP\ICacheFactory;
  12. use OCP\IUserManager;
  13. use OCP\User\Events\UserChangedEvent;
  14. use OCP\User\Events\UserDeletedEvent;
  15. /**
  16. * Class that cache the relation UserId -> Display name
  17. *
  18. * This saves fetching the user from a user backend and later on fetching
  19. * their preferences. It's generally not an issue if this data is slightly
  20. * outdated.
  21. * @template-implements IEventListener<UserChangedEvent|UserDeletedEvent>
  22. */
  23. class DisplayNameCache implements IEventListener {
  24. private array $cache = [];
  25. private ICache $memCache;
  26. private IUserManager $userManager;
  27. public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) {
  28. $this->memCache = $cacheFactory->createDistributed('displayNameMappingCache');
  29. $this->userManager = $userManager;
  30. }
  31. public function getDisplayName(string $userId): ?string {
  32. if (isset($this->cache[$userId])) {
  33. return $this->cache[$userId];
  34. }
  35. $displayName = $this->memCache->get($userId);
  36. if ($displayName) {
  37. $this->cache[$userId] = $displayName;
  38. return $displayName;
  39. }
  40. $user = $this->userManager->get($userId);
  41. if ($user) {
  42. $displayName = $user->getDisplayName();
  43. } else {
  44. $displayName = null;
  45. }
  46. $this->cache[$userId] = $displayName;
  47. $this->memCache->set($userId, $displayName, 60 * 10); // 10 minutes
  48. return $displayName;
  49. }
  50. public function clear(): void {
  51. $this->cache = [];
  52. $this->memCache->clear();
  53. }
  54. public function handle(Event $event): void {
  55. if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
  56. $userId = $event->getUser()->getUID();
  57. $newDisplayName = $event->getValue();
  58. $this->cache[$userId] = $newDisplayName;
  59. $this->memCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
  60. }
  61. if ($event instanceof UserDeletedEvent) {
  62. $userId = $event->getUser()->getUID();
  63. unset($this->cache[$userId]);
  64. $this->memCache->remove($userId);
  65. }
  66. }
  67. }