DisplayNameCache.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Anna Larch <anna.larch@gmx.net>
  5. * @author Anna Larch <anna.larch@gmx.net>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Group;
  23. use OCP\Cache\CappedMemoryCache;
  24. use OCP\EventDispatcher\Event;
  25. use OCP\EventDispatcher\IEventListener;
  26. use OCP\Group\Events\GroupChangedEvent;
  27. use OCP\ICache;
  28. use OCP\ICacheFactory;
  29. use OCP\IGroupManager;
  30. /**
  31. * Class that cache the relation Group ID -> Display name
  32. *
  33. * This saves fetching the group from the backend for "just" the display name
  34. */
  35. class DisplayNameCache implements IEventListener {
  36. private CappedMemoryCache $cache;
  37. private ICache $memCache;
  38. private IGroupManager $groupManager;
  39. public function __construct(ICacheFactory $cacheFactory, IGroupManager $groupManager) {
  40. $this->cache = new CappedMemoryCache();
  41. $this->memCache = $cacheFactory->createDistributed('groupDisplayNameMappingCache');
  42. $this->groupManager = $groupManager;
  43. }
  44. public function getDisplayName(string $groupId): ?string {
  45. if (isset($this->cache[$groupId])) {
  46. return $this->cache[$groupId];
  47. }
  48. $displayName = $this->memCache->get($groupId);
  49. if ($displayName) {
  50. $this->cache[$groupId] = $displayName;
  51. return $displayName;
  52. }
  53. $group = $this->groupManager->get($groupId);
  54. if ($group) {
  55. $displayName = $group->getDisplayName();
  56. } else {
  57. $displayName = null;
  58. }
  59. $this->cache[$groupId] = $displayName;
  60. $this->memCache->set($groupId, $displayName, 60 * 10); // 10 minutes
  61. return $displayName;
  62. }
  63. public function clear(): void {
  64. $this->cache = new CappedMemoryCache();
  65. $this->memCache->clear();
  66. }
  67. public function handle(Event $event): void {
  68. if ($event instanceof GroupChangedEvent && $event->getFeature() === 'displayName') {
  69. $groupId = $event->getGroup()->getGID();
  70. $newDisplayName = $event->getValue();
  71. $this->cache[$groupId] = $newDisplayName;
  72. $this->memCache->set($groupId, $newDisplayName, 60 * 10); // 10 minutes
  73. }
  74. }
  75. }