{"version":3,"sources":["../../../../src/cache/types/redis.ts"],"sourcesContent":["import { Cluster, Redis, type RedisOptions } from 'ioredis'\n\nimport { EquippedError } from '../../errors'\nimport { Instance } from '../../instance'\nimport { Cache } from '../base'\nimport { type RedisConfig } from '../pipes'\n\nexport class RedisCache extends Cache {\n\tclient: Redis | Cluster\n\n\tconstructor(settings: RedisConfig, extraConfig?: Partial<RedisOptions>) {\n\t\tsuper()\n\t\tconst node = {\n\t\t\t...(settings.host ? { host: settings.host } : {}),\n\t\t\t...(settings.port ? { port: settings.port } : {}),\n\t\t}\n\t\tconst common = {\n\t\t\t...extraConfig,\n\t\t\t...(settings.password ? { password: settings.password } : {}),\n\t\t\t...(settings.username ? { username: settings.username } : {}),\n\t\t\t...(settings.tls ? { tls: {} } : {}),\n\t\t\tlazyConnect: true,\n\t\t}\n\t\tthis.client = settings.cluster\n\t\t\t? new Cluster([node], {\n\t\t\t\t\t...extraConfig,\n\t\t\t\t\tredisOptions: common,\n\t\t\t\t\tlazyConnect: true,\n\t\t\t\t})\n\t\t\t: new Redis({ ...common, ...node })\n\t\tthis.client.on('error', async (error) => {\n\t\t\tInstance.crash(new EquippedError(`Redis failed with error`, {}, error))\n\t\t})\n\t\tif (!extraConfig) Instance.on('start', async () => this.client.connect(), 1)\n\t\tInstance.on('close', async () => this.client.quit(), 1)\n\t}\n\n\tprivate getScopedKey(key: string): string {\n\t\treturn Instance.get().getScopedName(key, ':')\n\t}\n\n\tasync delete(key: string) {\n\t\tawait this.client.del(this.getScopedKey(key))\n\t}\n\n\tasync get(key: string) {\n\t\treturn await this.client.get(this.getScopedKey(key))\n\t}\n\n\tasync set(key: string, data: string, ttlInSecs?: number) {\n\t\tif (ttlInSecs) await this.client.setex(this.getScopedKey(key), ttlInSecs, data)\n\t\telse this.client.set(this.getScopedKey(key), data)\n\t}\n\n\tasync getOrSet<T>(key: string, fn: () => Promise<T>, ttlInSecs?: number) {\n\t\tconst cached = await this.get(this.getScopedKey(key))\n\t\tif (cached)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