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/page-content.js | |
| parent | 0d4278bd482d2ec2563b29cb3597eb8c7227a2d7 (diff) | |
| parent | bde516b6b39cccfe8ac3248cd7f85592e6298d7a (diff) | |
Merge branch 'new-release' into feature/integrasi_biteship
Diffstat (limited to 'src/pages/api/page-content.js')
| -rw-r--r-- | src/pages/api/page-content.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/pages/api/page-content.js b/src/pages/api/page-content.js new file mode 100644 index 00000000..3cb8a237 --- /dev/null +++ b/src/pages/api/page-content.js @@ -0,0 +1,44 @@ +import { createClient } from 'redis'; +import { getPageContent } from '~/services/pageContent'; +// import { fetchCategoryManagementSolr } from '../../lib/home/api/categoryManagementApi'; +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) { + const { path } = req.query; + try { + await connectRedis(); + // await client.del('onbording-popup'); + + let cachedData; + if (req.method === 'GET') { + cachedData = await client.get(`page-content:${path}`); + + if (!cachedData) { + const items = await getPageContent({ path }); + console.log('items', items); + await client.set( + `page-content:${path}`, + JSON.stringify(items), + 'EX', + 604800 // Expiry 1 minggu + ); + cachedData = await client.get(`page-content:${path}`); + } + 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' }); + } +} |
