Add: Cache full Category list

This commit is contained in:
Donatas Kirda 2024-05-17 08:34:52 +03:00
parent e62ee40950
commit 33aa98e456
Signed by: bloodwiing
GPG Key ID: 63020D8D3F4A164F

View File

@ -45,6 +45,10 @@ export const getCategoriesCachedByRef = refExtendCachedMethod(getCategoriesCache
export async function getCategories(category_ids = undefined) { export async function getCategories(category_ids = undefined) {
if (category_ids !== undefined && category_ids.length == 0) return new Map(); if (category_ids !== undefined && category_ids.length == 0) return new Map();
if (category_ids === undefined && cache.has('all')) {
return /** @type {Result<Category>} */ (cache.get('all'));
}
const filter = category_ids ? sql`WHERE id IN ${ sql(category_ids) }` : sql``; const filter = category_ids ? sql`WHERE id IN ${ sql(category_ids) }` : sql``;
const query = sql` const query = sql`
@ -61,7 +65,11 @@ export async function getCategories(category_ids = undefined) {
categories.forEach(row => { categories.forEach(row => {
result.set(row['id'], parseCategoryFromRow(row)); result.set(row['id'], parseCategoryFromRow(row));
}) });
if (category_ids === undefined) {
cache.set('all', result);
}
return updateCategoryCache(result); return updateCategoryCache(result);
} }