import Redis from 'ioredis' import type { CacheDriver } from '../types' export interface RedisDriverOptions { host: string port: number password?: string db?: number } export class RedisDriver implements CacheDriver { name = 'redis' private redis: Redis constructor(options: RedisDriverOptions) { this.redis = new Redis({ host: options.host, port: options.port, password: options.password, db: options.db ?? 0, lazyConnect: true, }) } private serialize(value: T): string { return JSON.stringify(value) } private deserialize(data: string): T { try { return JSON.parse(data) as T } catch { throw new Error(`[cache] Failed to deserialize data: ${data.substring(0, 100)}`) } } async get(key: string): Promise { try { const data = await this.redis.get(key) if (!data) return null return this.deserialize(data) } catch (err) { throw err } } async set(key: string, value: T, ttl = 0): Promise { try { const serialized = this.serialize(value) if (ttl > 0) { await this.redis.set(key, serialized, 'EX', ttl) } else { await this.redis.set(key, serialized) } } catch (err) { throw err } } async del(key: string): Promise { try { await this.redis.del(key) } catch (err) { throw err } } async exists(key: string): Promise { try { const result = await this.redis.exists(key) return result === 1 } catch (err) { throw err } } async clear(): Promise { try { await this.redis.flushdb() } catch (err) { throw err } } async disconnect(): Promise { await this.redis.quit() } }