Files
kami-parse-server/src/Controllers/CacheController.js
Florent Vilmart d83a0b6808 Use Prettier JS (#5017)
* Adds prettier

* Run lint before tests
2018-09-01 13:58:06 -04:00

75 lines
1.6 KiB
JavaScript

import AdaptableController from './AdaptableController';
import CacheAdapter from '../Adapters/Cache/CacheAdapter';
const KEY_SEPARATOR_CHAR = ':';
function joinKeys(...keys) {
return keys.join(KEY_SEPARATOR_CHAR);
}
/**
* Prefix all calls to the cache via a prefix string, useful when grouping Cache by object type.
*
* eg "Role" or "Session"
*/
export class SubCache {
constructor(prefix, cacheController, ttl) {
this.prefix = prefix;
this.cache = cacheController;
this.ttl = ttl;
}
get(key) {
const cacheKey = joinKeys(this.prefix, key);
return this.cache.get(cacheKey);
}
put(key, value, ttl) {
const cacheKey = joinKeys(this.prefix, key);
return this.cache.put(cacheKey, value, ttl);
}
del(key) {
const cacheKey = joinKeys(this.prefix, key);
return this.cache.del(cacheKey);
}
clear() {
return this.cache.clear();
}
}
export class CacheController extends AdaptableController {
constructor(adapter, appId, options = {}) {
super(adapter, appId, options);
this.role = new SubCache('role', this);
this.user = new SubCache('user', this);
}
get(key) {
const cacheKey = joinKeys(this.appId, key);
return this.adapter.get(cacheKey).then(null, () => Promise.resolve(null));
}
put(key, value, ttl) {
const cacheKey = joinKeys(this.appId, key);
return this.adapter.put(cacheKey, value, ttl);
}
del(key) {
const cacheKey = joinKeys(this.appId, key);
return this.adapter.del(cacheKey);
}
clear() {
return this.adapter.clear();
}
expectedAdapterType() {
return CacheAdapter;
}
}
export default CacheController;