DisplayNameCache.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /**
  28. * Class that cache the relation UserId -> Display name
  29. *
  30. * This saves fetching the user from a user backend and later on fetching
  31. * their preferences. It's generally not an issue if this data is slightly
  32. * outdated.
  33. */
  34. class DisplayNameCache implements IEventListener {
  35. private array $cache = [];
  36. private ICache $memCache;
  37. private IUserManager $userManager;
  38. public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) {
  39. $this->memCache = $cacheFactory->createDistributed('displayNameMappingCache');
  40. $this->userManager = $userManager;
  41. }
  42. public function getDisplayName(string $userId): ?string {
  43. if (isset($this->cache[$userId])) {
  44. return $this->cache[$userId];
  45. }
  46. $displayName = $this->memCache->get($userId);
  47. if ($displayName) {
  48. $this->cache[$userId] = $displayName;
  49. return $displayName;
  50. }
  51. $user = $this->userManager->get($userId);
  52. if ($user) {
  53. $displayName = $user->getDisplayName();
  54. } else {
  55. $displayName = null;
  56. }
  57. $this->cache[$userId] = $displayName;
  58. $this->memCache->set($userId, $displayName, 60 * 10); // 10 minutes
  59. return $displayName;
  60. }
  61. public function clear(): void {
  62. $this->cache = [];
  63. $this->memCache->clear();
  64. }
  65. public function handle(Event $event): void {
  66. if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
  67. $userId = $event->getUser()->getUID();
  68. $newDisplayName = $event->getValue();
  69. $this->cache[$userId] = $newDisplayName;
  70. $this->memCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
  71. }
  72. }
  73. }