DisplayNameCache.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. */
  35. class DisplayNameCache implements IEventListener {
  36. private array $cache = [];
  37. private ICache $memCache;
  38. private IUserManager $userManager;
  39. public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) {
  40. $this->memCache = $cacheFactory->createDistributed('displayNameMappingCache');
  41. $this->userManager = $userManager;
  42. }
  43. public function getDisplayName(string $userId): ?string {
  44. if (isset($this->cache[$userId])) {
  45. return $this->cache[$userId];
  46. }
  47. $displayName = $this->memCache->get($userId);
  48. if ($displayName) {
  49. $this->cache[$userId] = $displayName;
  50. return $displayName;
  51. }
  52. $user = $this->userManager->get($userId);
  53. if ($user) {
  54. $displayName = $user->getDisplayName();
  55. } else {
  56. $displayName = null;
  57. }
  58. $this->cache[$userId] = $displayName;
  59. $this->memCache->set($userId, $displayName, 60 * 10); // 10 minutes
  60. return $displayName;
  61. }
  62. public function clear(): void {
  63. $this->cache = [];
  64. $this->memCache->clear();
  65. }
  66. public function handle(Event $event): void {
  67. if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
  68. $userId = $event->getUser()->getUID();
  69. $newDisplayName = $event->getValue();
  70. $this->cache[$userId] = $newDisplayName;
  71. $this->memCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
  72. }
  73. if ($event instanceof UserDeletedEvent) {
  74. $userId = $event->getUser()->getUID();
  75. unset($this->cache[$userId]);
  76. $this->memCache->remove($userId);
  77. }
  78. }
  79. }