pollo/src/server/redis.ts

37 lines
777 B
TypeScript
Raw Normal View History

2023-06-18 23:45:04 -06:00
import { Redis } from "@upstash/redis";
2023-07-03 15:37:14 -06:00
import { env } from "~/env.mjs";
2023-08-12 19:12:01 -06:00
import https from "https";
2023-06-18 23:45:04 -06:00
2023-08-12 19:12:01 -06:00
export const redis = Redis.fromEnv({
agent: new https.Agent({ keepAlive: true }),
});
2023-07-03 15:37:14 -06:00
export const setCache = async <T>(key: string, value: T) => {
try {
await redis.set(`${env.APP_ENV}_${key}`, value, {
ex: Number(env.UPSTASH_REDIS_EXPIRY_SECONDS),
});
return true;
} catch {
return false;
}
};
export const fetchCache = async <T>(key: string) => {
try {
const result = await redis.get(`${env.APP_ENV}_${key}`);
return result as T;
} catch {
return null;
}
};
export const invalidateCache = async (key: string) => {
try {
await redis.del(`${env.APP_ENV}_${key}`);
return true;
} catch {
return false;
}
};