Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.

Commit

Permalink
fixed small bug in cache initialization cycle, added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
YashoSharma committed Nov 11, 2016
2 parents 4ccba90 + 2a39d89 commit 570af2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contribute? See the [contribution guidelines](/CONTRIBUTING.md).
## Setup

We use Node.js + Express in the application layer. The MySQL RDBMS is used as
our primary datastore in the persistence layer.
our primary datastore in the persistence layer. The Redis store is used as our primary cache.

#### Software Version Note

Expand Down Expand Up @@ -63,6 +63,9 @@ A list of configuration keys is provided below:
| DB_PASSWORD | Any string | Overrides default MySQL password ('') |
| DB_HOSTNAME | Any valid URI | Overrides default MySQL host ('127.0.0.1') |
| DB_PORT | Any valid port number | Overrides default MySQL port (3306) |
| REDIS_HOST | IP address | Overrides default redis host ('127.0.0.1') |
| REDIS_PORT | Any valid port number | Overrides default redis port (6379) |


Additionally, an [AWS shared credentials file](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html)
can be made available with configuration options for those systems under the profile
Expand Down Expand Up @@ -110,6 +113,8 @@ If you see any errors, such as an inability to access the database, make sure yo
set up the schema correctly and that you have set any necessary MySQL environment
variables listed in the configuration section above.

You should also have a local Redis instance which will be used as the cache.

Note that if you're looking to contribute to this codebase, you should read the
[database README](/database/README.md) as well. It contains important information that all
contributors should be familiar with.
Expand Down
3 changes: 2 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ database.instantiate()
return cache.instantiate();
})
.then(function () {
//api is initialized here which assures that all modules have access to an initialized datastore and cache instance
//api is initialized here which assures that all modules have access
//to an initialized datastore and cache instance
var api = require('./api/');
instance.use('/v1', api.v1);

Expand Down
36 changes: 19 additions & 17 deletions api/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ client.config.credentials = new client.SharedIniFileCredentials({ profile: confi
client.isEnabled = !!client.config.credentials.accessKeyId;
var _remote = new client.ElastiCache();

function _buildConfiguration () {
function _buildConfiguration() {
_REDIS_CONFIG = {};
if(!client.isEnabled) {
_REDIS_CONFIG.host = config.redis.host;
Expand All @@ -30,6 +30,21 @@ function _buildConfiguration () {
});
}

function _instantiate(cache) {
if(cache.connected) {
return _Promise.resolve(cache);
}

return new Promise(function(resolve, reject) {
cache.on("ready", function() {
resolve(cache);
});
cache.on("error", function(err) {
reject(err);
});
});
}

function CacheManager () {
this._cache = undefined;
}
Expand All @@ -45,26 +60,13 @@ CacheManager.prototype.instance = function() {

CacheManager.prototype.instantiate = function (){
if(!this._cache) {
_buildConfiguration().bind(this)
return _buildConfiguration().bind(this)
.then(function(config) {
this._cache = redis.createClient(config);
return _instantiate(this._cache);
});
}

var cache = this._cache;

if(cache.connected) {
return _Promise.resolve(cache);
}

return new Promise(function(resolve, reject) {
cache.on("ready", function() {
resolve(cache);
});
cache.on("error", function(err) {
reject(err);
});
});
return _instantiate(this._cache);
};

logger.info("connecting to redis");
Expand Down
2 changes: 1 addition & 1 deletion api/v1/errors/RedisError.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function RedisError(message, source) {
this.message = (message) ? message : DEFAULT_MESSAGE;
}

RedisError.prototype = Object.create(UnprocessableRequestError.prototype);
RedisError.prototype = Object.create(ApiError.prototype);
RedisError.prototype.constructor = RedisError;

module.exports = RedisError;

0 comments on commit 570af2a

Please sign in to comment.