"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisCacheService = void 0;
class RedisCacheService {
    client;
    constructor(client) {
        this.client = client;
    }
    async get(key) {
        const value = await this.client.get(key);
        if (value === null) {
            return null;
        }
        try {
            return JSON.parse(value);
        }
        catch {
            return value;
        }
    }
    async set(key, value, ttlSeconds) {
        const serialized = typeof value === 'string' ? value : JSON.stringify(value);
        await this.client.setex(key, ttlSeconds, serialized);
    }
    async delete(key) {
        await this.client.del(key);
    }
    async wrap(key, factory, ttlSeconds) {
        const cached = await this.get(key);
        if (cached !== null) {
            return cached;
        }
        const value = await factory();
        await this.set(key, value, ttlSeconds);
        return value;
    }
}
exports.RedisCacheService = RedisCacheService;
//# sourceMappingURL=redis-cache-service.js.map