Commit 037b0fd1 by Ravindra Kanojiya

updated redis clear cache

1 parent 8334a2b2
......@@ -6,6 +6,7 @@ import Breadcrumb from "@/components/Common/Breadcrumb";
import InnerBanner from "@/components/Common/InnerBanner";
import { Contact } from "@/container/Home/Contact";
import { getAboutBySlug } from "@/services/aboutApi";
import { getRedisClient } from "@/redis-client";
import Head from "next/head";
const bannerData = [
{
......@@ -40,16 +41,21 @@ const AboutPage = ({ aboutData = {} }) => {
};
export default AboutPage;
export async function getServerSideProps({ params }) {
try {
const aboutData = await getAboutBySlug();
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_about`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: { aboutData: JSON.parse(cachedData) } };
}
return {
props: {
aboutData,
},
};
const aboutData = await getAboutBySlug();
await redis.set(REDIS_KEY, JSON.stringify(aboutData), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: { aboutData } };
} catch (error) {
console.error("Product page SSR error:", error);
return { notFound: true };
......
......@@ -8,6 +8,7 @@ import AboutInfoBrands from "@/components/Inalco/AboutInfoBrands";
import CompanyOverview from "@/components/Inalco/CompanyOverview";
import WeCare from "@/components/Inalco/WeCare";
import { getInalcoBySlug } from "@/services/inalcoApi";
import { getRedisClient } from "@/redis-client";
const bannerData = [
{
id: 8,
......@@ -53,16 +54,21 @@ export default function inalcoPage({ brandData, inalcoData }) {
);
}
export async function getServerSideProps({ params }) {
try {
const inalcoData = await getInalcoBySlug();
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_inalco`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: { inalcoData: JSON.parse(cachedData) } };
}
return {
props: {
inalcoData,
},
};
const inalcoData = await getInalcoBySlug();
await redis.set(REDIS_KEY, JSON.stringify(inalcoData), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: { inalcoData } };
} catch (error) {
console.error("Product page SSR error:", error);
return { notFound: true };
......
......@@ -8,6 +8,7 @@ import Head from "next/head";
import AboutInfoBrands from "@/components/Rimadesios/AboutInfoBrands";
import CompanyOverview from "@/components/Rimadesios/CompanyOverview";
import { getRimadesioBySlug } from "@/services/rimadesioApi";
import { getRedisClient } from "@/redis-client";
const bannerData = [
{
id: 8,
......@@ -54,16 +55,21 @@ export default function rimadesiosPage({ brandData, rimadesioData }) {
}
export async function getServerSideProps({ params }) {
try {
const rimadesioData = await getRimadesioBySlug();
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_rimadesio`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: { rimadesioData: JSON.parse(cachedData) } };
}
return {
props: {
rimadesioData,
},
};
const rimadesioData = await getRimadesioBySlug();
await redis.set(REDIS_KEY, JSON.stringify(rimadesioData), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: { rimadesioData } };
} catch (error) {
console.error("Product page SSR error:", error);
return { notFound: true };
......
......@@ -8,6 +8,7 @@ import { Contact } from "@/container/Home/Contact";
import { getBrandsBySlug } from "@/services/brandsApi";
import Head from "next/head";
import { getValcucineBySlug } from "@/services/valcucineApi";
import { getRedisClient } from "@/redis-client";
const bannerData = [
{
id: 8,
......@@ -57,16 +58,21 @@ export default function valcucinesPage({ brandData, valcucineData }) {
export async function getServerSideProps({ params }) {
try {
const valcucineData = await getValcucineBySlug();
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_valcucine`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: { valcucineData: JSON.parse(cachedData) } };
}
return {
props: {
valcucineData,
},
};
const valcucineData = await getValcucineBySlug();
await redis.set(REDIS_KEY, JSON.stringify(valcucineData), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: { valcucineData } };
} catch (error) {
console.error("Product page SSR error:", error);
return { notFound: true };
......
......@@ -5,6 +5,7 @@ import HeadTitle from "@/components/Common/HeadTitle";
import InnerBanner from "@/components/Common/InnerBanner";
import InnerContent from "@/components/Contacts/InnerContent";
import { getCataloguesBySlug } from "@/services/cataloguesApi";
import { getRedisClient } from "@/redis-client";
import Head from "next/head";
const bannerData = [
{
......@@ -44,16 +45,21 @@ const ContactsPage = ({cataloguesData={cataloguesData}}) => {
};
export default ContactsPage;
export async function getServerSideProps({ params }) {
try {
const cataloguesData = await getCataloguesBySlug();
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_catalogues`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: { cataloguesData: JSON.parse(cachedData) } };
}
return {
props: {
cataloguesData,
},
};
const cataloguesData = await getCataloguesBySlug();
await redis.set(REDIS_KEY, JSON.stringify(cataloguesData), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: { cataloguesData } };
} catch (error) {
console.error("Product page SSR error:", error);
return { notFound: true };
......
......@@ -8,6 +8,7 @@ import Catalogues from "@/container/Home/Catalogues";
import { Contact } from "@/container/Home/Contact";
import { getCollectionDetailCategoryData } from "@/services/collectionDetailCategoryApi";
import { getCataloguesBySlug } from "@/services/cataloguesApi";
import { getRedisClient } from "@/redis-client";
import CompanyOverview from "@/components/Collection/CompanyOverview";
import TechnicalDetails from "@/components/Collection/TechnicalDetails";
import Explore from "@/components/Collection/Explore";
......@@ -153,8 +154,17 @@ export default ProductPage;
/* ---------- SSR ---------- */
export async function getServerSideProps({ params }) {
try {
const { productSlug } = params;
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_product_${productSlug}`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: JSON.parse(cachedData) };
}
const productData = await getCollectionDetailCategoryData(productSlug);
const cataloguesData = await getCataloguesBySlug();
......@@ -163,12 +173,10 @@ export async function getServerSideProps({ params }) {
return { notFound: true };
}
return {
props: {
productData: productData[0], // ✅ single product
cataloguesData,
},
};
const result = { productData: productData[0], cataloguesData };
await redis.set(REDIS_KEY, JSON.stringify(result), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: result };
} catch (error) {
console.error("Product page SSR error:", error);
return { notFound: true };
......
......@@ -16,6 +16,7 @@ import { getCollectionDetailCategoryData } from "@/services/collectionDetailCate
import { getCollectionSubCategoryData } from "@/services/collectionSubCategoryApi";
import { getCataloguesBySlug } from "@/services/cataloguesApi";
import { fetchFromStrapi } from "@/services/api";
import { getRedisClient } from "@/redis-client";
import { Tab, Tabs } from "react-bootstrap";
import { useEffect, useState } from "react";
......@@ -194,22 +195,31 @@ export default SubCategoryOrProductPage;
====================================================== */
export async function getServerSideProps({ params }) {
try {
const { subCategory } = params;
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_subcategory_${subCategory}`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: JSON.parse(cachedData) };
}
/* 1️⃣ Check if this slug is a PRODUCT */
const productCheck = await getCollectionDetailCategoryData(subCategory);
if (productCheck && productCheck.length > 0) {
const cataloguesData = await getCataloguesBySlug();
return {
props: {
const result = {
type: "product",
productData: productCheck[0],
cataloguesData,
},
};
await redis.set(REDIS_KEY, JSON.stringify(result), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: result };
}
/* 2️⃣ Otherwise treat as SUBCATEGORY */
......@@ -243,13 +253,10 @@ export async function getServerSideProps({ params }) {
const activeSubCategory =
categoriesSub.find((item) => item.slug === subCategory) || null;
return {
props: {
type: "subcategory",
products,
activeSubCategory,
},
};
const result = { type: "subcategory", products, activeSubCategory };
await redis.set(REDIS_KEY, JSON.stringify(result), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: result };
} catch (error) {
console.error(error);
return {
......
......@@ -4,6 +4,7 @@ import HeadTitle from "@/components/Common/HeadTitle";
import { getCollectionPageData } from "@/services/collectionApi";
import { getCollectionCategoryBySlug } from "@/services/collectionCategoryApi";
import { getCollectionSubCategoryData } from "@/services/collectionSubCategoryApi";
import { getRedisClient } from "@/redis-client";
export default function CategoryPage({ categoryData, categoriesSub,collectionDataa }) {
......@@ -39,26 +40,29 @@ export default function CategoryPage({ categoryData, categoriesSub,collectionDat
}
export async function getServerSideProps({ params }) {
try {
const { category } = params;
const redis = getRedisClient();
const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_category_${category}`;
const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;
try {
const cachedData = await redis.get(REDIS_KEY);
if (cachedData) {
console.log("redis data fetched");
return { props: JSON.parse(cachedData) };
}
const categoryData = await getCollectionCategoryBySlug(category);
const categoriesSub = await getCollectionSubCategoryData(category);
// const collectionDataa = await getCollectionPageData(category);
return {
props: {
categoryData,
categoriesSub,
// collectionDataa
},
};
const result = { categoryData, categoriesSub };
await redis.set(REDIS_KEY, JSON.stringify(result), "EX", REDIS_EXPIRE);
console.log("normal data fetched");
return { props: result };
} catch (error) {
return {
props: {
categoryData: null,
categoriesSub: [],
// collectionDataa:[]
},
};
}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!