1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-12-26 15:45:38 -05:00

Refactor get and set for Redis

This commit is contained in:
toast-ts 2024-06-23 18:23:54 +10:00
parent 72720359c0
commit 44768d9f59

View File

@ -17,17 +17,11 @@ export default class CacheServer {
.on('error', (err:ErrorReply)=>Logger.console('error', this.prefix, `Encountered an error in Redis: ${err.message}`)) .on('error', (err:ErrorReply)=>Logger.console('error', this.prefix, `Encountered an error in Redis: ${err.message}`))
} }
public static async get(key:any, jsonMode:boolean):Promise<any> { public static async get(key:any, jsonMode:boolean):Promise<any> {
let cachedResult:any; const cachedResult = jsonMode ? await RedisClient.json.get(key) : await RedisClient.get(key);
if (jsonMode) cachedResult = await RedisClient.json.get(key); return jsonMode ? cachedResult : cachedResult ? JSON.parse(String(cachedResult)) : null;
else {
cachedResult = await RedisClient.get(key);
if (cachedResult) cachedResult = JSON.parse(cachedResult);
}
return cachedResult;
} }
public static async set(key:any, value:any, jsonMode:boolean):Promise<any> { public static async set(key:any, value:any, jsonMode:boolean):Promise<any> {
if (jsonMode) return await RedisClient.json.set(key, '.', value); return jsonMode ? await RedisClient.json.set(key, '.', value) : await RedisClient.set(key, JSON.stringify(value));
else return await RedisClient.set(key, JSON.stringify(value));
} }
/** @param time Cache expiration in seconds */ /** @param time Cache expiration in seconds */
public static expiry = async(key:any, time:number)=>await RedisClient.expire(key, time); // NOTE: time is in seconds, not milliseconds -- you know what you did wrong public static expiry = async(key:any, time:number)=>await RedisClient.expire(key, time); // NOTE: time is in seconds, not milliseconds -- you know what you did wrong