Created
August 19, 2017 08:53
-
-
Save ofanidariyan/8a87cdf7660a10596314f5875caeb4f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
//Define all dependencies needed | |
const express = require('express'); | |
const responseTime = require('response-time') | |
const axios = require('axios'); | |
const redis = require('redis'); | |
const client = redis.createClient(); | |
//Load Express Framework | |
var app = express(); | |
//Create a middleware that adds a X-Response-Time header to responses. | |
app.use(responseTime()); | |
const getBook = (req, res) => { | |
let isbn = req.query.isbn; | |
let url = `https://www.googleapis.com/books/v1/volumes?q=isbn:${isbn}`; | |
return axios.get(url) | |
.then(response => { | |
let book = response.data.items; | |
// Set the string-key:isbn in our cache. With he contents of the cache : title | |
// Set cache expiration to 1 hour (60 minutes) | |
client.setex(isbn, 3600, JSON.stringify(book)); | |
res.send(book); | |
}) | |
.catch(err => { | |
res.send('The book you are looking for is not found !!!'); | |
}); | |
}; | |
const getCache = (req, res) => { | |
let isbn = req.query.isbn; | |
//Check the cache data from the server redis | |
client.get(isbn, (err, result) => { | |
if (result) { | |
res.send(result); | |
} else { | |
getBook(req, res); | |
} | |
}); | |
} | |
app.get('/book', getCache); | |
app.listen(3000, function() { | |
console.log('Your node is running on port 3000 !!!') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment