[Add] Cache models on instantiation

...to remove need for Edit to persist these immediately, which in
turn causes #770 and #678
This commit is contained in:
Victor Woeltjen
2016-03-21 10:18:06 -07:00
parent ad4c456ca2
commit 17faf000b0
5 changed files with 86 additions and 12 deletions

View File

@@ -27,6 +27,7 @@ define([
"./src/models/StaticModelProvider", "./src/models/StaticModelProvider",
"./src/models/RootModelProvider", "./src/models/RootModelProvider",
"./src/models/ModelAggregator", "./src/models/ModelAggregator",
"./src/models/ModelCacheService",
"./src/models/PersistedModelProvider", "./src/models/PersistedModelProvider",
"./src/models/CachingModelDecorator", "./src/models/CachingModelDecorator",
"./src/models/MissingModelDecorator", "./src/models/MissingModelDecorator",
@@ -58,6 +59,7 @@ define([
StaticModelProvider, StaticModelProvider,
RootModelProvider, RootModelProvider,
ModelAggregator, ModelAggregator,
ModelCacheService,
PersistedModelProvider, PersistedModelProvider,
CachingModelDecorator, CachingModelDecorator,
MissingModelDecorator, MissingModelDecorator,
@@ -182,7 +184,10 @@ define([
{ {
"provides": "modelService", "provides": "modelService",
"type": "decorator", "type": "decorator",
"implementation": CachingModelDecorator "implementation": CachingModelDecorator,
"depends": [
"cacheService"
]
}, },
{ {
"provides": "modelService", "provides": "modelService",
@@ -319,6 +324,7 @@ define([
"key": "persistence", "key": "persistence",
"implementation": PersistenceCapability, "implementation": PersistenceCapability,
"depends": [ "depends": [
"cacheService",
"persistenceService", "persistenceService",
"identifierService", "identifierService",
"notificationService", "notificationService",
@@ -354,6 +360,10 @@ define([
} }
], ],
"services": [ "services": [
{
"key": "cacheService",
"implementation": ModelCacheService
},
{ {
"key": "now", "key": "now",
"implementation": Now "implementation": Now

View File

@@ -46,6 +46,7 @@ define(
* @implements {Capability} * @implements {Capability}
*/ */
function PersistenceCapability( function PersistenceCapability(
cacheService,
persistenceService, persistenceService,
identifierService, identifierService,
notificationService, notificationService,
@@ -56,6 +57,7 @@ define(
this.modified = domainObject.getModel().modified; this.modified = domainObject.getModel().modified;
this.domainObject = domainObject; this.domainObject = domainObject;
this.cacheService = cacheService;
this.identifierService = identifierService; this.identifierService = identifierService;
this.persistenceService = persistenceService; this.persistenceService = persistenceService;
this.notificationService = notificationService; this.notificationService = notificationService;
@@ -130,6 +132,7 @@ define(
domainObject = this.domainObject, domainObject = this.domainObject,
model = domainObject.getModel(), model = domainObject.getModel(),
modified = model.modified, modified = model.modified,
cacheService = this.cacheService,
persistenceService = this.persistenceService, persistenceService = this.persistenceService,
persistenceFn = model.persisted !== undefined ? persistenceFn = model.persisted !== undefined ?
this.persistenceService.updateObject : this.persistenceService.updateObject :
@@ -146,6 +149,9 @@ define(
getKey(domainObject.getId()), getKey(domainObject.getId()),
domainObject.getModel() domainObject.getModel()
]).then(function(result){ ]).then(function(result){
if (result) {
cacheService.remove(domainObject.getId());
}
return rejectIfFalsey(result, self.$q); return rejectIfFalsey(result, self.$q);
}).catch(function(error){ }).catch(function(error){
return notifyOnError(error, domainObject, self.notificationService, self.$q); return notifyOnError(error, domainObject, self.notificationService, self.$q);

View File

@@ -35,9 +35,8 @@ define(
* @param {ModelService} modelService this service to decorate * @param {ModelService} modelService this service to decorate
* @implements {ModelService} * @implements {ModelService}
*/ */
function CachingModelDecorator(modelService) { function CachingModelDecorator(cacheService, modelService) {
this.cache = {}; this.cacheService = cacheService;
this.cached = {};
this.modelService = modelService; this.modelService = modelService;
} }
@@ -51,10 +50,9 @@ define(
} }
CachingModelDecorator.prototype.getModels = function (ids) { CachingModelDecorator.prototype.getModels = function (ids) {
var cache = this.cache, var cacheService = this.cacheService,
cached = this.cached,
neededIds = ids.filter(function notCached(id) { neededIds = ids.filter(function notCached(id) {
return !cached[id]; return !cacheService.has(id);
}); });
// Update the cached instance of a model to a new value. // Update the cached instance of a model to a new value.
@@ -71,7 +69,7 @@ define(
// If we'd previously cached an undefined value, or are now // If we'd previously cached an undefined value, or are now
// seeing undefined, replace the item in the cache entirely. // seeing undefined, replace the item in the cache entirely.
if (oldModel === undefined || model === undefined) { if (oldModel === undefined || model === undefined) {
cache[id] = model; cacheService.put(id, model);
return model; return model;
} }
@@ -91,15 +89,15 @@ define(
// Store the provided models in our cache // Store the provided models in our cache
function cacheAll(models) { function cacheAll(models) {
Object.keys(models).forEach(function (id) { Object.keys(models).forEach(function (id) {
cache[id] = cached[id] ? var model = cacheService.has(id) ?
updateModel(id, models[id]) : models[id]; updateModel(id, models[id]) : models[id];
cached[id] = true; cacheService.put(id, model);
}); });
} }
// Expose the cache (for promise chaining) // Expose the cache (for promise chaining)
function giveCache() { function giveCache() {
return cache; return cacheService.all();
} }
// Look up if we have unknown IDs // Look up if we have unknown IDs

View File

@@ -0,0 +1,55 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define*/
define([], function () {
'use strict';
function ModelCacheService() {
this.cache = {};
this.cached = {};
}
ModelCacheService.prototype.put = function (id, model) {
this.cached[id] = true;
this.cache[id] = model;
};
ModelCacheService.prototype.get = function (id) {
return this.cache[id];
};
ModelCacheService.prototype.has = function (id) {
return this.cached[id];
};
ModelCacheService.prototype.remove = function (id) {
delete this.cached[id];
delete this.cache[id];
};
ModelCacheService.prototype.all = function () {
return this.cache;
};
return ModelCacheService;
});

View File

@@ -44,10 +44,15 @@ define(
* @param {IdentifierService} identifierService service to generate * @param {IdentifierService} identifierService service to generate
* new identifiers * new identifiers
*/ */
function Instantiate(capabilityService, identifierService) { function Instantiate(
capabilityService,
identifierService,
cacheService
) {
return function (model, id) { return function (model, id) {
var capabilities = capabilityService.getCapabilities(model); var capabilities = capabilityService.getCapabilities(model);
id = id || identifierService.generate(); id = id || identifierService.generate();
cacheService.put(id, model);
return new DomainObjectImpl(id, model, capabilities); return new DomainObjectImpl(id, model, capabilities);
}; };
} }