Skip to content

Commit

Permalink
Merge pull request #14 from evan-kenzan/part1
Browse files Browse the repository at this point in the history
Now with puzzle caching and initial part 4 interactive stuff
  • Loading branch information
evan-kenzan authored Apr 14, 2017
2 parents a1af320 + b4c7e41 commit cd582c2
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 54 deletions.
33 changes: 28 additions & 5 deletions applications/crossword/common/models/crossword.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
'use strict';
var request = require('request');

var Etcd = require('node-etcd')

module.exports = function(Crossword) {

var etcd = new Etcd("http://example-etcd-cluster-client-service:2379");

Crossword.get = function(cb) {
Crossword.findOne(function(err, crossword) {

var etcdPuzzleResp = etcd.getSync("puzzle");

if (etcdPuzzleResp && !etcdPuzzleResp.err) {

console.log(`Responding with cache`);
fireHit();
if(err) handleError(err.message, cb);
cb(null, crossword);
});
var cachedPuzzle = JSON.parse(etcdPuzzleResp.body.node.value);
cachedPuzzle.fromCache = true;
cb(null, cachedPuzzle);
} else {
Crossword.findOne(function(err, crossword) {

fireHit();
if(err) {
handleError(err.message, cb);
} else {
var puzzleString = JSON.stringify(crossword);
etcd.setSync("puzzle", puzzleString, { ttl: 30 });
console.log(`Responding from Mongo`);
crossword.fromCache = false;
cb(null, crossword);
}
});
}
}

Crossword.put = function(words, cb) {
if(words) {
etcd.delSync("puzzle");
Crossword.findOne(function (err, crossword) {
fireHit();
if (err) handleError(err.message, cb);
Expand Down
1 change: 1 addition & 0 deletions applications/crossword/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^4.0.0",
"loopback-connector-mongodb": "^3.0.1",
"node-etcd": "^5.0.3",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^1.0.1",
"toastr": "^2.1.2"
Expand Down
2 changes: 1 addition & 1 deletion applications/crossword/server/boot/create-sample-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function(app) {
console.log("Found " + crosswords.length + " existing crosswords");
if(!crosswords.length) {
app.models.Crossword.create({
fromCache: true,
fromCache: false,
words: words,
}, function (err, crossword) {
if (err) throw err;
Expand Down
Loading

0 comments on commit cd582c2

Please sign in to comment.