Skip to content

Commit

Permalink
Add memcached
Browse files Browse the repository at this point in the history
  • Loading branch information
corradio committed Sep 15, 2016
1 parent 0f80a5a commit 6f8cf2b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"d3": "^4.2.3",
"express": "^4.10.2",
"mathjs": "^3.5.1",
"memcached": "^2.2.2",
"mongodb": "^2.2.9",
"node-statsd": "^0.1.1"
},
Expand Down
37 changes: 27 additions & 10 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var express = require('express');
var http = require('http');
var app = express();
var server = http.Server(app);
var Memcached = require('memcached');
var memcachedClient = new Memcached(process.env['MEMCACHED_HOST']);
var d3 = require('d3');
var statsd = require('node-statsd');
var MongoClient = require('mongodb').MongoClient;
Expand Down Expand Up @@ -145,16 +147,31 @@ app.get('/v1/solar', function(req, res) {
app.get('/v1/production', function(req, res) {
statsdClient.increment('v1_production_GET');
var t0 = new Date().getTime();
queryLastValues(function (err, result) {
if (err) {
statsdClient.increment('production_GET_ERROR');
console.error(err);
res.status(500).json({error: 'Unknown database error'});
} else {
obj = {}
result.forEach(function(d) { obj[d['_id']] = d.lastDocument; });
res.json({status: 'ok', data: obj});
statsdClient.timing('production_GET', new Date().getTime() - t0);
function returnObj(obj, cached) {
if (cached) statsdClient.increment('v1_production_GET_HIT_CACHE');
var deltaMs = new Date().getTime() - t0;
res.json({status: 'ok', data: obj, took: deltaMs + 'ms', cached: cached});
statsdClient.timing('production_GET', deltaMs);
}
memcachedClient.get('production', function (err, data) {
if (data) returnObj(data, true);
else {
if (false && value) { returnObj(obj, true) } else {
queryLastValues(function (err, result) {
if (err) {
statsdClient.increment('production_GET_ERROR');
console.error(err);
res.status(500).json({error: 'Unknown database error'});
} else {
obj = {}
result.forEach(function(d) { obj[d['_id']] = d.lastDocument; });
memcachedClient.set('production', obj, 5 * 60, function (err) {
console.error(err);
});
returnObj(obj, false);
}
});
};
}
});
});
Expand Down
9 changes: 7 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ services:
api:
build: api
command: ./node_modules/.bin/nodemon server.js
depends_on: [mongo]
environment: [ENV=development, 'MONGO_URL=mongodb://mongo:27017/electricity']
depends_on: [mongo, memcached]
environment:
- ENV=development
- MEMCACHED_HOST=memcached
- 'MONGO_URL=mongodb://mongo:27017/electricity'
ports: ['8000:8000']
volumes:
- './api/static:/home/static'
Expand All @@ -16,6 +19,8 @@ services:
environment: [ENV=development, 'MONGO_URL=mongodb://mongo:27017/electricity']
volumes:
- './api/data:/home/data'
memcached:
image: memcached
mongo:
image: mongo
volumes: ['./mongodata/:/data/db']
5 changes: 4 additions & 1 deletion production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ services:
build: api
environment:
- ENV=production
- MEMCACHED_HOST=memcached
- MONGO_URL=mongodb://mongo:27017/electricity
- STATSD_HOST=grafana-graphite-statsd.infrastructure_default
- VIRTUAL_HOST=electricitymap-api.tmrow.co
env_file: ./mailgun.env
depends_on: [mongo]
depends_on: [mongo, memcached]
networks: [default, infrastructure] # required to be able to com' with statsd & nginx
volumes: ['/home/shared/electricitymap/data:/home/data']
feeder:
Expand All @@ -21,6 +22,8 @@ services:
depends_on: [mongo]
networks: [default, infrastructure] # required to be able to com' with statsd
volumes: ['/home/shared/electricitymap/data:/home/data']
memcached:
image: memcached
mongo:
image: mongo
volumes: ['/home/shared/electricitymap/mongodata:/data/db']
Expand Down

0 comments on commit 6f8cf2b

Please sign in to comment.