File size: 1,858 Bytes
4e83772 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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<T>(key: string): Promise<T | null> {
const value = await this.client.get(key);
return value ? JSON.parse(value) : null;
}
async set(key: string, value: unknown, ttlSeconds?: number): Promise<void> {
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<void> {
await this.client.del(key);
}
async exists(key: string): Promise<boolean> {
return (await this.client.exists(key)) === 1;
}
async setAdd(key: string, value: string): Promise<number> {
return await this.client.sadd(key, value);
}
async setRemove(key: string, value: string): Promise<number> {
return await this.client.srem(key, value);
}
async getSetLength(key: string): Promise<number> {
return await this.client.scard(key);
}
async incrementKey(key: string): Promise<number> {
return this.client.incr(key);
}
async decrementKey(key: string): Promise<number> {
return this.client.decr(key);
}
async setKeyExpiry(key: string, time: number): Promise<number> {
return this.client.expire(key, time);
}
async publishInvalidation(type: string, slug: string): Promise<number> {
return this.client.publish("cache:invalidate", JSON.stringify({ type, slug }));
}
async publishBuild(data: { deploymentId: string; projectId: string; envs: { name: string; value: string }[] }): Promise<number> {
return this.client.publish("build:start", JSON.stringify(data));
}
async disconnect(): Promise<void> {
await this.client.quit();
}
}
export default RedisService;
|