Skip to content

Commit cc582ab

Browse files
committed
perf: Batch getting the display names of groups
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 0d1fc05 commit cc582ab

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

apps/provisioning_api/lib/Controller/AUserDataOCSController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,11 @@ protected function findGroupsWithDisplayname(array $userDetails): array {
275275
$groupIds = array_unique($groupIds);
276276
sort($groupIds);
277277

278-
return array_map(function ($groupId) {
279-
$displayname = $this->groupDisplayNameCache->getDisplayName($groupId) ?? $groupId;
280-
return ['id' => $groupId, 'displayname' => $displayname];
281-
}, $groupIds);
278+
$info = [];
279+
foreach ($this->groupDisplayNameCache->getDisplayNames($groupIds) as $groupId => $displayName) {
280+
$info[] = ['id' => $groupId, 'displayname' => $displayName ?? $groupId];
281+
}
282+
return $info;
282283
}
283284

284285
/**

apps/provisioning_api/tests/Controller/GroupsControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,9 @@ public function testGetGroupUsersDetails(): void {
495495
->willReturn([]);
496496

497497
$this->groupDisplayNameCache
498-
->method('getDisplayName')
499-
->with('ncg1')
500-
->willReturn('Group One');
498+
->method('getDisplayNames')
499+
->with(['ncg1'])
500+
->willReturn(['ncg1' => 'Group One']);
501501

502502
$result = $this->api->getGroupUsersDetails($gid);
503503

@@ -550,9 +550,9 @@ public function testGetGroupUsersDetailsEncoded(): void {
550550
->willReturn([]);
551551

552552
$this->groupDisplayNameCache
553-
->method('getDisplayName')
554-
->with('Department A/B C/D')
555-
->willReturn('Department A/B C/D-name');
553+
->method('getDisplayNames')
554+
->with(['Department A/B C/D'])
555+
->willReturn(['Department A/B C/D' => 'Department A/B C/D-name']);
556556

557557
$result = $this->api->getGroupUsersDetails(urlencode($gid));
558558

lib/private/Group/DisplayNameCache.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,45 @@ public function getDisplayName(string $groupId): ?string {
5757
return $displayName;
5858
}
5959

60+
/**
61+
* @param list<string> $groupIds
62+
* @return array<string, ?string>
63+
*/
64+
public function getDisplayNames(array $groupIds): array {
65+
$result = [];
66+
$missing = [];
67+
foreach ($groupIds as $groupId) {
68+
if (isset($this->cache[$groupId])) {
69+
$result[$groupId] = $this->cache[$groupId];
70+
} else {
71+
$displayName = $this->memCache->get($groupId);
72+
if ($displayName) {
73+
$this->cache[$groupId] = $displayName;
74+
$result[$groupId] = $displayName;
75+
} else {
76+
$missing[] = $groupId;
77+
}
78+
}
79+
}
80+
81+
/** @var Manager $groupManager */
82+
$groupManager = $this->groupManager;
83+
$groups = $groupManager->getGroupsObjects($missing);
84+
$stillMissingGroups = array_diff($missing, array_keys($groups));
85+
foreach ($groups as $groupId => $group) {
86+
$displayName = $group->getDisplayName() ?? $group->getGID();
87+
$this->cache[$groupId] = $displayName;
88+
$this->memCache->set($groupId, $displayName, 60 * 10); // 10 minutes
89+
$result[$groupId] = $displayName;
90+
}
91+
92+
foreach ($stillMissingGroups as $groupId) {
93+
$result[$groupId] = null;
94+
}
95+
96+
return $result;
97+
}
98+
6099
public function clear(): void {
61100
$this->cache = new CappedMemoryCache();
62101
$this->memCache->clear();

lib/private/Group/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected function getGroupObject($gid, $displayName = null) {
181181
* @param array<string, string> $displayNames Array containing already know display name for a groupId
182182
* @return array<string, IGroup>
183183
*/
184-
protected function getGroupsObjects(array $gids, array $displayNames = []): array {
184+
public function getGroupsObjects(array $gids, array $displayNames = []): array {
185185
$backends = [];
186186
$groups = [];
187187
foreach ($gids as $gid) {

0 commit comments

Comments
 (0)