perf: Cache full/partial board data differently

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2023-02-16 18:41:47 +01:00
parent 29d21e05e8
commit b19b7794bc
2 changed files with 33 additions and 21 deletions

View File

@@ -65,7 +65,7 @@ class BoardApiController extends ApiController {
public function index($details = null) { public function index($details = null) {
$modified = $this->request->getHeader('If-Modified-Since'); $modified = $this->request->getHeader('If-Modified-Since');
if ($modified === null || $modified === '') { if ($modified === null || $modified === '') {
$boards = $this->boardService->findAll(0, $details); $boards = $this->boardService->findAll(0, $details === true);
} else { } else {
$date = Util::parseHTTPDate($modified); $date = Util::parseHTTPDate($modified);
if (!$date) { if (!$date) {

View File

@@ -79,7 +79,8 @@ class BoardService {
private IEventDispatcher $eventDispatcher; private IEventDispatcher $eventDispatcher;
private ChangeHelper $changeHelper; private ChangeHelper $changeHelper;
private CardMapper $cardMapper; private CardMapper $cardMapper;
private ?array $boardsCache = null; private ?array $boardsCacheFull = null;
private ?array $boardsCachePartial = null;
private IURLGenerator $urlGenerator; private IURLGenerator $urlGenerator;
private IDBConnection $connection; private IDBConnection $connection;
private BoardServiceValidator $boardServiceValidator; private BoardServiceValidator $boardServiceValidator;
@@ -147,39 +148,42 @@ class BoardService {
} }
/** /**
* @return array * @return Board[]
*/ */
public function findAll($since = -1, $details = null, $includeArchived = true) { public function findAll(int $since = -1, bool $fullDetails = false, bool $includeArchived = true): array {
if ($this->boardsCache) { if ($this->boardsCacheFull && $fullDetails) {
return $this->boardsCache; return $this->boardsCacheFull;
} }
if ($this->boardsCachePartial && !$fullDetails) {
return $this->boardsCachePartial;
}
$complete = $this->getUserBoards($since, $includeArchived); $complete = $this->getUserBoards($since, $includeArchived);
$result = $this->enrichBoards($complete, $details !== null); return $this->enrichBoards($complete, $fullDetails !== null);
$this->boardsCache = $result;
return array_values($result);
} }
/** /**
* @param $boardId
* @return Board
* @throws DoesNotExistException * @throws DoesNotExistException
* @throws \OCA\Deck\NoPermissionException * @throws \OCA\Deck\NoPermissionException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws BadRequestException * @throws BadRequestException
*/ */
public function find($boardId, $fullDetails = true) { public function find(int $boardId, bool $fullDetails = true): Board {
$this->boardServiceValidator->check(compact('boardId')); $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); $this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
/** @var Board $board */ /** @var Board $board */
$board = $this->boardMapper->find($boardId, true, true); $board = $this->boardMapper->find($boardId, true, true);
[$board] = array_values($this->enrichBoards([$board], $fullDetails)); [$board] = $this->enrichBoards([$board], $fullDetails);
if ($fullDetails) {
$this->boardsCache[$boardId] = $board;
}
return $board; return $board;
} }
@@ -665,9 +669,16 @@ class BoardService {
$this->enrichWithActiveSessions($board); $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) { private function enrichWithStacks($board, $since = -1) {
@@ -710,7 +721,8 @@ class BoardService {
$boardOwnerId = $board->getOwner(); $boardOwnerId = $board->getOwner();
$this->boardMapper->flushCache($boardId, $boardOwnerId); $this->boardMapper->flushCache($boardId, $boardOwnerId);
unset($this->boardsCache[$boardId]); unset($this->boardsCacheFull[$boardId]);
unset($this->boardsCachePartial[$boardId]);
} }
private function enrichWithCards($board) { private function enrichWithCards($board) {