Skip to content

Commit

Permalink
Merge pull request #125 from Somnath-Chattaraj/redis
Browse files Browse the repository at this point in the history
Added redis for caching
  • Loading branch information
tanish35 authored Oct 23, 2024
2 parents 5308a05 + 32579cd commit 0656830
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
88 changes: 88 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"express-async-handler": "^1.2.0",
"fuse.js": "^7.0.0",
"html-to-text": "^9.0.5",
"ioredis": "^5.4.1",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.14",
"nodemon": "^3.1.4",
Expand Down
8 changes: 8 additions & 0 deletions backend/src/controllers/postController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import prisma from "../lib/prisma";
import Fuse from "fuse.js";
import sendMail from "../mail/sendMail";
import { htmlToText } from "html-to-text";
import { setCachedData, getCachedData } from "../lib/redis";

// @ts-ignore
const searchPosts = asyncHandler(async (req: Request, res: Response) => {
Expand All @@ -12,6 +13,12 @@ const searchPosts = asyncHandler(async (req: Request, res: Response) => {
if (!query) {
return res.status(400).json({ message: "Search query is required" });
}
const cacheKey = `search:${query}`;
const cachedResults = await getCachedData(cacheKey);
if (cachedResults) {
return res.status(200).json({ posts: JSON.parse(cachedResults) });
}

const posts = await prisma.post.findMany({
select: {
post_id: true,
Expand Down Expand Up @@ -45,6 +52,7 @@ const searchPosts = asyncHandler(async (req: Request, res: Response) => {
});

const searchResults = fuse.search(query).map((result) => result.item);
await setCachedData(cacheKey, JSON.stringify(searchResults), 3600);

return res.status(200).json({ posts: searchResults });
});
Expand Down
39 changes: 39 additions & 0 deletions backend/src/lib/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Redis from "ioredis";

const redisURL = process.env.REDIS_URL;

if (!redisURL) {
throw new Error("REDIS_URL is not defined");
}
const redis = new Redis(redisURL);

export const getCachedData = async (key: string): Promise<string | null> => {
try {
return await redis.get(key);
} catch (error) {
console.error(`Error fetching data from Redis for key: ${key}`, error);
return null;
}
};

export const setCachedData = async (
key: string,
value: string,
expiry: number = 3600
): Promise<void> => {
try {
await redis.set(key, value, "EX", expiry);
} catch (error) {
console.error(`Error setting data in Redis for key: ${key}`, error);
}
};

export const disconnectRedis = async (): Promise<void> => {
try {
await redis.quit();
} catch (error) {
console.error("Error disconnecting from Redis", error);
}
};

export default redis;

0 comments on commit 0656830

Please sign in to comment.