UNPKG

@graphql-mesh/cache-upstash-redis

Version:
49 lines (48 loc) 1.43 kB
import { process } from '@graphql-mesh/cross-helpers'; import { Redis } from '@upstash/redis'; import { DisposableSymbols } from '@whatwg-node/disposablestack'; export default class UpstashRedisCache { constructor(config) { this.abortCtrl = new AbortController(); this.redis = new Redis({ url: process.env.UPSTASH_REDIS_REST_URL, token: process.env.UPSTASH_REDIS_REST_TOKEN, enableAutoPipelining: true, signal: this.abortCtrl.signal, latencyLogging: !!process.env.DEBUG, ...config, }); } get(key) { return this.redis.get(key); } set(key, value, options) { if (options?.ttl) { return this.redis.set(key, value, { px: options.ttl * 1000, }); } else { return this.redis.set(key, value); } } delete(key) { return this.redis.del(key).then(num => num > 0); } async getKeysByPrefix(prefix) { const keys = []; let cursor = '0'; do { const result = await this.redis.scan(cursor, { match: prefix + '*', count: 100, }); cursor = result[0]; keys.push(...result[1]); } while (cursor !== '0'); return keys; } [DisposableSymbols.dispose]() { return this.abortCtrl.abort(); } }