Skip to content

Commit

Permalink
Always cache the complete user record.
Browse files Browse the repository at this point in the history
Rationale: the callback which tweaks the result is not part of the cache
key and differes between calls to getUsers(). So either the cache-key
would have to code the callback or we simply cache the complete user
record, regardless of the callback.
  • Loading branch information
rotdrop committed Feb 1, 2022
1 parent 91e8cac commit d4970ce
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions lib/Backend/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,23 +455,40 @@ public function getUsers($search = "", $limit = null, $offset = null, $callback
$users = $this->cache->get($cacheKey);

if (!is_null($users)) {

$this->logger->debug(
"Returning from cache getUsers($search, $limit, $offset): count("
. count($users) . ")", ["app" => $this->appName]
);
return $users;
}
$users = array_map(function($array) {
$object = new User();
foreach ($array as $key => $value) {
$object->{$key} = $value;
}
return $object;
}, $users);

$users = $this->userRepository->findAllBySearchTerm(
"%" . $search . "%", $limit, $offset
);
} else {

if ($users === false) {
return [];
}
$users = $this->userRepository->findAllBySearchTerm(
"%" . $search . "%", $limit, $offset
);

foreach ($users as $user) {
$this->cache->set("user_" . $user->uid, $user);
if ($users === false) {
return [];
}

foreach ($users as $user) {
$this->cache->set("user_" . $user->uid, $user);
}

$this->cache->set($cacheKey, $users);
$this->logger->debug(
"Returning getUsers($search, $limit, $offset): count("
. count(
$users
) . ")", ["app" => $this->appName]
);
}

$callback = is_callable($callback)
Expand All @@ -481,14 +498,6 @@ public function getUsers($search = "", $limit = null, $offset = null, $callback
};
$users = array_map($callback, $users);

$this->cache->set($cacheKey, $users);
$this->logger->debug(
"Returning getUsers($search, $limit, $offset): count("
. count(
$users
) . ")", ["app" => $this->appName]
);

return $users;
}

Expand Down

0 comments on commit d4970ce

Please sign in to comment.