Fixed the fix and built

This commit is contained in:
abalabahaha
2015-12-11 21:22:29 -08:00
parent 6ed6932e24
commit cd4a70cfb2
7 changed files with 270 additions and 286 deletions

View File

@@ -14,28 +14,15 @@ var Cache = (function (_Array) {
_Array.call(this);
this.discrim = discrim || "id";
this.discrimCache = [];
this.highPerformance = false;
this.discrimCache = {};
}
Cache.prototype.setHighPerformance = function setHighPerformance() {
this.highPerformance = true;
};
Cache.prototype.setNormalPerformance = function setNormalPerformance() {
this.discrimCache = [];
this.highPerformance = false;
};
Cache.prototype.get = function get(key, value) {
var found = null;
this.forEach(function (val, index, array) {
if (val[key] == value) {
found = val;
return;
}
});
return found;
if (key === this.discrim) return this.discrimCache[value] || null;
var l = this.length;
for (var i = 0; i < l; i++) if (this[i][key] == value) return this[i];
return null;
};
Cache.prototype.has = function has(object) {
@@ -54,18 +41,16 @@ var Cache = (function (_Array) {
};
Cache.prototype.add = function add(data) {
var exit = false;
exit = ~this.discrimCache.indexOf(data[this.discrim]);
if (exit) {
return data;
} else {
if (this.limit && this.length >= this.limit) {
this.splice(0, 1);
}
this.push(data);
this.discrimCache.push(data[this.discrim]);
return data;
var cacheKey = this.discrim === "id" ? data.id : data[this.discrim];
if (this.discrimCache[cacheKey]) {
return this.discrimCache[cacheKey];
}
if (this.limit && this.length >= this.limit) {
this.splice(0, 1);
}
this.push(data);
this.discrimCache[cacheKey] = data;
return data;
};
Cache.prototype.update = function update(old, data) {
@@ -73,6 +58,7 @@ var Cache = (function (_Array) {
if (item) {
var index = this.indexOf(item);
this[index] = data;
this.discrimCache[data[this.discrim]] = data;
return this[index];
} else {
return false;
@@ -84,6 +70,7 @@ var Cache = (function (_Array) {
};
Cache.prototype.remove = function remove(data) {
delete this.discrimCache[data[this.discrim]];
var index = this.indexOf(data);
if (~index) {
this.splice(index, 1);