From b19b7794bc773d824275f18d58263c382532790f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 16 Feb 2023 18:41:47 +0100 Subject: [PATCH] perf: Cache full/partial board data differently MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Controller/BoardApiController.php | 2 +- lib/Service/BoardService.php | 52 ++++++++++++++++----------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/Controller/BoardApiController.php b/lib/Controller/BoardApiController.php index cc974f083..a128b5c64 100644 --- a/lib/Controller/BoardApiController.php +++ b/lib/Controller/BoardApiController.php @@ -65,7 +65,7 @@ public function __construct($appName, IRequest $request, BoardService $service, public function index($details = null) { $modified = $this->request->getHeader('If-Modified-Since'); if ($modified === null || $modified === '') { - $boards = $this->boardService->findAll(0, $details); + $boards = $this->boardService->findAll(0, $details === true); } else { $date = Util::parseHTTPDate($modified); if (!$date) { diff --git a/lib/Service/BoardService.php b/lib/Service/BoardService.php index ba0d212aa..ce3326474 100644 --- a/lib/Service/BoardService.php +++ b/lib/Service/BoardService.php @@ -79,7 +79,8 @@ class BoardService { private IEventDispatcher $eventDispatcher; private ChangeHelper $changeHelper; private CardMapper $cardMapper; - private ?array $boardsCache = null; + private ?array $boardsCacheFull = null; + private ?array $boardsCachePartial = null; private IURLGenerator $urlGenerator; private IDBConnection $connection; private BoardServiceValidator $boardServiceValidator; @@ -147,39 +148,42 @@ public function getUserBoards(?int $since = null, bool $includeArchived = true, } /** - * @return array + * @return Board[] */ - public function findAll($since = -1, $details = null, $includeArchived = true) { - if ($this->boardsCache) { - return $this->boardsCache; + public function findAll(int $since = -1, bool $fullDetails = false, bool $includeArchived = true): array { + if ($this->boardsCacheFull && $fullDetails) { + return $this->boardsCacheFull; } + + if ($this->boardsCachePartial && !$fullDetails) { + return $this->boardsCachePartial; + } + $complete = $this->getUserBoards($since, $includeArchived); - $result = $this->enrichBoards($complete, $details !== null); - $this->boardsCache = $result; - return array_values($result); + return $this->enrichBoards($complete, $fullDetails !== null); } /** - * @param $boardId - * @return Board * @throws DoesNotExistException * @throws \OCA\Deck\NoPermissionException * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws BadRequestException */ - public function find($boardId, $fullDetails = true) { + public function find(int $boardId, bool $fullDetails = true): Board { $this->boardServiceValidator->check(compact('boardId')); - if ($this->boardsCache && isset($this->boardsCache[$boardId])) { - return $this->boardsCache[$boardId]; + + if (isset($this->boardsCacheFull[$boardId]) && $fullDetails) { + return $this->boardsCacheFull[$boardId]; + } + + if (isset($this->boardsCachePartial[$boardId]) && !$fullDetails) { + return $this->boardsCachePartial[$boardId]; } $this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ); /** @var Board $board */ $board = $this->boardMapper->find($boardId, true, true); - [$board] = array_values($this->enrichBoards([$board], $fullDetails)); - if ($fullDetails) { - $this->boardsCache[$boardId] = $board; - } + [$board] = $this->enrichBoards([$board], $fullDetails); return $board; } @@ -665,9 +669,16 @@ private function enrichBoards(array $boards, bool $fullDetails = true): array { $this->enrichWithActiveSessions($board); } - $result[$board->getId()] = $board; + // Cache for further usage + if ($fullDetails) { + $this->boardsCacheFull[$board->getId()] = $board; + } else { + $this->boardsCachePartial[$board->getId()] = $board; + } + } - return $result; + + return $boards; } private function enrichWithStacks($board, $since = -1) { @@ -710,7 +721,8 @@ private function clearBoardFromCache(Board $board) { $boardOwnerId = $board->getOwner(); $this->boardMapper->flushCache($boardId, $boardOwnerId); - unset($this->boardsCache[$boardId]); + unset($this->boardsCacheFull[$boardId]); + unset($this->boardsCachePartial[$boardId]); } private function enrichWithCards($board) {