diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-12-09 15:01:43 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-12-09 15:01:43 +0700 |
| commit | cecccfaf318e0e7c52132cf1d04c90c0df745d14 (patch) | |
| tree | 6828e39e974478995244558ff81d11eac357c026 /src/pages/api/flashsale-header.js | |
| parent | f2451beed670dc697aa2026b2df6ad1ad1e90da2 (diff) | |
| parent | bb118223e541504262da4509e9188610539702fe (diff) | |
Merge branch 'new-release' into Feature/pengajuan-tempo
Diffstat (limited to 'src/pages/api/flashsale-header.js')
| -rw-r--r-- | src/pages/api/flashsale-header.js | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/src/pages/api/flashsale-header.js b/src/pages/api/flashsale-header.js index 31f8efdd..21cb9c9d 100644 --- a/src/pages/api/flashsale-header.js +++ b/src/pages/api/flashsale-header.js @@ -15,23 +15,35 @@ export default async function handler(req, res) { try { await connectRedis(); const cacheKey = `flashsale_header`; - // await client.del(cacheKey); + + // Cek data yang ada di Redis let cachedData = await client.get(cacheKey); if (cachedData) { - const data = JSON.parse(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 flashSale = await odooApi('GET', `/api/v1/flashsale/header`); + if (flashSale.length === 0) { + return res.status(200).json({ data: [] }); + } else { + await client.set( + cacheKey, + JSON.stringify(flashSale), + 'EX', + flashSale.duration + ); - await client.set( - cacheKey, - JSON.stringify(flashSale), - 'EX', - flashSale.duration - ); - cachedData = await client.get(cacheKey); - return res.status(200).json({ data: cachedData }); + cachedData = await client.get(cacheKey); + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } } } catch (error) { console.error('Error interacting with Redis or fetching data:', error); |
