server/utils/redis.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

/* import type { Prisma } from "@/prisma/client";
2023-12-02 00:00:00 +01:00
import chalk from "chalk";
2024-04-07 06:16:54 +02:00
import { config } from "config-manager";
2023-12-02 00:00:00 +01:00
import Redis from "ioredis";
import { createPrismaRedisCache } from "prisma-redis-middleware";
const cacheRedis = config.redis.cache.enabled
2024-04-07 07:30:49 +02:00
? new Redis({
host: config.redis.cache.host,
port: Number(config.redis.cache.port),
password: config.redis.cache.password,
db: Number(config.redis.cache.database),
})
: null;
cacheRedis?.on("error", (e) => {
console.log(e);
2023-12-02 00:00:00 +01:00
});
export { cacheRedis };
export const initializeRedisCache = async () => {
2024-04-07 07:30:49 +02:00
if (cacheRedis) {
// Test connection
try {
await cacheRedis.ping();
} catch (e) {
console.error(
`${chalk.red("✗")} ${chalk.bold(
"Error while connecting to Redis",
)}`,
);
throw e;
}
console.log(`${chalk.green("✓")} ${chalk.bold("Connected to Redis")}`);
const cacheMiddleware: Prisma.Middleware = createPrismaRedisCache({
storage: {
type: "redis",
options: {
client: cacheRedis,
invalidation: {
referencesTTL: 300,
},
},
},
cacheTime: 300,
onError: (e) => {
console.error(e);
},
});
return cacheMiddleware;
}
return null;
2023-12-02 00:00:00 +01:00
};
2024-04-14 02:46:33 +02:00
*/