Skip to content

Commit

Permalink
perf: Cache full/partial board data differently
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Feb 17, 2023
1 parent 29d21e0 commit b19b779
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/Controller/BoardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
52 changes: 32 additions & 20 deletions lib/Service/BoardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b19b779

Please sign in to comment.