Last active
March 4, 2018 19:14
-
-
Save akhilesh26/32dbf046f2eae7453136a7c3c04ff868 to your computer and use it in GitHub Desktop.
Caching will be manage by using redis
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
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