Skip to content

Instantly share code, notes, and snippets.

@akhilesh26
Last active March 4, 2018 19:14
Show Gist options
  • Save akhilesh26/32dbf046f2eae7453136a7c3c04ff868 to your computer and use it in GitHub Desktop.
Save akhilesh26/32dbf046f2eae7453136a7c3c04ff868 to your computer and use it in GitHub Desktop.
Caching will be manage by using redis
const Router = require('koa-router');
const router = new Router();
var redis = require('redis');
var client = redis.createClient(6379, '127.0.0.1');
router.get('/search', async(ctx) => {
var query = ctx.request.query.q;
client.get(query, (error,queryResult) => {
if(error){through error}
if(queryResult){
ctx.responce.json(JSON.parse(queryResult));
}
else{
queryResult = await queries.search(query);
// store the key-value pair (query:queryResult) in our cache
// with an expiry of 1 hour (3600s)
client.setex(query,3600, queryResult);
// return the result to the user
ctx.responce.json(JSON.parse(queryResult));
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment