"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.Cache = void 0;
exports.resetCacheRedisConnection = resetCacheRedisConnection;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const lifecycle_1 = require("./lifecycle");
const redis_1 = require("./redis");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":6},{\"kind\":22,\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":5}]}},{\"kind\":2},{\"kind\":22,\"type\":{\"kind\":3}},{\"kind\":22,\"type\":{\"kind\":12,\"types\":[{\"kind\":2,\"typeName\":\"T\"},{\"kind\":5}]}},{\"kind\":2,\"typeName\":\"T\"},{\"kind\":2,\"typeName\":\"() =\\u003e RedisConnection\\u003cCacheRedisClient\\u003e\",\"typeName\":\"CacheRedisProvider\"}]]", []);
let defaultRedisConnection;
function getDefaultRedisConnection() {
    if (!defaultRedisConnection) {
        const connection = (0, redis_1.createRedis)('CACHE');
        (0, lifecycle_1.registerRedisStateReset)(connection.client, () => {
            if (defaultRedisConnection === connection)
                defaultRedisConnection = undefined;
        });
        defaultRedisConnection = connection;
    }
    return defaultRedisConnection;
}
function resetCacheRedisConnection() {
    defaultRedisConnection = undefined;
}
let _CacheMetadata_1;
class Cache {
    getRedis;
    constructor(getRedis = getDefaultRedisConnection) {
        this.getRedis = getRedis;
    }
    async get(key) {
        const { client, prefix } = this.getRedis();
        return client.get(cacheKey(prefix, key));
    }
    async set(key, value, ttl = 60) {
        const { client, prefix } = this.getRedis();
        await client.set(cacheKey(prefix, key), value, 'EX', ttl);
    }
    async getObj(key) {
        const value = await this.get(key);
        return value ? JSON.parse(value) : null;
    }
    async setObj(key, value, ttl = 60) {
        await this.set(key, JSON.stringify(value), ttl);
    }
    static get(key) {
        return defaultCache.get(key);
    }
    static set(key, value, ttl = 60) {
        return defaultCache.set(key, value, ttl);
    }
    static getObj(key) {
        return defaultCache.getObj(key);
    }
    static setObj(key, value, ttl = 60) {
        return defaultCache.setObj(key, value, ttl);
    }
    static __tsfType = _CacheMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"Cache\",\"typeName\":\"Cache\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"get\",\"parameters\":[{\"name\":\"key\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"set\",\"parameters\":[{\"name\":\"key\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false},{\"name\":\"value\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false},{\"name\":\"ttl\",\"type\":{\"$tsfType\":2},\"optional\":true,\"default\":true}],\"returnType\":{\"$tsfType\":3}},{\"name\":\"getObj\",\"parameters\":[{\"name\":\"key\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":4}},{\"name\":\"setObj\",\"parameters\":[{\"name\":\"key\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false},{\"name\":\"value\",\"type\":{\"$tsfType\":5},\"optional\":false,\"default\":false},{\"name\":\"ttl\",\"type\":{\"$tsfType\":2},\"optional\":true,\"default\":true}],\"returnType\":{\"$tsfType\":3}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"getRedis\",\"type\":{\"$tsfType\":6},\"optional\":true,\"default\":true}]}]", [() => Cache], __tsf_metadata_type);
}
exports.Cache = Cache;
_CacheMetadata_1.classType = Cache;
Cache.__tsfType = _CacheMetadata_1;
function cacheKey(prefix, key) {
    return `${prefix}:cache:${key}`;
}
const defaultCache = new Cache(getDefaultRedisConnection);
exports.__tsfTypeAliases = JSON.parse("[1,{\"CacheRedisClient\":{\"kind\":18,\"typeName\":\"CacheRedisClient\",\"types\":[]},\"CacheRedisProvider\":{\"kind\":2,\"typeName\":\"() =\\u003e RedisConnection\\u003cCacheRedisClient\\u003e\",\"typeName\":\"CacheRedisProvider\"}}]")[1];
//# sourceMappingURL=cache.js.map