import { IRedisCache } from "@/interfaces/cache/IRedisCache.js"; import { Redis } from "ioredis"; class RedisService implements IRedisCache { private client: Redis; constructor(client: Redis) { this.client = client; } async get(key: string): Promise { const value = await this.client.get(key); return value ? JSON.parse(value) : null; } async set(key: string, value: unknown, ttlSeconds?: number): Promise { const data = JSON.stringify(value); if (ttlSeconds) { await this.client.set(key, data, "EX", ttlSeconds); } else { await this.client.set(key, data, "EX", 1800); } } async del(key: string): Promise { await this.client.del(key); } async exists(key: string): Promise { return (await this.client.exists(key)) === 1; } async setAdd(key: string, value: string): Promise { return await this.client.sadd(key, value); } async setRemove(key: string, value: string): Promise { return await this.client.srem(key, value); } async getSetLength(key: string): Promise { return await this.client.scard(key); } async incrementKey(key: string): Promise { return this.client.incr(key); } async decrementKey(key: string): Promise { return this.client.decr(key); } async setKeyExpiry(key: string, time: number): Promise { return this.client.expire(key, time); } async publishInvalidation(type: string, slug: string): Promise { return this.client.publish("cache:invalidate", JSON.stringify({ type, slug })); } async publishBuild(data: { deploymentId: string; projectId: string; envs: { name: string; value: string }[] }): Promise { return this.client.publish("build:start", JSON.stringify(data)); } async disconnect(): Promise { await this.client.quit(); } } export default RedisService;