From cfe7fc7ac473c85340924f75570f065f0c936802 Mon Sep 17 00:00:00 2001 From: Claus-Justus Heine Date: Tue, 1 Feb 2022 14:12:37 +0100 Subject: [PATCH] Always cache the complete user record. 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. Signed-off-by: Claus-Justus Heine --- lib/Backend/UserBackend.php | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/Backend/UserBackend.php b/lib/Backend/UserBackend.php index c78bc0d..458336a 100644 --- a/lib/Backend/UserBackend.php +++ b/lib/Backend/UserBackend.php @@ -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) @@ -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; }