Skip to content

Commit 151bea2

Browse files
steven-supersolidflovilmart
authored andcommitted
Try to retrieve schema from all schemas cache if not found in individual cache (#2912)
* Try to get schema from main schema if not found in single schema * Add newline * Add missing return * Add missing done to tests
1 parent af55cd1 commit 151bea2

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

spec/SchemaCache.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var CacheController = require('../src/Controllers/CacheController.js').default;
2+
var InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter').default;
3+
var SchemaCache = require('../src/Controllers/SchemaCache').default;
4+
5+
describe('SchemaCache', () => {
6+
var schemaCache;
7+
8+
beforeEach(() => {
9+
var cacheAdapter = new InMemoryCacheAdapter({});
10+
var cacheController = new CacheController(cacheAdapter, 'appId');
11+
schemaCache = new SchemaCache(cacheController);
12+
});
13+
14+
it('can retrieve a single schema after all schemas stored', (done) => {
15+
var allSchemas = [{
16+
className: 'Class1'
17+
}, {
18+
className: 'Class2'
19+
}];
20+
schemaCache.setAllClasses(allSchemas);
21+
schemaCache.getOneSchema('Class2').then((schema) => {
22+
expect(schema).not.toBeNull();
23+
done();
24+
});
25+
});
26+
27+
it('does not return all schemas after a single schema is stored', (done) => {
28+
var schema = {
29+
className: 'Class1'
30+
};
31+
schemaCache.setOneSchema('Class1', schema);
32+
schemaCache.getAllClasses().then((allSchemas) => {
33+
expect(allSchemas).toBeNull();
34+
done();
35+
});
36+
});
37+
});

src/Controllers/SchemaCache.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,21 @@ export default class SchemaCache {
5050
if (!this.ttl) {
5151
return Promise.resolve(null);
5252
}
53-
return this.cache.get(this.prefix+className);
53+
return this.cache.get(this.prefix+className).then((schema) => {
54+
if (schema) {
55+
return Promise.resolve(schema);
56+
}
57+
return this.cache.get(this.prefix+MAIN_SCHEMA).then((cachedSchemas) => {
58+
cachedSchemas = cachedSchemas || [];
59+
schema = cachedSchemas.find((cachedSchema) => {
60+
return cachedSchema.className === className;
61+
});
62+
if (schema) {
63+
return Promise.resolve(schema);
64+
}
65+
return Promise.resolve(null);
66+
});
67+
});
5468
}
5569

5670
clear() {

0 commit comments

Comments
 (0)