2020-04-04 12:10:53 +00:00
|
|
|
|
|
|
|
export /*abstract */class Cache {
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @return {boolean}
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */has(key) {
|
|
|
|
throw (new Error('not implemented'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @return {any}
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */fetch(key) {
|
|
|
|
throw (new Error('not implemented'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {any} value
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */store(key, value) {
|
|
|
|
throw (new Error('not implemented'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {()=>Promise<any>} retrieve
|
|
|
|
* @return {Promise<any>}
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*public */async get(key, retrieve) {
|
|
|
|
if (this.has(key)) {
|
|
|
|
const value = this.fetch(key);
|
|
|
|
return Promise.resolve(value);
|
|
|
|
} else {
|
|
|
|
const value = await retrieve();
|
|
|
|
this.store(key, value);
|
|
|
|
return Promise.resolve(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
class CacheNone extends Cache {
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*public */constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */has(key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */fetch(key) {
|
|
|
|
throw (new Error('not possible'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */store(key, value) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
export class CacheLocalstorage extends Cache {
|
|
|
|
/**
|
|
|
|
* @param {string} [corner] for separating the cache instance from others
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*public */constructor(corner = null) {
|
|
|
|
super();
|
|
|
|
this.corner = corner;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*private */augmentKey(key) {
|
|
|
|
return ((this.corner === null) ? key : (this.corner + '/' + key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */has(key) {
|
|
|
|
return (window.localStorage.getItem(this.augmentKey(key)) !== null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */fetch(key) {
|
|
|
|
const valueRaw = window.localStorage.getItem(this.augmentKey(key));
|
|
|
|
const value = JSON.parse(valueRaw);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-04 12:33:48 +00:00
|
|
|
* @author svartoyg
|
2020-04-04 12:10:53 +00:00
|
|
|
*/
|
|
|
|
/*protected */store(key, value) {
|
|
|
|
const valueRaw = JSON.stringify(value);
|
|
|
|
window.localStorage.setItem(this.augmentKey(key), valueRaw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|