Commit 8334a2b2 by Ravindra Kanojiya

updated redis

1 parent 048e6e11
import redis from "@/lib/redis"; import redis from "@/lib/redis";
export default async function handler(req, res) { export default async function handler(req, res) {
if (req.method !== "DELETE") { if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" }); return res.status(405).json({ error: "Method not allowed" });
} }
const authKey = req.headers["authorization"]?.replace("Bearer ", "");
if (!authKey || authKey !== process.env.REDIS_CLEAR_CHACE_KEY) {
return res.status(401).json({ error: "Unauthorized" });
}
const prefix = process.env.REDIS_KEY_PREFIX || "Akruti";
try { try {
// ⚠️ Dangerous: deletes all keys const pattern = `${prefix}*`;
await redis.flushall(); let cursor = "0";
let deletedCount = 0;
do {
const [nextCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
cursor = nextCursor;
if (keys.length > 0) {
await redis.del(...keys);
deletedCount += keys.length;
}
} while (cursor !== "0");
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
message: "All Redis keys deleted", message: `Deleted ${deletedCount} keys matching prefix "${prefix}"`,
deletedCount,
}); });
} catch (error) { } catch (error) {
return res.status(500).json({ return res.status(500).json({
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!