DisplayNameCache.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Group\Events\GroupDeletedEvent;
  28. use OCP\ICache;
  29. use OCP\ICacheFactory;
  30. use OCP\IGroupManager;
  31. /**
  32. * Class that cache the relation Group ID -> Display name
  33. *
  34. * This saves fetching the group from the backend for "just" the display name
  35. * @template-implements IEventListener<GroupChangedEvent|GroupDeletedEvent>
  36. */
  37. class DisplayNameCache implements IEventListener {
  38. private CappedMemoryCache $cache;
  39. private ICache $memCache;
  40. private IGroupManager $groupManager;
  41. public function __construct(ICacheFactory $cacheFactory, IGroupManager $groupManager) {
  42. $this->cache = new CappedMemoryCache();
  43. $this->memCache = $cacheFactory->createDistributed('groupDisplayNameMappingCache');
  44. $this->groupManager = $groupManager;
  45. }
  46. public function getDisplayName(string $groupId): ?string {
  47. if (isset($this->cache[$groupId])) {
  48. return $this->cache[$groupId];
  49. }
  50. $displayName = $this->memCache->get($groupId);
  51. if ($displayName) {
  52. $this->cache[$groupId] = $displayName;
  53. return $displayName;
  54. }
  55. $group = $this->groupManager->get($groupId);
  56. if ($group) {
  57. $displayName = $group->getDisplayName();
  58. } else {
  59. $displayName = null;
  60. }
  61. $this->cache[$groupId] = $displayName;
  62. $this->memCache->set($groupId, $displayName, 60 * 10); // 10 minutes
  63. return $displayName;
  64. }
  65. public function clear(): void {
  66. $this->cache = new CappedMemoryCache();
  67. $this->memCache->clear();
  68. }
  69. public function handle(Event $event): void {
  70. if ($event instanceof GroupChangedEvent && $event->getFeature() === 'displayName') {
  71. $groupId = $event->getGroup()->getGID();
  72. $newDisplayName = $event->getValue();
  73. $this->cache[$groupId] = $newDisplayName;
  74. $this->memCache->set($groupId, $newDisplayName, 60 * 10); // 10 minutes
  75. }
  76. if ($event instanceof GroupDeletedEvent) {
  77. $groupId = $event->getGroup()->getGID();
  78. unset($this->cache[$groupId]);
  79. $this->memCache->remove($groupId);
  80. }
  81. }
  82. }