Skip to content

Commit

Permalink
Updated caching design
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jan 10, 2014
1 parent f5974ff commit a4f98c9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ MemoryCache.prototype.valid = function(conditions) {
return conditions && conditions._id;
};

MemoryCache.prototype.store = function(document, callback) {
MemoryCache.prototype.store = function(conditions, document, callback) {
// Conditions are null when storing on an insert, otherwise they represent the conditions
// that resulted in the object being retrieved from the database.
var id = JSON.stringify(document._id);
this.cache[id] = document;
callback();
Expand Down
6 changes: 3 additions & 3 deletions lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Instance.prototype.save = function(changes, callback) {
if(err) return onError(err);

this.__state.isNew = false;
this.__state.model.onRetrieved(created[0], callback || function() { }, (function(value) {
this.__state.model.onRetrieved(conditions, created[0], callback || function() { }, (function(value) {
this.__state.model.fromSource(value);
this.__state.original = _.cloneDeep(value);
this.__state.modified = _.cloneDeep(value);
Expand Down Expand Up @@ -113,7 +113,7 @@ Instance.prototype.save = function(changes, callback) {
this.__state.model.collection.findOne(conditions, (function(err, latest) {
if(err) return onError(err);

this.__state.model.onRetrieved(latest, callback || function() { }, (function(value) {
this.__state.model.onRetrieved(conditions, latest, callback || function() { }, (function(value) {
this.__state.model.fromSource(value);
this.__state.original = _.cloneDeep(value);
this.__state.modified = _.cloneDeep(value);
Expand Down Expand Up @@ -143,7 +143,7 @@ Instance.prototype.refresh = Instance.prototype.update = function(callback) {
this.__state.model.collection.findOne(conditions, (function(err, latest) {
if(err) return onError(err);

this.__state.model.onRetrieved(latest, callback || function() { }, (function(value) {
this.__state.model.onRetrieved(conditions, latest, callback || function() { }, (function(value) {
this.__state.model.fromSource(value);
this.__state.original = _.cloneDeep(value);
this.__state.modified = _.cloneDeep(value);
Expand Down
20 changes: 12 additions & 8 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,36 @@ Model.prototype.wrap = function (document, isNew) {
return new this.Instance(document, isNew);
};

Model.prototype.onRetrieved = function(results, callback, wrapper, options) {
Model.prototype.onRetrieved = function(conditions, results, callback, wrapper, options) {
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Object">The object retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Array" elementType="Object">The objects retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Object">The object retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///<param name="wrapper" type="Function">A function which converts the retrieved objects prior to submission</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Array" elementType="Object">The objects retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///<param name="wrapper" type="Function">A function which converts the retrieved objects prior to submission</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Array" elementType="Object">The objects retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///<param name="wrapper" type="Function">A function which converts the retrieved objects prior to submission</param>
Expand Down Expand Up @@ -243,7 +248,7 @@ Model.prototype.onRetrieved = function(results, callback, wrapper, options) {
doHook(this.options.hooks.ready, wrapped, (function(err) {
if(err) return done(err);
if(options.cache)
return this.cache.store(cacheDoc, function() {
return this.cache.store(conditions, cacheDoc, function() {
return done(null, wrapped);
});
else
Expand Down Expand Up @@ -348,8 +353,7 @@ Model.prototype.find = function (conditions, options, callback) {
conditions = conditions || {};
options = options || {};
_.defaults(options, {
wrap: true,
cache: true
wrap: true
});

var $ = this;
Expand All @@ -359,7 +363,7 @@ Model.prototype.find = function (conditions, options, callback) {
this.collection.find(conditions).toArray(function (err, results) {
if (err) return callback(err);
if (!results) return callback(null, null);
return $.onRetrieved(results, callback, options.wrap || function(value) { return value; });
return $.onRetrieved(conditions, results, callback, null, { wrap: options.wrap, cache: false });
});
};

Expand Down Expand Up @@ -424,14 +428,14 @@ Model.prototype.findOne = Model.prototype.get = function (conditions, options, c
if (err) return callback(err);
if (!results) return callback(null, null);

return this.onRetrieved(results, callback, null, { wrap: options.wrap, cache: options.cache });
return this.onRetrieved(conditions, results, callback, null, { wrap: options.wrap, cache: options.cache });
}).bind(this));
}).bind(this);

if(options.cache && this.cache && this.cache.valid(conditions))
this.cache.fetch(conditions, (function(err, doc) {
if(!err && doc)
return this.onRetrieved(doc, callback, null, { wrap: options.wrap, cache: false });
return this.onRetrieved(conditions, doc, callback, null, { wrap: options.wrap, cache: false });
else
return fromDB();
}).bind(this));
Expand Down Expand Up @@ -504,7 +508,7 @@ Model.prototype.insert = Model.prototype.create = function (object, options, cal
$.collection.insert(prepped, { w: callback ? 1 : 0 }, function(err, inserted) {
if(err) return end(err);
if(callback)
return $.onRetrieved(inserted, end, null, options);
return $.onRetrieved(null, inserted, end, null, { wrap: options.wrap, cache: options.cache });
return end();
});
};
Expand Down
3 changes: 2 additions & 1 deletion lib/caches/NoOpCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ NoOpCache.prototype.valid = function(conditions) {
/// <return type="Boolean"/>
};

NoOpCache.prototype.store = function(document, callback) {
NoOpCache.prototype.store = function(conditions, document, callback) {
/// <summary>Stores a document in the cache for future access</summary>
/// <param name="conditions" type="Object">The conditions that resulted in this object being stored, null for insertions</param>
/// <param name="document" type="Object">The database object to store in the cache</param>
/// <param name="callback" type="Function">A function which is called once the document has been stored</param>

Expand Down
2 changes: 1 addition & 1 deletion test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ EventEmitterCache.prototype.__proto__ = EventEmitter.prototype;
EventEmitterCache.prototype.valid = function(conditions) {
return conditions && conditions._id;
};
EventEmitterCache.prototype.store = function(document, callback) {
EventEmitterCache.prototype.store = function(conditions, document, callback) {
this.emit('store');
var id = JSON.stringify(document._id);
this.cache[id] = document;
Expand Down

0 comments on commit a4f98c9

Please sign in to comment.