DisplayNameCache.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. * @license AGPL-3.0-or-later
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. namespace OC\User;
  21. use OCP\EventDispatcher\Event;
  22. use OCP\EventDispatcher\IEventListener;
  23. use OCP\ICache;
  24. use OCP\ICacheFactory;
  25. use OCP\IUserManager;
  26. use OCP\User\Events\UserChangedEvent;
  27. use OCP\User\Events\UserDeletedEvent;
  28. /**
  29. * Class that cache the relation UserId -> Display name
  30. *
  31. * This saves fetching the user from a user backend and later on fetching
  32. * their preferences. It's generally not an issue if this data is slightly
  33. * outdated.
  34. * @template-implements IEventListener<UserChangedEvent|UserDeletedEvent>
  35. */
  36. class DisplayNameCache implements IEventListener {
  37. private array $cache = [];
  38. private ICache $memCache;
  39. private IUserManager $userManager;
  40. public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) {
  41. $this->memCache = $cacheFactory->createDistributed('displayNameMappingCache');
  42. $this->userManager = $userManager;
  43. }
  44. public function getDisplayName(string $userId): ?string {
  45. if (isset($this->cache[$userId])) {
  46. return $this->cache[$userId];
  47. }
  48. $displayName = $this->memCache->get($userId);
  49. if ($displayName) {
  50. $this->cache[$userId] = $displayName;
  51. return $displayName;
  52. }
  53. $user = $this->userManager->get($userId);
  54. if ($user) {
  55. $displayName = $user->getDisplayName();
  56. } else {
  57. $displayName = null;
  58. }
  59. $this->cache[$userId] = $displayName;
  60. $this->memCache->set($userId, $displayName, 60 * 10); // 10 minutes
  61. return $displayName;
  62. }
  63. public function clear(): void {
  64. $this->cache = [];
  65. $this->memCache->clear();
  66. }
  67. public function handle(Event $event): void {
  68. if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
  69. $userId = $event->getUser()->getUID();
  70. $newDisplayName = $event->getValue();
  71. $this->cache[$userId] = $newDisplayName;
  72. $this->memCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
  73. }
  74. if ($event instanceof UserDeletedEvent) {
  75. $userId = $event->getUser()->getUID();
  76. unset($this->cache[$userId]);
  77. $this->memCache->remove($userId);
  78. }
  79. }
  80. }