-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add redis for cache in users persistence.
- Loading branch information
Paul Selden
committed
Jan 6, 2014
1 parent
da27fef
commit 0a77850
Showing
8 changed files
with
58 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |