32 lines
810 B
JavaScript
32 lines
810 B
JavaScript
require('dotenv').config();
|
|
const debug = require('debug')('soundbirder:cache');
|
|
const redis = require('redis');
|
|
const host = process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost';
|
|
const port = process.env.REDIS_PORT ? process.env.REDIS_PORT : 6379;
|
|
const url = `redis://${host}:${port}`;
|
|
const redisClient = redis.createClient({
|
|
url
|
|
});
|
|
|
|
(async () => {
|
|
redisClient.connect();
|
|
})();0
|
|
|
|
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;
|
|
}
|
|
|
|
module.exports = {
|
|
cacheResponse,
|
|
getCached
|
|
} |