pollo/app/_lib/redis.ts

37 lines
765 B
TypeScript
Raw Normal View History

2023-11-21 09:05:09 -07:00
import { Redis } from "ioredis";
2023-09-24 23:49:24 -06:00
import { env } from "env.mjs";
2023-06-18 23:45:04 -06:00
2023-11-21 09:32:54 -07:00
export const redis = env.REDIS_URL
? new Redis(env.REDIS_URL, {
family: 6,
})
: null;
2023-07-03 15:37:14 -06:00
export const setCache = async <T>(key: string, value: T) => {
2023-11-21 09:05:09 -07:00
console.log(env.REDIS_URL);
2023-07-03 15:37:14 -06:00
try {
2023-11-21 09:05:09 -07:00
await redis?.set(`${env.APP_ENV}_${key}`, JSON.stringify(value));
2023-07-03 15:37:14 -06:00
return true;
} catch {
return false;
}
};
export const fetchCache = async <T>(key: string) => {
try {
2023-11-21 09:05:09 -07:00
const result = (await redis?.get(`${env.APP_ENV}_${key}`)) as string;
return JSON.parse(result) as T;
2023-07-03 15:37:14 -06:00
} catch {
return null;
}
};
export const invalidateCache = async (key: string) => {
try {
2023-11-21 09:05:09 -07:00
await redis?.del(`${env.APP_ENV}_${key}`);
2023-07-03 15:37:14 -06:00
return true;
} catch {
return false;
}
};