refactor: commands from menu
This commit is contained in:
24
lib/cache/cache.ts
vendored
24
lib/cache/cache.ts
vendored
@ -1,4 +1,5 @@
|
||||
import {
|
||||
Bulk,
|
||||
connect,
|
||||
Redis,
|
||||
RedisConnectOptions,
|
||||
@ -14,16 +15,31 @@ async function createCache<T>(): Promise<Map<string, T> | Redis> {
|
||||
const conf: RedisConnectOptions = {
|
||||
hostname: REDIS_HOST,
|
||||
port: REDIS_PORT || 6379,
|
||||
maxRetryCount: 2,
|
||||
};
|
||||
if (REDIS_PASS) {
|
||||
conf.password = REDIS_PASS;
|
||||
}
|
||||
const client = await connect(conf);
|
||||
console.log("[redis] connected");
|
||||
return client;
|
||||
try {
|
||||
const client = await connect(conf);
|
||||
console.log("[redis] connected");
|
||||
return client;
|
||||
} catch (_err) {
|
||||
console.log("[cache] cant connect to redis, falling back to mock");
|
||||
}
|
||||
}
|
||||
|
||||
return new Map<string, T>();
|
||||
const mockRedis = new Map<string, RedisValue>();
|
||||
|
||||
return {
|
||||
async set(key: string, value: RedisValue) {
|
||||
mockRedis.set(key, value);
|
||||
return value.toString();
|
||||
},
|
||||
async get(key: string) {
|
||||
return mockRedis.get(key) as Bulk;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const cache = await createCache();
|
||||
|
Reference in New Issue
Block a user