Files
Checkmate/server/service/redisService.js
T

59 lines
1.3 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) => {
2025-05-19 11:22:53 -07:00
this.logger.error({
message: "Error closing Redis connection",
service: SERVICE_NAME,
method: "closeAllConnections",
details: { error: err },
});
2025-05-16 14:09:49 -07:00
})
);
2025-05-16 14:09:49 -07:00
await Promise.all(closePromises);
this.connections.clear();
2025-05-19 11:22:53 -07:00
this.logger.info({
message: "All Redis connections closed",
service: SERVICE_NAME,
method: "closeAllConnections",
});
2025-05-16 14:09:49 -07:00
}
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;