1
0

DisplayNameCache.php 2.7 KB

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