diff options
| author | trisusilo48 <tri.susilo@altama.co.id> | 2024-11-26 11:57:44 +0700 |
|---|---|---|
| committer | trisusilo48 <tri.susilo@altama.co.id> | 2024-11-26 11:57:44 +0700 |
| commit | 038d1db4d1ed4839a329128b425649d60146d096 (patch) | |
| tree | 386a6a5f42e2327e5613ce64dedbe2acc5331e08 /src/pages/api/flashsale-header.js | |
| parent | 821d218ff687a585c99937948989408541b596e4 (diff) | |
| parent | f254d47618ddfd2c1e37a51562b9728d62bd8eb6 (diff) | |
Merge branch 'new-release' into feature/integrasi_biteship
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' }); + } +} |
