2018-09-01 13:58:06 -04:00
|
|
|
const MAIN_SCHEMA = '__MAIN_SCHEMA';
|
|
|
|
|
const SCHEMA_CACHE_PREFIX = '__SCHEMA';
|
2016-07-23 06:23:59 +02:00
|
|
|
|
|
|
|
|
import { randomString } from '../cryptoUtils';
|
2016-08-12 13:25:24 -04:00
|
|
|
import defaults from '../defaults';
|
2016-07-23 06:23:59 +02:00
|
|
|
|
|
|
|
|
export default class SchemaCache {
|
|
|
|
|
cache: Object;
|
|
|
|
|
|
2020-10-25 15:06:58 -05:00
|
|
|
constructor(cacheController, ttl = defaults.schemaCacheTTL, singleCache = false) {
|
2016-07-23 06:23:59 +02:00
|
|
|
this.ttl = ttl;
|
|
|
|
|
if (typeof ttl == 'string') {
|
|
|
|
|
this.ttl = parseInt(ttl);
|
|
|
|
|
}
|
|
|
|
|
this.cache = cacheController;
|
2016-11-02 23:05:23 +00:00
|
|
|
this.prefix = SCHEMA_CACHE_PREFIX;
|
2016-11-03 14:01:31 +00:00
|
|
|
if (!singleCache) {
|
2016-11-02 23:05:23 +00:00
|
|
|
this.prefix += randomString(20);
|
|
|
|
|
}
|
2016-07-23 06:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAllClasses() {
|
|
|
|
|
if (!this.ttl) {
|
|
|
|
|
return Promise.resolve(null);
|
|
|
|
|
}
|
2017-01-11 12:31:40 -08:00
|
|
|
return this.cache.get(this.prefix + MAIN_SCHEMA);
|
2016-07-23 06:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setAllClasses(schema) {
|
|
|
|
|
if (!this.ttl) {
|
|
|
|
|
return Promise.resolve(null);
|
|
|
|
|
}
|
2019-05-24 16:42:27 -05:00
|
|
|
return this.cache.put(this.prefix + MAIN_SCHEMA, schema);
|
2016-07-23 06:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getOneSchema(className) {
|
|
|
|
|
if (!this.ttl) {
|
|
|
|
|
return Promise.resolve(null);
|
|
|
|
|
}
|
2020-07-13 17:13:08 -05:00
|
|
|
return this.cache.get(this.prefix + MAIN_SCHEMA).then(cachedSchemas => {
|
2019-05-24 16:42:27 -05:00
|
|
|
cachedSchemas = cachedSchemas || [];
|
2020-07-13 17:13:08 -05:00
|
|
|
const schema = cachedSchemas.find(cachedSchema => {
|
2019-05-24 16:42:27 -05:00
|
|
|
return cachedSchema.className === className;
|
|
|
|
|
});
|
2016-10-23 16:59:39 +01:00
|
|
|
if (schema) {
|
|
|
|
|
return Promise.resolve(schema);
|
|
|
|
|
}
|
2019-05-24 16:42:27 -05:00
|
|
|
return Promise.resolve(null);
|
2016-10-23 16:59:39 +01:00
|
|
|
});
|
2016-07-23 06:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
2019-05-24 16:42:27 -05:00
|
|
|
return this.cache.del(this.prefix + MAIN_SCHEMA);
|
2016-07-23 06:23:59 +02:00
|
|
|
}
|
|
|
|
|
}
|