fix(routes/mercari): set cache max age to routeExpire (#20653)

Item description and price may change, and it can be critical to get the
latest price, otherwise the item may be bought by others.

However, it's still useful to cache the result, as it might be used by
other routes (e.g. different keywords or sorting parameters).
This commit is contained in:
Yufan You
2025-12-09 20:29:06 +08:00
committed by GitHub
parent dbb692462d
commit 7fc7a62a25
2 changed files with 8 additions and 2 deletions
+4 -1
View File
@@ -1,3 +1,4 @@
import { config } from '@/config';
import type { Route } from '@/types';
import cache from '@/utils/cache';
@@ -58,7 +59,9 @@ async function handler(ctx) {
const { sort, order, status, keyword } = ctx.req.param();
const statusArray = MercariStatus[status] ? [MercariStatus[status]] : [];
const searchItems = (await fetchSearchItems(MercariSort[sort], MercariOrder[order], statusArray, keyword)).items;
const items = await Promise.all(searchItems.map((item) => cache.tryGet(`mercari:${item.id}`, async () => await fetchItemDetail(item.id, item.itemType).then((detail) => formatItemDetail(detail)))));
const items = await Promise.all(
searchItems.map((item) => cache.tryGet(`mercari:${item.id}`, async () => await fetchItemDetail(item.id, item.itemType).then((detail) => formatItemDetail(detail)), config.cache.routeExpire, false))
);
return {
title: `${keyword} の検索結果`,
+4 -1
View File
@@ -1,3 +1,4 @@
import { config } from '@/config';
import type { Route } from '@/types';
import cache from '@/utils/cache';
@@ -87,7 +88,9 @@ async function handler(ctx) {
const { keyword, sort, order, status, options } = parseSearchQuery(queryString);
const searchItems = (await fetchSearchItems(sort, order, status, keyword, options)).items;
const items = await Promise.all(searchItems.map((item) => cache.tryGet(`mercari:${item.id}`, async () => await fetchItemDetail(item.id, item.itemType).then((detail) => formatItemDetail(detail)))));
const items = await Promise.all(
searchItems.map((item) => cache.tryGet(`mercari:${item.id}`, async () => await fetchItemDetail(item.id, item.itemType).then((detail) => formatItemDetail(detail)), config.cache.routeExpire, false))
);
return {
title: `${keyword} の検索結果`,