Skip to content

Commit d5600bb

Browse files
committed
Add caching via Redis
1 parent 2b10022 commit d5600bb

File tree

4 files changed

+129
-8
lines changed

4 files changed

+129
-8
lines changed

npm-shrinkwrap.json

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
},
99
"dependencies": {
1010
"numeral": "^2.0.0",
11-
"express": "^4.13.3"
11+
"express": "^4.13.3",
12+
"redis": "^2.8.0",
13+
"express-redis-cache": "^0.1.0"
1214
},
1315
"devDependencies": {
1416
"mocha": "^4.0.0",

service.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,55 @@ const HOST = '0.0.0.0';
88

99
const app = express();
1010

11+
const USE_CACHE = process.env.USE_CACHE;
12+
const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
13+
const REDIS_PORT = process.env.REDIS_PORT || 6379;
14+
const REDIS_PASSWORD = process.env.REDIS_PASSWORD;
15+
16+
function createCache() {
17+
if (USE_CACHE) {
18+
var client = require('redis').createClient(
19+
REDIS_PORT,
20+
REDIS_HOST,
21+
{
22+
connect_timeout: 500,
23+
});
24+
25+
client.on('error', function (err) {
26+
throw err;
27+
});
28+
29+
var cache = require('express-redis-cache')({
30+
client: client,
31+
expire: 60
32+
});
33+
34+
cache.on('message', function(message){
35+
console.log("cache", message);
36+
});
37+
38+
cache.on('error', function(error){
39+
console.error("cache", error);
40+
});
41+
42+
return cache;
43+
} else {
44+
return null;
45+
}
46+
}
47+
const cache = createCache();
48+
49+
function cacheRoute() {
50+
if (cache) {
51+
return cache.route();
52+
} else {
53+
// no-op
54+
return function(req, res, next) {
55+
next();
56+
}
57+
}
58+
}
59+
1160
function parseA(req) {
1261
return numeral(req.query.a).value();
1362
}
@@ -31,35 +80,40 @@ app.get('/', function (req, res) {
3180
res.sendFile(path.join(__dirname + '/index.html'));
3281
})
3382

34-
app.get('/add', function (req, res) {
83+
app.get('/add', cacheRoute(), function (req, res) {
3584
const a = parseA(req);
3685
const b = parseB(req);
3786
const value = calc.add(a, b);
3887
sendResult(value, res);
3988
})
4089

41-
app.get('/subtract', function (req, res) {
90+
app.get('/subtract', cacheRoute(), function (req, res) {
4291
const a = parseA(req);
4392
const b = parseB(req);
4493
const value = calc.subtract(a, b);
4594
sendResult(value, res);
4695
})
4796

48-
app.get('/multiply', function (req, res) {
97+
app.get('/multiply', cacheRoute(), function (req, res) {
4998
const a = parseA(req);
5099
const b = parseB(req);
51100
const value = calc.multiply(a, b);
52101
sendResult(value, res);
53102
})
54103

55-
app.get('/divide', function (req, res) {
104+
app.get('/divide', cacheRoute(), function (req, res) {
56105
const a = parseA(req);
57106
const b = parseB(req);
58107
const value = calc.divide(a, b);
59108
sendResult(value, res);
60109
})
61110

62111
if (!module.parent) {
63-
app.listen(PORT, HOST);
112+
var server = app.listen(PORT, HOST);
113+
server.on('close', function () {
114+
if (cache) {
115+
cache.client.end(true);
116+
}
117+
});
64118
}
65-
module.exports = app;
119+
module.exports = { app, cache };

test/service_spec.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@ const chai = require('chai');
44
const chaiHttp = require('chai-http');
55
const should = chai.should();
66
const expect = chai.expect;
7-
const app = require('../service');
7+
const service = require('../service');
8+
const app = service.app;
9+
const cache = service.cache;
810

911
chai.use(chaiHttp);
1012

1113
describe('Calculator service', () => {
1214

1315
var server;
1416

17+
it('it should connect to the redis cache', (done) => {
18+
if (cache) {
19+
cache.on("connected", function() {
20+
done();
21+
});
22+
23+
if (cache.client.connected) {
24+
done();
25+
}
26+
27+
cache.on("error", function (err) {
28+
assert.fail(err, null);
29+
});
30+
} else {
31+
done();
32+
}
33+
});
34+
1535
it('it should add two numbers', (done) => {
1636
server = app.listen();
1737
chai.request(server)
@@ -28,6 +48,9 @@ describe('Calculator service', () => {
2848

2949
after(function(done) {
3050
if (server) {
51+
if (cache && cache.client.connected) {
52+
cache.client.end(true);
53+
}
3154
server.close(done);
3255
}
3356
});

0 commit comments

Comments
 (0)