Skip to content

Commit 7eb5d1b

Browse files
authored
Merge pull request #2366 from nextcloud/perf/insert-cache-bm-count
perf(BookmarkMapper): Cache bookmark count
2 parents 3b40559 + b8f25e4 commit 7eb5d1b

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

lib/Db/BookmarkMapper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class BookmarkMapper extends QBMapper {
7676
*/
7777
private $shareMapper;
7878

79+
/** @var array<string,int> */
80+
private array $userBookmarkCount = [];
81+
7982
/**
8083
* BookmarkMapper constructor.
8184
*
@@ -155,6 +158,9 @@ public function deleteAll(string $userId): void {
155158
$bm = $this->find($bookmark);
156159
$this->delete($bm);
157160
}
161+
if (isset($this->userBookmarkCount[$userId])) {
162+
$this->userBookmarkCount[$userId] = 0;
163+
}
158164
$result->closeCursor();
159165
}
160166

@@ -838,6 +844,9 @@ public function delete(Entity $entity): Bookmark {
838844
);
839845

840846
$returnedEntity = parent::delete($entity);
847+
if (isset($this->userBookmarkCount[$entity->getUserId()])) {
848+
$this->userBookmarkCount[$entity->getUserId()] -= 1;
849+
}
841850

842851
$id = $entity->getId();
843852

@@ -901,6 +910,9 @@ public function insert(Entity $entity): Bookmark {
901910

902911
try {
903912
parent::insert($entityToInsert);
913+
if (isset($this->userBookmarkCount[$entityToInsert->getUserId()])) {
914+
$this->userBookmarkCount[$entityToInsert->getUserId()] += 1;
915+
}
904916
$this->eventDispatcher->dispatchTyped(new InsertEvent('bookmark', $entityToInsert->getId()));
905917
return $entityToInsert;
906918
} catch (Exception $e) {
@@ -946,6 +958,9 @@ public function insertOrUpdate(Entity $entity): Bookmark {
946958
* @throws Exception
947959
*/
948960
public function countBookmarksOfUser(string $userId) : int {
961+
if (isset($this->userBookmarkCount[$userId])) {
962+
return $this->userBookmarkCount[$userId];
963+
}
949964
$qb = $this->db->getQueryBuilder();
950965
$qb
951966
->select($qb->func()->count('id'))
@@ -957,6 +972,7 @@ public function countBookmarksOfUser(string $userId) : int {
957972
$count = 0;
958973
}
959974
$result->closeCursor();
975+
$this->userBookmarkCount[$userId] = (int)$count;
960976
return (int)$count;
961977
}
962978

0 commit comments

Comments
 (0)