Skip to content

Instantly share code, notes, and snippets.

@andreialecu
Created May 14, 2016 18:36
Show Gist options
  • Save andreialecu/06cf2dd7c149bbe7ed775adfe4fb63c5 to your computer and use it in GitHub Desktop.
Save andreialecu/06cf2dd7c149bbe7ed775adfe4fb63c5 to your computer and use it in GitHub Desktop.
deployd redis caching
if(!ctx.server.redisClient) {
var redis = require('redis');
ctx.server.redisClient = redis.createClient(global.process.env.REDIS_URL);
}
if (!internal || !query.key) return;
//console.log('cache deleting', query.key);
ctx.server.redisClient.del(query.key, function(err) {
if (err) {
console.error(err);
}
setResult({ok: true});
});
if(!ctx.server.redisClient) {
var redis = require('redis');
ctx.server.redisClient = redis.createClient(global.process.env.REDIS_URL);
}
if (!internal || !query.key) return;
//console.log('cache getting', query.key);
$addCallback();
ctx.server.redisClient.get(query.key, function(err, value) {
if (err || !value) {
ctx.res.statusCode = 404;
$finishCallback();
return;
}
//console.log('got redis cache item for', query.key, value.length);
setResult(JSON.parse(value));
$finishCallback();
});
if(!ctx.server.redisClient) {
var redis = require('redis');
ctx.server.redisClient = redis.createClient(global.process.env.REDIS_URL);
}
if (!internal || !body.key) return;
console.log('cache setting', body.key);
$addCallback();
ctx.server.redisClient.set(body.key, JSON.stringify(body.data), function(err) {
if (err) {
console.error(err);
}
ctx.server.redisClient.expire(body.key, 60); // expire key in 60 seconds
setResult({ok: true});
$finishCallback();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment