summaryrefslogtreecommitdiff
path: root/src/pages/api/banner-section.js
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2024-11-15 14:55:57 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2024-11-15 14:55:57 +0700
commit36c6e0361762cb0faff10996e30737a58cf14284 (patch)
tree4cb225b8f106c5ea2ce1d929fc8af0b9757ea0e4 /src/pages/api/banner-section.js
parentc69b71c16ff7cc7a347394710d069ef711db08bb (diff)
parent7ca8fce4ac97ffc31042dd4d016460a5e61284a5 (diff)
Merge branch 'new-release' of https://bitbucket.org/altafixco/next-indoteknik into new-release
Diffstat (limited to 'src/pages/api/banner-section.js')
-rw-r--r--src/pages/api/banner-section.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/pages/api/banner-section.js b/src/pages/api/banner-section.js
new file mode 100644
index 00000000..7d7040c0
--- /dev/null
+++ b/src/pages/api/banner-section.js
@@ -0,0 +1,44 @@
+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();
+ const cacheKey = 'hero-banner';
+ // 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=home-banner'
+ );
+
+ // Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik)
+ await client.set(
+ cacheKey,
+ JSON.stringify(dataBannerSections),
+ 'EX',
+ 259200
+ );
+
+ return res.status(200).json({ data: dataBannerSections });
+ }
+ } catch (error) {
+ console.error('Error interacting with Redis or fetching data:', error);
+ return res.status(500).json({ error: 'Internal Server Error' });
+ }
+}