diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-11-15 15:59:52 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-11-15 15:59:52 +0700 |
| commit | 48441ffd9a92827ffedf5e0b0f4e490132e02c82 (patch) | |
| tree | 4510174886f4c98f543ea21d69d3a53f73f5d1c2 /src/pages/api | |
| parent | d50e637383087cf03d2ad94ad6d076f30f3490c5 (diff) | |
<iman> update radis error code preferredBrand
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/shop/preferredBrand.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/pages/api/shop/preferredBrand.js b/src/pages/api/shop/preferredBrand.js new file mode 100644 index 00000000..9480c63b --- /dev/null +++ b/src/pages/api/shop/preferredBrand.js @@ -0,0 +1,46 @@ +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(); + // await client.del('preferredBrand'); + + let cachedData; + if (req.method === 'GET') { + cachedData = await client.get('preferredBrand'); + + if (!cachedData) { + const items = await odooApi( + 'GET', + '/api/v1/manufacture?level=prioritas' + ); + await client.set( + 'preferredBrand', + JSON.stringify(items), + 'EX', + 259200 // Expiry 3 hari + ); + cachedData = await client.get('preferredBrand'); + } + const data = cachedData ? JSON.parse(cachedData) : null; + res.status(200).json({ data }); + } else { + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } + } catch (error) { + console.error('Error interacting with Redis:', error); + res.status(500).json({ error: 'Error interacting with Redis' }); + } +} |
