Files
Checkmate/server/service/redisService.js
T

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-05-16 14:09:49 -07:00
const SERVICE_NAME = "RedisService";
2025-05-16 14:09:49 -07:00
class RedisService {
static SERVICE_NAME = SERVICE_NAME;
constructor({ Redis, logger }) {
this.Redis = Redis;
this.connections = new Set();
this.logger = logger;
}
2025-05-16 14:09:49 -07:00
getNewConnection(options = {}) {
const connection = new this.Redis(process.env.REDIS_URL, {
retryStrategy: (times) => {
2025-05-16 14:09:49 -07:00
return null;
},
2025-05-16 14:09:49 -07:00
...options,
});
2025-05-16 14:09:49 -07:00
this.connections.add(connection);
return connection;
}
2025-05-05 14:01:17 -07:00
2025-05-16 14:09:49 -07:00
async closeAllConnections() {
const closePromises = Array.from(this.connections).map((conn) =>
conn.quit().catch((err) => {
console.error("Error closing Redis connection:", err);
})
);
2025-05-16 14:09:49 -07:00
await Promise.all(closePromises);
this.connections.clear();
console.log("All Redis connections closed");
}
2025-05-16 14:09:49 -07:00
async flushRedis() {
this.logger.info({
message: "Flushing Redis",
service: SERVICE_NAME,
method: "flushRedis",
});
2025-05-16 14:09:49 -07:00
const flushPromises = Array.from(this.connections).map((conn) => conn.flushall());
await Promise.all(flushPromises);
this.logger.info({
message: "Redis flushed",
service: SERVICE_NAME,
method: "flushRedis",
});
}
}
export default RedisService;