diff --git a/README.md b/README.md index e91682b..b919dfd 100644 --- a/README.md +++ b/README.md @@ -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 ======= diff --git a/app.js b/app.js index fad9548..ba41364 100644 --- a/app.js +++ b/app.js @@ -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'); diff --git a/platform/db/index.js b/platform/db/index.js index 5091fab..f9de154 100644 --- a/platform/db/index.js +++ b/platform/db/index.js @@ -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; \ No newline at end of file +exports.sequelize = require('./sequelize/index'); +exports.redis = require('./redis'); \ No newline at end of file diff --git a/platform/db/redis.js b/platform/db/redis.js new file mode 100644 index 0000000..8d302a9 --- /dev/null +++ b/platform/db/redis.js @@ -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)); \ No newline at end of file diff --git a/platform/db/sequelize/index.js b/platform/db/sequelize/index.js new file mode 100644 index 0000000..eb10851 --- /dev/null +++ b/platform/db/sequelize/index.js @@ -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; \ No newline at end of file diff --git a/platform/db/models/post.js b/platform/db/sequelize/models/post.js similarity index 100% rename from platform/db/models/post.js rename to platform/db/sequelize/models/post.js diff --git a/platform/db/models/user.js b/platform/db/sequelize/models/user.js similarity index 100% rename from platform/db/models/user.js rename to platform/db/sequelize/models/user.js diff --git a/platform/users/usersPersistence.js b/platform/users/usersPersistence.js index d4028b1..452d684 100644 --- a/platform/users/usersPersistence.js +++ b/platform/users/usersPersistence.js @@ -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; } \ No newline at end of file