blob: b7f3c1c5774ed0fc01d61b0804e91bcb8baa2eb3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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) {
const { type } = req.query;
try {
await connectRedis();
const cacheKey = `homepage_bannerSection_${type}`;
// 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=${type}`
);
if(!dataBannerSections) return res.status(200).json({ data: [] });
// Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik)
await client.set(
cacheKey,
JSON.stringify(dataBannerSections),
'EX',
259200
);
cachedData = await client.get(cacheKey);
return res.status(200).json({ data: cachedData });
}
} catch (error) {
console.error('Error interacting with Redis or fetching data:', error);
return res.status(500).json({ error: 'Internal Server Error' });
}
}
|