soundbirder/controllers/cache.js

38 lines
833 B
JavaScript
Executable File

const { json } = require('express');
const { redisClient } = require('../redis');
const debug = require('debug')('soundbirder:cache');
function cacheResponse(request, response) {
debug("Caching response", request);
redisClient.set(request, JSON.stringify(response));
}
async function getCached(request) {
const cached = await redisClient.get(request);
if (cached) {
debug("Got cached response", request);
return JSON.parse(cached);
}
return null;
}
function push(key, value) {
redisClient.lpush(key, JSON.stringify(value));
}
async function pop(key) {
const cached = await redisClient.lpop(key);
debug("Pop cached", cached);
if (cached) {
return JSON.parse(cached);
}
return null;
}
module.exports = {
cacheResponse,
getCached,
push,
pop
}