Skip to content

Commit 1af5574

Browse files
st3inykesselb
authored andcommitted
perf: cache pre-fetched mailboxes on the HTTP level
Signed-off-by: Richard Steinmetz <[email protected]>
1 parent 3e94377 commit 1af5574

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

lib/Controller/MessagesController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public function index(int $mailboxId,
136136
?int $cursor = null,
137137
?string $filter = null,
138138
?int $limit = null,
139-
?string $view = null): JSONResponse {
139+
?string $view = null,
140+
?string $etag = null /* Is sent to guarantee unique urls for the cache */): JSONResponse {
140141
try {
141142
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $mailboxId);
142143
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
@@ -145,7 +146,7 @@ public function index(int $mailboxId,
145146
}
146147

147148
$this->logger->debug("loading messages of mailbox <$mailboxId>");
148-
$sort = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? IMailSearch::ORDER_NEWEST_FIRST: IMailSearch::ORDER_OLDEST_FIRST;
149+
$sort = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? IMailSearch::ORDER_NEWEST_FIRST : IMailSearch::ORDER_OLDEST_FIRST;
149150

150151
$view = $view === 'singleton' ? IMailSearch::VIEW_SINGLETON : IMailSearch::VIEW_THREADED;
151152

@@ -159,9 +160,12 @@ public function index(int $mailboxId,
159160
$this->currentUserId,
160161
$view
161162
);
162-
return new JSONResponse(
163-
$messages
164-
);
163+
164+
$response = new JSONResponse($messages);
165+
if ($etag) {
166+
$response->cacheFor(7 * 24 * 3600, false, true);
167+
}
168+
return $response;
165169
}
166170

167171
/**

lib/Db/Mailbox.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ public function getStats(): MailboxStats {
141141
return new MailboxStats($this->getMessages(), $this->getUnseen());
142142
}
143143

144+
public function getEtag(): string {
145+
$hash = hash('md5', implode('|', [
146+
(string)$this->getId(),
147+
$this->getSyncNewToken() ?? 'null',
148+
$this->getSyncChangedToken() ?? 'null',
149+
$this->getSyncVanishedToken() ?? 'null',
150+
]));
151+
return "\"$hash\"";
152+
}
153+
144154
#[\Override]
145155
#[ReturnTypeWillChange]
146156
public function jsonSerialize() {
@@ -160,6 +170,7 @@ public function jsonSerialize() {
160170
'unread' => $this->unseen,
161171
'myAcls' => $this->myAcls,
162172
'shared' => $this->shared === true,
173+
'etag' => $this->getEtag(),
163174
];
164175
}
165176
}

src/components/Mailbox.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export default {
274274
const envelopes = await this.mainStore.fetchEnvelopes({
275275
mailboxId: mailbox.databaseId,
276276
limit: this.initialPageSize,
277+
includeEtag: true,
277278
})
278279
this.syncedMailboxes.add(mailbox.databaseId)
279280
logger.debug(`Prefetched ${envelopes.length} envelopes for folder ${mailbox.displayName} (${mailbox.databaseId})`)

src/service/MessageService.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function fetchEnvelope(accountId, id) {
3232
})
3333
}
3434

35-
export function fetchEnvelopes(accountId, mailboxId, query, cursor, limit, sort, view) {
35+
export function fetchEnvelopes(accountId, mailboxId, query, cursor, limit, sort, view, etag) {
3636
const url = generateUrl('/apps/mail/api/messages')
3737
const params = {
3838
mailboxId,
@@ -53,6 +53,9 @@ export function fetchEnvelopes(accountId, mailboxId, query, cursor, limit, sort,
5353
if (view) {
5454
params.view = view
5555
}
56+
if (etag) {
57+
params.etag = etag
58+
}
5659

5760
return axios
5861
.get(url, {

src/store/mainStore/actions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ export default function mainStoreActions() {
662662
mailboxId,
663663
query,
664664
addToUnifiedMailboxes = true,
665+
includeEtag = false,
665666
}) {
666667
return handleHttpAuthErrors(async () => {
667668
const mailbox = this.getMailbox(mailboxId)
@@ -709,7 +710,7 @@ export default function mainStoreActions() {
709710
}),
710711
),
711712
),
712-
)(mailbox.accountId, mailboxId, query, undefined, PAGE_SIZE, this.getPreference('sort-order'), this.getPreference('layout-message-view'))
713+
)(mailbox.accountId, mailboxId, query, undefined, PAGE_SIZE, this.getPreference('sort-order'), this.getPreference('layout-message-view'), includeEtag ? mailbox.etag : undefined)
713714
})
714715
},
715716
async fetchNextEnvelopePage({

0 commit comments

Comments
 (0)