37 lines
782 B
JavaScript
Executable File
37 lines
782 B
JavaScript
Executable File
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) {
|
|
let cached = await redisClient.pget(request);
|
|
if (cached) {
|
|
debug("Cached", 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
|
|
} |