// LOCKED: TRUE
// All content in this file is locked and cannot be edited.

import { createClient, RedisClientType } from 'redis';
import { CacheManager } from '../types/cache';
import { ProviderResponse } from '../types/chat';

export class RedisCacheManager implements CacheManager {
  private client: RedisClientType;

  constructor(redisUrl: string) {
    this.client = createClient({ url: redisUrl });
    this.client.connect().catch(console.error);
  }

  async get(key: string): Promise<ProviderResponse | null> {
    const value = await this.client.get(key);
    return value ? JSON.parse(value) : null;
  }

  async set(key: string, value: ProviderResponse, ttl: number): Promise<void> {
    await this.client.setEx(key, ttl, JSON.stringify(value));
  }

  async delete(key: string): Promise<void> {
    await this.client.del(key);
  }

  async clear(): Promise<void> {
    await this.client.flushDb();
  }

  async close(): Promise<void> {
    await this.client.quit();
  }
}