diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2024-11-15 07:39:14 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2024-11-15 07:39:14 +0000 |
| commit | 7ca8fce4ac97ffc31042dd4d016460a5e61284a5 (patch) | |
| tree | d0f1bb7711e2d93859e1d534551328b14a3ae78e /src/pages/api/page-content.js | |
| parent | 49f2e6a5612d000c3a740513c1a54b73bb656d77 (diff) | |
| parent | e5544ace96dd9a200ca5876b8e9ba837ad0a26ac (diff) | |
Merged in CR/redis (pull request #379)
CR/redis
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' }); + } +} |
