import { createClient } from 'redis';
export class RedisKVStore {
    client;
    constructor(url) {
        this.client = createClient({ url: url });
        this.client.on('error', (err) => {
            console.error(`Redis (kvstore) error: ${err}`);
        });
    }
    async connect() {
        return this.client.connect().then(() => { });
    }
    async get(key) {
        return this.client.get(key);
    }
    async set(key, value, canOverride = true, ttlInMs = 0) {
        const options = {};
        if (ttlInMs > 0) {
            options['PX'] = ttlInMs;
        }
        if (!canOverride) {
            options['NX'] = true;
        }
        const res = await this.client.set(key, value, options);
        if (res !== 'OK') {
            throw new Error(`Failed to set key: ${key}, value: ${value}, canOverride: ${canOverride}, ttlInMs: ${ttlInMs}`);
        }
    }
    async exists(key) {
        return (await this.client.exists(key)) > 0;
    }
    async delete(key) {
        await this.client.del(key);
    }
}
//# sourceMappingURL=RedisStore.js.map