Skip to content

Commit

Permalink
Add redis for cache in users persistence.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Selden committed Jan 6, 2014
1 parent da27fef commit 0a77850
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Prerequisites

- node.js version with harmony generators support (v0.11.9 is tested).
- PostgreSQL running on port 5432 with no username/password -- database is named "blogs_db"
- Redis (used for cache) running on port 6379

Usage
=======
Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app.use(middleware.compress());
app.use(middleware.mount('/v1', router.middleware()));

co(function *(){
var connection = yield db.sequelize.sync();
var connection = yield db.sequelize.client.sync();
if(connection){
app.listen(3000);
console.log('connected to database and listening on port 3000');
Expand Down
30 changes: 2 additions & 28 deletions platform/db/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
var fs = require('fs'),
path = require('path'),
Sequelize = require('sequelize'),
sequelize = new Sequelize('blogs_db', null, null, { // TODO - configuration
dialect: "postgres",
port: 5432
}),
db = {};

// read all models and import them into the "db" object
fs
.readdirSync(__dirname + '/models')
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function (file) {
var model = sequelize.import(path.join(__dirname + '/models', file));
db[model.name] = model;
});

Object.keys(db).forEach(function (modelName) {
if (db[modelName].options.hasOwnProperty('associate')) {
db[modelName].options.associate(db);
}
});

db.sequelize = sequelize;
module.exports = db;
exports.sequelize = require('./sequelize/index');
exports.redis = require('./redis');
7 changes: 7 additions & 0 deletions platform/db/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var redis = require('redis');
var options = { // TODO -- fill with config
host: 'localhost',
port: 6379
};

module.exports = require('co-redis')(redis.createClient(options));
31 changes: 31 additions & 0 deletions platform/db/sequelize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var username = null;
var password = null;
var options = { // TODO -- pull from config
dialect: "postgres",
port: 5432
};
var client = new Sequelize('blogs_db', username, password, options);
var models = {};

// read all models and import them into the "db" object
fs
.readdirSync(__dirname + '/models')
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function (file) {
var model = client.import(path.join(__dirname + '/models', file));
models[model.name] = model;
});

Object.keys(models).forEach(function (modelName) {
if (models[modelName].options.hasOwnProperty('associate')) {
models[modelName].options.associate(models);
}
});

module.exports = models;
module.exports.client = client;
File renamed without changes.
File renamed without changes.
18 changes: 16 additions & 2 deletions platform/users/usersPersistence.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
var db = require('../db');

exports.getUser = function *(id){
return yield db.User.find(Number(id));
var id = Number(id);
var cachedUser = yield db.redis.get('/users/' + id);
if(cachedUser){
return JSON.parse(cachedUser);
}

var user = yield db.sequelize.User.find(Number(id));
if(user){
yield db.redis.set('/users/' + id, JSON.stringify(user));
}
return user;
};

exports.createUser = function *(name){
return yield db.User.create({ name: name });
var newUser = yield db.sequelize.User.create({ name: name });
if(newUser){
yield db.redis.set('/users/' + newUser.id, JSON.stringify(newUser));
}
return newUser;
}

0 comments on commit 0a77850

Please sign in to comment.