Skip to content

Commit 20fab3e

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
perf: Batch getting the display names of groups
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent b2da15d commit 20fab3e

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
@@ -274,10 +274,11 @@ protected function findGroupsWithDisplayname(array $userDetails): array {
274274
$groupIds = array_unique($groupIds);
275275
sort($groupIds);
276276

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

283284
/**

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
@@ -55,6 +55,45 @@ public function getDisplayName(string $groupId): ?string {
5555
return $displayName;
5656
}
5757

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

lib/private/Group/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected function getGroupObject($gid, $displayName = null) {
170170
* @param array<string, string> $displayNames Array containing already know display name for a groupId
171171
* @return array<string, IGroup>
172172
*/
173-
protected function getGroupsObjects(array $gids, array $displayNames = []): array {
173+
public function getGroupsObjects(array $gids, array $displayNames = []): array {
174174
$backends = [];
175175
$groups = [];
176176
foreach ($gids as $gid) {

0 commit comments

Comments
 (0)