Passed
Push — master ( cd7cec...c4157e )
by
unknown
15:30 queued 17s
created

DisplayNameCache::getDisplayName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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