Cache gets (#3437)

* Cache gets

* Added test
This commit is contained in:
Andrew Henry 2020-10-08 20:30:23 -07:00 committed by GitHub
parent b637307de6
commit f3d593bc1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -47,6 +47,7 @@ define([
this.providers = {};
this.rootRegistry = new RootRegistry();
this.rootProvider = new RootObjectProvider.default(this.rootRegistry);
this.cache = {};
}
/**
@ -154,6 +155,11 @@ define([
* has been saved, or be rejected if it cannot be saved
*/
ObjectAPI.prototype.get = function (identifier) {
let keystring = this.makeKeyString(identifier);
if (this.cache[keystring] !== undefined) {
return this.cache[keystring];
}
identifier = utils.parseKeyString(identifier);
const provider = this.getProvider(identifier);
@ -165,7 +171,15 @@ define([
throw new Error('Provider does not support get!');
}
return provider.get(identifier);
let objectPromise = provider.get(identifier);
this.cache[keystring] = objectPromise;
return objectPromise.then(result => {
delete this.cache[keystring];
return result;
});
};
ObjectAPI.prototype.delete = function () {

View File

@ -30,9 +30,11 @@ describe("The Object API", () => {
beforeEach(() => {
mockProvider = jasmine.createSpyObj("mock provider", [
"create",
"update"
"update",
"get"
]);
mockProvider.create.and.returnValue(Promise.resolve(true));
mockProvider.get.and.returnValue(Promise.resolve(mockDomainObject));
objectAPI.addProvider(TEST_NAMESPACE, mockProvider);
});
it("Calls 'create' on provider if object is new", () => {
@ -57,6 +59,14 @@ describe("The Object API", () => {
expect(mockProvider.create).not.toHaveBeenCalled();
expect(mockProvider.update).not.toHaveBeenCalled();
});
it("Caches multiple requests for the same object", () => {
expect(mockProvider.get.calls.count()).toBe(0);
objectAPI.get(mockDomainObject.identifier);
expect(mockProvider.get.calls.count()).toBe(1);
objectAPI.get(mockDomainObject.identifier);
expect(mockProvider.get.calls.count()).toBe(1);
});
});
});
});