summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/pages/api/search-flashsale.js24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/pages/api/search-flashsale.js b/src/pages/api/search-flashsale.js
index 4e62c329..6555bfaf 100644
--- a/src/pages/api/search-flashsale.js
+++ b/src/pages/api/search-flashsale.js
@@ -21,41 +21,37 @@ export default async function handler(req, res) {
try {
await connectRedis();
-
const cacheKey = `flashsale_product:${operation}:${query}`;
-
let cachedData = await client.get(cacheKey);
if (cachedData) {
const data = JSON.parse(cachedData);
-
+
if (!data || (Array.isArray(data) && data.length === 0)) {
- await client.del(cacheKey)
+ await client.del(cacheKey);
return res.status(200).json({ data: [] });
}
return res.status(200).json({ data });
} else {
-
+ const qstr = String(query || '').replace(/^\?/, '');
const dataProductSearch = await axios(
- `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}&operation=${operation}`
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${qstr}&operation=${operation}`
);
- if (dataProductSearch.data.length === 0) {
+ const payload = dataProductSearch.data;
+ const arr = Array.isArray(payload) ? payload : payload?.data || [];
+
+ if (arr.length === 0) {
return res.status(200).json({ data: [] });
} else {
-
- const ttl = Math.max(1, parseInt(String(duration), 10) || 300);
-
await client.set(
cacheKey,
- JSON.stringify(dataProductSearch.data),
+ JSON.stringify(arr),
'EX',
- tt
+ Math.max(1, parseInt(String(duration), 10) || 300)
);
cachedData = await client.get(cacheKey);
-
-
return res.status(200).json({ data: JSON.parse(cachedData) });
}
}