mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
59 lines
1.2 KiB
TypeScript
59 lines
1.2 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
import chalk from "chalk";
|
|
import { config } from "config-manager";
|
|
import Redis from "ioredis";
|
|
import { createPrismaRedisCache } from "prisma-redis-middleware";
|
|
|
|
const cacheRedis = config.redis.cache.enabled
|
|
? 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);
|
|
});
|
|
|
|
export { cacheRedis };
|
|
|
|
export const initializeRedisCache = async () => {
|
|
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;
|
|
};
|