From 46b7747c87ab12c0b690de07c6be5f447499eca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Drouyer?= Date: Thu, 1 Nov 2012 01:13:16 +0100 Subject: [PATCH] fixed and enhanced caching in getters and setters in abstract-class --- lib/abstract-class.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 65392c4b..ecd39cf4 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -729,17 +729,23 @@ AbstractClass.belongsTo = function (anotherClass, params) { }.bind(this)); }; - this.prototype[methodName] = function (p) { + this.prototype[methodName] = function (refresh, p) { + if (arguments.length === 1) { + p = refresh; + refresh = false; + } else if (arguments.length > 2) { + throw new Error('Method only can\'t be only called with more than two arguments'); + } var self = this; - var cachedValue = null; - if (this.__cachedRelations && this.__cachedRelations[methodName]) { + var cachedValue; + if (!refresh && this.__cachedRelations && (typeof this.__cachedRelations[methodName] !== 'undefined')) { cachedValue = this.__cachedRelations[methodName]; } if (p instanceof AbstractClass) { // acts as setter this[fk] = p.id; this.__cachedRelations[methodName] = p; } else if (typeof p === 'function') { // acts as async getter - if (cachedValue === null) { + if (typeof cachedValue === 'undefined') { this.__finders__[methodName](this[fk], function(err, inst) { if (!err) { self.__cachedRelations[methodName] = inst; @@ -752,10 +758,10 @@ AbstractClass.belongsTo = function (anotherClass, params) { return cachedValue; } } else if (typeof p === 'undefined') { // acts as sync getter - return cachedValue || this[fk]; + return this[fk]; } else { // setter this[fk] = p; - this.__cachedRelations[methodName] = p; + delete this.__cachedRelations[methodName]; } }; @@ -793,6 +799,7 @@ function defineScope(cls, targetClass, name, params, methods) { var f = function caller(condOrRefresh, cb) { var actualCond = {}; var actualRefresh = false; + var saveOnCache = true; if (arguments.length === 1) { cb = condOrRefresh; } else if (arguments.length === 2) { @@ -800,23 +807,23 @@ function defineScope(cls, targetClass, name, params, methods) { actualRefresh = condOrRefresh; } else { actualCond = condOrRefresh; + actualRefresh = true; + saveOnCache = false; } - } else if (arguments.length > 2) { - throw new Error('Method only can\'t be called with more than two arguments'); + } else { + throw new Error('Method only can be only called with one or two arguments'); } - if (!this.__cachedRelations || !this.__cachedRelations[name] || actualRefresh) { + if (!this.__cachedRelations || (typeof this.__cachedRelations[name] == 'undefined') || actualRefresh) { var self = this; return targetClass.all(mergeParams(actualCond, caller._scope), function(err, data) { - self.__cachedRelations[name] = data; + if (!err && saveOnCache) { + self.__cachedRelations[name] = data; + } cb(err, data); }); } else { - if (cb) { - cb(null, this.__cachedRelations); - } else { - return this.__cachedRelations[name]; - } + cb(null, this.__cachedRelations[name]); } }; f._scope = typeof params === 'function' ? params.call(this) : params;