Skip to content

Commit

Permalink
redis library and product list cache added
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulekci committed Oct 19, 2017
1 parent e5ed663 commit 88c2df8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
1 change: 1 addition & 0 deletions .docker/redis/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bind 0.0.0.0
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ services:
image: redis:alpine
ports:
- "6379:6379"
command: ["redis-server", "/etc/redis/redis.conf"]
volumes:
- "./.docker/redis/redis.conf:/etc/redis/redis.conf"

logstash:
build: ./.docker/logstash
Expand Down
39 changes: 39 additions & 0 deletions libraries/redis-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var redis = require('redis');
client = redis.createClient({'host': 'redis'});

var RedisClient = {
push: function(key, value, callback) {
client.lpush(key, value, function(err, result) {
callback(err, result);
});
},

pop: function() {
client.brpop(key, value, function(err, result) {
callback(err, result);
});
},

set: function(key, value, ttl) {
if (value) {
client.set(key, JSON.stringify(value), 'EX', ttl);
}
},

get: function(key, callback) {
client.get(key, function(err, result) {
if (result) {
callback(err, JSON.parse(result));
return;
}
callback(true, {});
});
},

delete: function() {
client.delete(key);
}
}


module.exports = RedisClient;
30 changes: 30 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"async": "^2.5.0",
"blueimp-md5": "^2.10.0",
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
Expand All @@ -18,6 +19,7 @@
"mysql": "^2.14.1",
"node-datetime": "^2.0.3",
"nodemon": "^1.12.1",
"redis": "^2.8.0",
"serve-favicon": "~2.4.5",
"twig": "~0.10.3"
}
Expand Down
30 changes: 24 additions & 6 deletions routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ var productService = require('../services/productService');
var categoryService = require('../services/categoryService');
var waterfall = require('async/waterfall');
var productSearchService = require('../services/productSearchService');
var redisClient = require('../libraries/redis-client');
var md5 = require('blueimp-md5');

/* GET products listing. */
router.get('/', function(req, res, next) {
var queryParams = req.query
productService.getRecords(queryParams, function(err, results) {
if(err) { res.send(500,"Server Error"); return; }
// Respond with results as JSON
res.render('products', {'title': 'Products', products: results, 'totalCount': results.length});
});
var queryParams = req.query;
waterfall([
function(waterfallCallback) {
redisClient.get(md5(queryParams), function(err, results) {
if (err) { console.log(err); waterfallCallback(); return; }
if (results) {
res.render('products', {'title': 'Products', products: results, 'totalCount': results.length, 'cache': true});
return;
}
waterfallCallback();
});
},
function(waterfallCallback) {
productService.getRecords(queryParams, function(err, results) {
if(err) { res.send(500,"Server Error"); return; }
redisClient.set(md5(queryParams), results, 100);
// Respond with results as JSON
res.render('products', {'title': 'Products', products: results, 'totalCount': results.length, 'cache': false});

});
}
]);
});

/* GET products listing. */
Expand Down
2 changes: 1 addition & 1 deletion views/products.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block body %}
<a href="/product/new" class="float-right">New Product</a>
<h1>{{title}} <sup><small>(Total : {{totalCount}})</small></sup></h1>
<h1>{{title}} <sup><small>(Total : {{totalCount}} | Cache: {% if cache %}true{% else %}false{% endif %})</small></sup></h1>
<div>
<ul>
{% for product in products %}
Expand Down

0 comments on commit 88c2df8

Please sign in to comment.