diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-11-29 10:55:52 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-11-29 10:55:52 +0700 |
| commit | ac83b0ea5afd82194f38fbc913678e16a81b5c2c (patch) | |
| tree | 4373da17bcb43d752b7bd888e28ba906099a19aa /src/pages/api/flashsale-header.js | |
| parent | c7b437cd0541cde9e2a829ec3c5689dc237505a8 (diff) | |
| parent | b4861f70338adb3960125923a7e6e4032279c88e (diff) | |
Merge branch 'new-release' into Feature/pengajuan-tempo
# Conflicts:
# src/lib/auth/components/Menu.jsx
# src/pages/my/menu.jsx
Diffstat (limited to 'src/pages/api/flashsale-header.js')
| -rw-r--r-- | src/pages/api/flashsale-header.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/pages/api/flashsale-header.js b/src/pages/api/flashsale-header.js new file mode 100644 index 00000000..31f8efdd --- /dev/null +++ b/src/pages/api/flashsale-header.js @@ -0,0 +1,40 @@ +import odooApi from '@/core/api/odooApi'; +import { createClient } from 'redis'; + +const client = createClient(); + +client.on('error', (err) => console.error('Redis Client Error', err)); + +const connectRedis = async () => { + if (!client.isOpen) { + await client.connect(); + } +}; + +export default async function handler(req, res) { + try { + await connectRedis(); + const cacheKey = `flashsale_header`; + // await client.del(cacheKey); + let cachedData = await client.get(cacheKey); + + if (cachedData) { + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } else { + const flashSale = await odooApi('GET', `/api/v1/flashsale/header`); + + await client.set( + cacheKey, + JSON.stringify(flashSale), + 'EX', + flashSale.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); + return res.status(500).json({ error: 'Internal Server Error' }); + } +} |
