disable cache

This commit is contained in:
Pete Richards 2016-11-08 11:20:34 -08:00
parent 2708562872
commit 42c48cb93b
3 changed files with 17 additions and 67 deletions

View File

@ -38,75 +38,25 @@ define(
this.modelService = modelService;
}
// Fast-resolving promise
function fastPromise(value) {
return (value || {}).then ? value : {
then: function (callback) {
return fastPromise(callback(value));
}
};
}
CachingModelDecorator.prototype.getModels = function (ids) {
var cacheService = this.cacheService,
neededIds = ids.filter(function notCached(id) {
return !cacheService.has(id);
});
var loadFromCache = ids.filter(function cached(id) {
return this.cacheService.has(id);
}, this),
loadFromService = ids.filter(function notCached(id) {
return !this.cacheService.has(id);
}, this);
// Update the cached instance of a model to a new value.
// We update in-place to ensure there is only ever one instance
// of any given model exposed by the modelService as a whole.
function updateModel(id, model) {
var oldModel = cacheService.get(id);
// Same object instance is a possibility, so don't copy
if (oldModel === model) {
return model;
}
// If we'd previously cached an undefined value, or are now
// seeing undefined, replace the item in the cache entirely.
if (oldModel === undefined || model === undefined) {
cacheService.put(id, model);
return model;
}
// Otherwise, empty out the old model...
Object.keys(oldModel).forEach(function (k) {
delete oldModel[k];
});
// ...and replace it with the contents of the new model.
Object.keys(model).forEach(function (k) {
oldModel[k] = model[k];
});
return oldModel;
if (!loadFromCache.length) {
return this.modelService.getModels(loadFromService);
}
// Store the provided models in our cache
function cacheAll(models) {
Object.keys(models).forEach(function (id) {
var model = cacheService.has(id) ?
updateModel(id, models[id]) : models[id];
cacheService.put(id, model);
});
}
// Expose the cache (for promise chaining)
function giveCache() {
return cacheService.all();
}
// Look up if we have unknown IDs
if (neededIds.length > 0) {
return this.modelService.getModels(neededIds)
.then(cacheAll)
.then(giveCache);
}
// Otherwise, just expose the cache directly
return fastPromise(cacheService.all());
return this.modelService.getModels(loadFromService)
.then(function (modelResults) {
loadFromCache.forEach(function (id) {
modelResults[id] = this.cacheService.get(id);
}, this);
return modelResults;
}.bind(this));
};
return CachingModelDecorator;

View File

@ -50,7 +50,7 @@ define(
return function (model, id) {
var capabilities = capabilityService.getCapabilities(model);
id = id || identifierService.generate();
cacheService.put(id, model);
// cacheService.put(id, model);
return new DomainObjectImpl(id, model, capabilities);
};
}

View File

@ -39,7 +39,7 @@ define(
model.id = id;
var capabilities = capabilityService.getCapabilities(model);
model.id = old_id;
cacheService.put(id, model);
// cacheService.put(id, model);
return new DomainObjectImpl(id, model, capabilities);
};
}