From 151bea25ee4970420db1a2a42e0abd952af6fbce Mon Sep 17 00:00:00 2001 From: Steven Shipton Date: Sun, 23 Oct 2016 16:59:39 +0100 Subject: [PATCH] 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 --- spec/SchemaCache.spec.js | 37 ++++++++++++++++++++++++++++++++++ src/Controllers/SchemaCache.js | 16 ++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 spec/SchemaCache.spec.js diff --git a/spec/SchemaCache.spec.js b/spec/SchemaCache.spec.js new file mode 100644 index 00000000..12e9fb38 --- /dev/null +++ b/spec/SchemaCache.spec.js @@ -0,0 +1,37 @@ +var CacheController = require('../src/Controllers/CacheController.js').default; +var InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter').default; +var SchemaCache = require('../src/Controllers/SchemaCache').default; + +describe('SchemaCache', () => { + var schemaCache; + + beforeEach(() => { + var cacheAdapter = new InMemoryCacheAdapter({}); + var cacheController = new CacheController(cacheAdapter, 'appId'); + schemaCache = new SchemaCache(cacheController); + }); + + it('can retrieve a single schema after all schemas stored', (done) => { + var allSchemas = [{ + className: 'Class1' + }, { + className: 'Class2' + }]; + schemaCache.setAllClasses(allSchemas); + schemaCache.getOneSchema('Class2').then((schema) => { + expect(schema).not.toBeNull(); + done(); + }); + }); + + it('does not return all schemas after a single schema is stored', (done) => { + var schema = { + className: 'Class1' + }; + schemaCache.setOneSchema('Class1', schema); + schemaCache.getAllClasses().then((allSchemas) => { + expect(allSchemas).toBeNull(); + done(); + }); + }); +}); diff --git a/src/Controllers/SchemaCache.js b/src/Controllers/SchemaCache.js index a72fc021..63dc481d 100644 --- a/src/Controllers/SchemaCache.js +++ b/src/Controllers/SchemaCache.js @@ -50,7 +50,21 @@ export default class SchemaCache { if (!this.ttl) { return Promise.resolve(null); } - return this.cache.get(this.prefix+className); + return this.cache.get(this.prefix+className).then((schema) => { + if (schema) { + return Promise.resolve(schema); + } + return this.cache.get(this.prefix+MAIN_SCHEMA).then((cachedSchemas) => { + cachedSchemas = cachedSchemas || []; + schema = cachedSchemas.find((cachedSchema) => { + return cachedSchema.className === className; + }); + if (schema) { + return Promise.resolve(schema); + } + return Promise.resolve(null); + }); + }); } clear() {