From 0c142968af5390d4cb615d18fae119aa77fb4654 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 2 Dec 2024 09:41:45 +0700 Subject: update redis search product flash sale --- src/pages/api/search-flashsale.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/search-flashsale.js b/src/pages/api/search-flashsale.js index d9e56c83..c00f6c64 100644 --- a/src/pages/api/search-flashsale.js +++ b/src/pages/api/search-flashsale.js @@ -23,20 +23,28 @@ export default async function handler(req, res) { 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); // Hapus kunci jika data kosong + return res.status(200).json({ data: [] }); + } return res.status(200).json({ data }); } else { const dataProductSearch = await axios( `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}&operation=${operation}]` ); - - await client.set( - cacheKey, - JSON.stringify(dataProductSearch.data), - 'EX', - duration - ); - cachedData = await client.get(cacheKey); - return res.status(200).json({ data: cachedData }); + if (dataProductSearch.data.length === 0) { + return res.status(200).json({ data: [] }); + } else { + await client.set( + cacheKey, + JSON.stringify(dataProductSearch.data), + 'EX', + duration + ); + cachedData = await client.get(cacheKey); + return res.status(200).json({ data: cachedData }); + } } } catch (error) { console.error('Error interacting with Redis or fetching data:', error); -- cgit v1.2.3