Add option to re-use schema cache between requests (#2979)
* Add option to reuse database controller between requests. Clear schema cache when deleting everything * Add test * Rename setting to persistSchemaCache to more accurately reflect effect * Repurpose option to determine whether to randomize cache prefix. Restore Config.js controller creation. Add tests * Fix bug with missing parameter passed to to SchemaCache * Renaming and formatting * Fix property name typo * Rename option to avoid double negative and still be falsey by default. Style fix
This commit is contained in:
committed by
Florent Vilmart
parent
801308d9b7
commit
b347bff641
49
spec/EnableSingleSchemaCache.spec.js
Normal file
49
spec/EnableSingleSchemaCache.spec.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const auth = require('../src/Auth');
|
||||
const Config = require('../src/Config');
|
||||
const rest = require('../src/rest');
|
||||
|
||||
describe('Enable single schema cache', () => {
|
||||
beforeEach((done) => {
|
||||
reconfigureServer({
|
||||
enableSingleSchemaCache: true,
|
||||
schemaCacheTTL: 30000
|
||||
}).then(() => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can perform multiple create and query operations', (done) => {
|
||||
let config = fakeRequestForConfig();
|
||||
let nobody = auth.nobody(config);
|
||||
rest.create(config, nobody, 'Foo', {type: 1}).then(() => {
|
||||
config = fakeRequestForConfig();
|
||||
nobody = auth.nobody(config);
|
||||
return rest.create(config, nobody, 'Foo', {type: 2});
|
||||
}).then(() => {
|
||||
config = fakeRequestForConfig();
|
||||
nobody = auth.nobody(config);
|
||||
return rest.create(config, nobody, 'Bar');
|
||||
}).then(() => {
|
||||
config = fakeRequestForConfig();
|
||||
nobody = auth.nobody(config);
|
||||
return rest.find(config, nobody, 'Bar', {type: 1});
|
||||
}).then((response) => {
|
||||
fail('Should throw error');
|
||||
done();
|
||||
}, (error) => {
|
||||
config = fakeRequestForConfig();
|
||||
nobody = auth.nobody(config);
|
||||
expect(error).toBeDefined();
|
||||
return rest.find(config, nobody, 'Foo', {type: 1});
|
||||
}).then((response) => {
|
||||
config = fakeRequestForConfig();
|
||||
nobody = auth.nobody(config);
|
||||
expect(response.results.length).toEqual(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const fakeRequestForConfig = function() {
|
||||
return new Config('test');
|
||||
};
|
||||
@@ -3,35 +3,66 @@ var InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter')
|
||||
var SchemaCache = require('../src/Controllers/SchemaCache').default;
|
||||
|
||||
describe('SchemaCache', () => {
|
||||
var schemaCache;
|
||||
let cacheController;
|
||||
|
||||
beforeEach(() => {
|
||||
var cacheAdapter = new InMemoryCacheAdapter({});
|
||||
var cacheController = new CacheController(cacheAdapter, 'appId');
|
||||
schemaCache = new SchemaCache(cacheController);
|
||||
});
|
||||
beforeEach(() => {
|
||||
const cacheAdapter = new InMemoryCacheAdapter({});
|
||||
cacheController = new CacheController(cacheAdapter, 'appId');
|
||||
});
|
||||
|
||||
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('can retrieve a single schema after all schemas stored', (done) => {
|
||||
const schemaCache = new SchemaCache(cacheController);
|
||||
const allSchemas = [{
|
||||
className: 'Class1'
|
||||
}, {
|
||||
className: 'Class2'
|
||||
}];
|
||||
schemaCache.setAllClasses(allSchemas).then(() => {
|
||||
return 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();
|
||||
});
|
||||
});
|
||||
it('does not return all schemas after a single schema is stored', (done) => {
|
||||
const schemaCache = new SchemaCache(cacheController);
|
||||
const schema = {
|
||||
className: 'Class1'
|
||||
};
|
||||
schemaCache.setOneSchema(schema.className, schema).then(() => {
|
||||
return schemaCache.getAllClasses();
|
||||
}).then((allSchemas) => {
|
||||
expect(allSchemas).toBeNull();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('doesn\'t persist cached data by default', (done) => {
|
||||
const schemaCache = new SchemaCache(cacheController);
|
||||
const schema = {
|
||||
className: 'Class1'
|
||||
};
|
||||
schemaCache.setOneSchema(schema.className, schema).then(() => {
|
||||
const anotherSchemaCache = new SchemaCache(cacheController);
|
||||
return anotherSchemaCache.getOneSchema(schema.className).then((schema) => {
|
||||
expect(schema).toBeNull();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('can persist cached data', (done) => {
|
||||
const schemaCache = new SchemaCache(cacheController, 5000, true);
|
||||
const schema = {
|
||||
className: 'Class1'
|
||||
};
|
||||
schemaCache.setOneSchema(schema.className, schema).then(() => {
|
||||
const anotherSchemaCache = new SchemaCache(cacheController, 5000, true);
|
||||
return anotherSchemaCache.getOneSchema(schema.className).then((schema) => {
|
||||
expect(schema).not.toBeNull();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user