Skip to content

Commit

Permalink
perf: Only query the db once for trusted servers
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Dec 5, 2023
1 parent 9fce9ae commit 33fdca1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions apps/federation/lib/TrustedServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\DB\Exception;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\Events\TrustedServerRemovedEvent;
use OCP\HintException;
Expand Down Expand Up @@ -59,6 +60,9 @@ class TrustedServers {
private IEventDispatcher $dispatcher;
private ITimeFactory $timeFactory;

/** @var list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}>|null */
private ?array $trustedServersCache = null;

public function __construct(
DbHandler $dbHandler,
IClientService $httpClientService,
Expand Down Expand Up @@ -126,10 +130,15 @@ public function removeServer(int $id): void {

/**
* Get all trusted servers
*
* @return list<array{id: int, url: string, url_hash: string, shared_secret: string, status: int, sync_token: string}>
*/
* @throws Exception
*/
public function getServers() {
return $this->dbHandler->getAllServer();
if ($this->trustedServersCache === null) {
$this->trustedServersCache = $this->dbHandler->getAllServer();
}
return $this->trustedServersCache;

Check notice

Code scanning / Psalm

LessSpecificReturnStatement Note

The type 'list<array{id: int, shared_secret: null|string, status: int, sync_token: null|string, url: string, url_hash: string}>' is more general than the declared return type 'list<array{id: int, shared_secret: string, status: int, sync_token: string, url: string, url_hash: string}>' for OCA\Federation\TrustedServers::getServers
}

/**
Expand Down

0 comments on commit 33fdca1

Please sign in to comment.