Skip to content

Commit

Permalink
Use redis for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Butz committed Dec 5, 2019
1 parent 0677ddf commit bda8c8f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ Node application that proxies requests to NASA's **A**stronomy **P**icture **O**

An API key for `api.nasa.gov` is required. A key can be generated in about 5 seconds at: https://api.nasa.gov/#signUp

Redis is running inside docker.

## Run

Start up redis using `docker-compose`:

```
docker-compose up
```

To run the server on port 3000:

```
NASA_API_KEY=<nasa-api-key-here> npm start
```

## Use

Hit the root endpoint with or without a date. Any request made with a date will have its response cached for 60 minutes.

```
curl https://localhost:3000/?date=2019-10-28
```
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const express = require('express');
const redis = require('redis');
const cookieParser = require('cookie-parser');
const logger = require('morgan');

const indexRouter = require('./routes/index');

const app = express();
const redisClient = redis.createClient();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use('/', indexRouter(process.env.NASA_API_KEY));
app.use('/', indexRouter(process.env.NASA_API_KEY, redisClient));

module.exports = app;
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.1"

services:
redis:
image: "redis:alpine"
ports:
- 6379:6379
2 changes: 1 addition & 1 deletion monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

while true; do
curl -s -o /dev/null -w "%{http_code}" http:https://apod-proxy.cfapps.io
curl -s -o /dev/null -w "%{http_code}" https:https://apod-proxy.apps.pcfone.io/
printf "\n"
sleep 1
done
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"morgan": "~1.9.1"
"morgan": "~1.9.1",
"redis": "^2.8.0"
},
"engines": {
"node": "13.1.0"
Expand Down
40 changes: 36 additions & 4 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,45 @@ function extractDate(date) {
return date;
}

function index(nasaApiKey) {
router.get('/', async function(req, res, next) {
function saveToCache(redisClient, key, value) {
redisClient.setex(key, 60 * 60, JSON.stringify(value));
}

function handleRequest(nasaApiKey, redisClient) {
return async (req, res, next) => {
const date = extractDate(req.query.date);
const desc = await client.getApodDesc(nasaApiKey, date);
const body = { desc };

await res.json(body);
saveToCache(redisClient, date, body);
}
}

function checkCache(redisClient) {
return (req, res, next) => {
const date = extractDate(req.query.date);

if (date === 'today') {
next();
return;
}

redisClient.get(date, (err, data) => {
if (data !== null) {
res.send(data);
} else {
next();
}
});
}
}

await res.json({ desc });
});
function index(nasaApiKey, redisClient) {
router.get('/',
checkCache(redisClient),
handleRequest(nasaApiKey, redisClient)
);

return router;
}
Expand Down

0 comments on commit bda8c8f

Please sign in to comment.