summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-10-06 22:22:34 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-10-06 22:22:34 +0700
commit469d4c6bb4a0b6fc5a6e380158f81ae9e44cf612 (patch)
tree1599a22e609b194e00d2f6eeed72c244e65a6c66
parent5e6e691de128ab17eb73b28e0b13e717d418e405 (diff)
<Miqdad> revert
-rw-r--r--src/pages/api/search-flashsale.js27
1 files changed, 9 insertions, 18 deletions
diff --git a/src/pages/api/search-flashsale.js b/src/pages/api/search-flashsale.js
index 6555bfaf..c00f6c64 100644
--- a/src/pages/api/search-flashsale.js
+++ b/src/pages/api/search-flashsale.js
@@ -15,44 +15,35 @@ const connectRedis = async () => {
export default async function handler(req, res) {
const { query, operation, duration } = req.query;
-
- res.setHeader('Cache-Control', 'no-store');
-
try {
await connectRedis();
-
- const cacheKey = `flashsale_product:${operation}:${query}`;
-
+ const cacheKey = `flashsale_product`;
+ // await client.del(cacheKey);
let cachedData = await client.get(cacheKey);
if (cachedData) {
const data = JSON.parse(cachedData);
-
+ // Periksa apakah data adalah array dan panjangnya 0
if (!data || (Array.isArray(data) && data.length === 0)) {
- await client.del(cacheKey);
+ await client.del(cacheKey); // Hapus kunci jika data kosong
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?${qstr}&operation=${operation}`
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}&operation=${operation}]`
);
-
- const payload = dataProductSearch.data;
- const arr = Array.isArray(payload) ? payload : payload?.data || [];
-
- if (arr.length === 0) {
+ if (dataProductSearch.data.length === 0) {
return res.status(200).json({ data: [] });
} else {
await client.set(
cacheKey,
- JSON.stringify(arr),
+ JSON.stringify(dataProductSearch.data),
'EX',
- Math.max(1, parseInt(String(duration), 10) || 300)
+ duration
);
cachedData = await client.get(cacheKey);
- return res.status(200).json({ data: JSON.parse(cachedData) });
+ return res.status(200).json({ data: cachedData });
}
}
} catch (error) {