Skip to content

Commit

Permalink
Improve group queries
Browse files Browse the repository at this point in the history
Before we'd also get the diplayname for each group in the backend. In a
separate query. This is of course not ideal as this information is
obtained on each and every query. Now this is queried once and properly
cached.

Also added more caching to the manager.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
  • Loading branch information
rullzer committed May 2, 2020
1 parent 24bb4a0 commit 5ebb535
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public function createGroup(string $gid): bool {
}

// Add to cache
$this->groupCache[$gid] = $gid;
$this->groupCache[$gid] = [
'gid' => $gid,
'displayname' => $gid
];

return $result === 1;
}
Expand Down Expand Up @@ -244,15 +247,19 @@ public function getUserGroups($uid) {

// No magic!
$qb = $this->dbConn->getQueryBuilder();
$cursor = $qb->select('gid')
->from('group_user')
$cursor = $qb->select('gu.gid', 'g.displayname')
->from('group_user', 'gu')
->leftJoin('gu', 'groups', 'g', $qb->expr()->eq('gu.gid', 'g.gid'))
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->execute();

$groups = [];
while ($row = $cursor->fetch()) {
$groups[] = $row['gid'];
$this->groupCache[$row['gid']] = $row['gid'];
$this->groupCache[$row['gid']] = [
'gid' => $row['gid'],
'displayname' => $row['displayname'],
];
}
$cursor->closeCursor();

Expand Down Expand Up @@ -309,15 +316,18 @@ public function groupExists($gid) {
}

$qb = $this->dbConn->getQueryBuilder();
$cursor = $qb->select('gid')
$cursor = $qb->select('gid', 'displayname')
->from('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
$result = $cursor->fetch();
$cursor->closeCursor();

if ($result !== false) {
$this->groupCache[$gid] = $gid;
$this->groupCache[$gid] = [
'gid' => $gid,
'displayname' => $result['displayname'],
];
return true;
}
return false;
Expand Down Expand Up @@ -430,6 +440,10 @@ public function countDisabledInGroup(string $gid): int {
}

public function getDisplayName(string $gid): string {
if (isset($this->groupCache[$gid])) {
return $this->groupCache[$gid]['displayname'];
}

$this->fixDI();

$query = $this->dbConn->getQueryBuilder();
Expand Down

0 comments on commit 5ebb535

Please sign in to comment.