diff options
| author | trisusilo48 <tri.susilo@altama.co.id> | 2024-11-15 14:55:57 +0700 |
|---|---|---|
| committer | trisusilo48 <tri.susilo@altama.co.id> | 2024-11-15 14:55:57 +0700 |
| commit | 36c6e0361762cb0faff10996e30737a58cf14284 (patch) | |
| tree | 4cb225b8f106c5ea2ce1d929fc8af0b9757ea0e4 /src/pages/api/page-content.js | |
| parent | c69b71c16ff7cc7a347394710d069ef711db08bb (diff) | |
| parent | 7ca8fce4ac97ffc31042dd4d016460a5e61284a5 (diff) | |
Merge branch 'new-release' of https://bitbucket.org/altafixco/next-indoteknik into new-release
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' }); + } +} |
