import { getRedisClient } from './utils/redis'; import Redis from 'ioredis'; import { deleteKeysByPattern } from './utils'; export class RedisCache { // @ts-ignore client: Redis; prefix; constructor ({ prefix = 'scraper:redis_cache:', }: { prefix?: string }) { this.prefix = prefix; this.init(); } async init() { this.client = await getRedisClient(); } key(item) { const key = `${this.prefix}${item}`; return key; } async add(item) { const key = this.key(item); return this.client.set(key, 1); } async has(item) { const key = this.key(item); return this.client.get(key); } async clear() { return deleteKeysByPattern(this.client, `${this.prefix}*`); } }