import Redis from 'ioredis'; import {REDIS_URL} from '../app/constants'; export const redis = new Redis(REDIS_URL); export const REDIS_ONE_MINUTE = 60; export const REDIS_DEFAULT_EXPIRY = REDIS_ONE_MINUTE / 60; /** * @param key The key for redis (use Keys#) * @param fn The function to return a resource. Can be a promise * @param seconds The amount of seconds to hold this resource in redis for. Defaults to 60 */ export async function wrapRedis(key: string, fn: () => Promise, seconds = REDIS_DEFAULT_EXPIRY): Promise { const cached = await redis.get(key); if (cached) { return JSON.parse(cached); } const recent = await fn(); if (recent) { await redis.set(key, JSON.stringify(recent), 'EX', seconds); } return recent; }