Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 8x 8x 5x 8x 116x 18x 18x 18x 14x 18x 18x 18x 18x 12x 18x 116x 10x 10x 1x 1x 2x 2x 2x 2x 2x 1x 1x 9x 9x 15x 15x 12x 9x 9x 10x 116x 6x 6x 6x 5x 10x 2x 8x 5x 6x 116x 1x 1x 1x 116x 3x 3x 116x 116x 116x 116x 116x 116x 9x 9x 116x 116x 1x 1x 1x 115x 115x 115x 115x 115x 115x 1x 1x 1x 1x 1x 116x 79x 79x 116x 116x 116x 116x 1x 1x 1x 1x 1x 1x 1x 1x | import { createClient, createCluster, RedisClientOptions, RedisClientType, RedisClusterOptions, RedisClusterType, } from 'redis'; import '@redis/client'; import '@redis/bloom'; import '@redis/graph'; import '@redis/json'; import '@redis/search'; import '@redis/time-series'; import type { Cache, Store, Config } from 'cache-manager'; type Clients = RedisClientType | RedisClusterType; export type RedisCache<T extends Clients = RedisClientType> = Cache< RedisStore<T> >; type Name<T extends Clients> = T extends RedisClientType ? 'redis' : T extends RedisClusterType ? 'redis-cluster' : never; export interface RedisStore<T extends Clients = RedisClientType> extends Store { name: Name<T>; isCacheable: (value: unknown) => boolean; get client(): T; } export type CustomOptions = { keyPrefix?: string; } export class NoCacheableError implements Error { name = 'NoCacheableError'; constructor(public message: string) {} } export const avoidNoCacheable = async <T>(p: Promise<T>) => { try { return await p; } catch (e) { if (!(e instanceof NoCacheableError)) throw e; } }; const getVal = (value: unknown) => JSON.stringify(value) || '"undefined"'; const getFullKey = (originalKey: string, keyPrefix?: string) => `${keyPrefix? `${keyPrefix}:` : ''}${originalKey}`; function builder<T extends Clients>( redisCache: T, name: Name<T>, reset: () => Promise<void>, keys: (pattern: string) => Promise<string[]>, options?: Config & CustomOptions, ) { const isCacheable = options?.isCacheable || ((value) => value !== undefined && value !== null); return { async get<T>(key: string) { const val = await redisCache.get(getFullKey(key, options?.keyPrefix)); if (val === undefined || val === null) return undefined; else return JSON.parse(val) as T; }, async set(key, value, ttl) { if (!isCacheable(value)) throw new NoCacheableError(`"${value}" is not a cacheable value`); const t = ttl === undefined ? options?.ttl : ttl; if (t !== undefined && t !== 0) await redisCache.set(getFullKey(key, options?.keyPrefix), getVal(value), { PX: t }); else await redisCache.set(getFullKey(key, options?.keyPrefix), getVal(value)); }, async mset(args, ttl) { const t = ttl === undefined ? options?.ttl : ttl; if (t !== undefined && t !== 0) { const multi = redisCache.multi(); for (const [key, value] of args) { if (!isCacheable(value)) throw new NoCacheableError( `"${getVal(value)}" is not a cacheable value`, ); multi.set(getFullKey(key, options?.keyPrefix), getVal(value), { PX: t }); } await multi.exec(); } else await redisCache.mSet( args.map(([key, value]) => { if (!isCacheable(value)) throw new Error(`"${getVal(value)}" is not a cacheable value`); return [key, getVal(value)] as [string, string]; }), ); }, mget: (...args) => redisCache .mGet(args) .then((x) => x.map((x) => x === null || x === undefined ? undefined : (JSON.parse(x) as unknown), ), ), async mdel(...args) { let keys = args.map((key) => getFullKey(key, options?.keyPrefix)); await redisCache.del(args); }, async del(key) { await redisCache.del(getFullKey(key, options?.keyPrefix)); }, ttl: async (key) => redisCache.pTTL(key), keys: (pattern = '*') => keys(pattern), reset, isCacheable, name, get client() { return redisCache; }, } as RedisStore<T>; } // TODO: past instance as option export async function redisStore(options?: RedisClientOptions & Config & CustomOptions) { const redisCache = createClient(options); await redisCache.connect(); return redisInsStore(redisCache as RedisClientType, options); } /** * redisCache should be connected */ export function redisInsStore(redisCache: RedisClientType, options?: Config & CustomOptions) { const reset = async () => { await redisCache.flushDb(); }; const keys = (pattern: string) => redisCache.keys(pattern); return builder(redisCache, 'redis', reset, keys, options); } // TODO: coverage export async function redisClusterStore(options: RedisClusterOptions & Config) { const redisCache = createCluster(options); await redisCache.connect(); return redisClusterInsStore(redisCache, options); } // TODO: coverage /** * redisCache should be connected */ export function redisClusterInsStore( redisCache: RedisClusterType, options: RedisClusterOptions & Config, ) { const reset = async () => { await Promise.all( redisCache.getMasters().map(async (node) => { if (node.client) { const client = await node.client; await client.flushDb(); } }), ); }; const keys = async (pattern: string) => ( await Promise.all( redisCache.getMasters().map(async (node) => { if (node.client) { const client = await node.client; return await client.keys(pattern); } return []; }), ) ).flat(); return builder(redisCache, 'redis-cluster', reset, keys, options); } |