Created
May 14, 2016 18:36
-
-
Save andreialecu/06cf2dd7c149bbe7ed775adfe4fb63c5 to your computer and use it in GitHub Desktop.
deployd redis caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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