diff options
| author | trisusilo48 <tri.susilo@altama.co.id> | 2024-11-18 11:35:58 +0700 |
|---|---|---|
| committer | trisusilo48 <tri.susilo@altama.co.id> | 2024-11-18 11:35:58 +0700 |
| commit | fb58a58715a7f5530a60479487457e5e0a1a41ec (patch) | |
| tree | 733f0332f127a4d6bc931330ec1cd103968dbb7d /src/pages/api/banner-section.js | |
| parent | 0d4278bd482d2ec2563b29cb3597eb8c7227a2d7 (diff) | |
| parent | bde516b6b39cccfe8ac3248cd7f85592e6298d7a (diff) | |
Merge branch 'new-release' into feature/integrasi_biteship
Diffstat (limited to 'src/pages/api/banner-section.js')
| -rw-r--r-- | src/pages/api/banner-section.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/pages/api/banner-section.js b/src/pages/api/banner-section.js new file mode 100644 index 00000000..7d7040c0 --- /dev/null +++ b/src/pages/api/banner-section.js @@ -0,0 +1,44 @@ +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 = 'hero-banner'; + // 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 dataBannerSections = await odooApi( + 'GET', + '/api/v1/banner?type=home-banner' + ); + + // Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik) + await client.set( + cacheKey, + JSON.stringify(dataBannerSections), + 'EX', + 259200 + ); + + return res.status(200).json({ data: dataBannerSections }); + } + } catch (error) { + console.error('Error interacting with Redis or fetching data:', error); + return res.status(500).json({ error: 'Internal Server Error' }); + } +} |
