64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import MessageBus from 'src/sdk/MessageBus';
|
|
|
|
export default abstract class AbstractService {
|
|
|
|
protected static readonly messageBus: MessageBus = MessageBus.getInstance();
|
|
|
|
private static readonly CACHE_TTL = 60 * 60 * 1000; // 60 minutes cache TTL
|
|
|
|
protected constructor() { }
|
|
|
|
protected static setItem(key: string, processId: string, process: any): void {
|
|
const cache: Record<string, {process: any, timestamp: number}> =
|
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
|
|
|
cache[processId] = {
|
|
process: process, // we overwrite existing process
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
sessionStorage.setItem(key, JSON.stringify(cache));
|
|
}
|
|
|
|
protected static getItem(key: string, processId: string): any | null {
|
|
const cache: Record<string, {process: any, timestamp: number}> =
|
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
|
|
|
const item = cache[processId];
|
|
if (!item) {
|
|
return null;
|
|
}
|
|
|
|
const now: number = Date.now();
|
|
if ((now - item.timestamp) < this.CACHE_TTL) {
|
|
return { process: item.process, timestamp: item.timestamp };
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected static getItems(key: string): Record<string, any> {
|
|
const cache: Record<string, {process: any, timestamp: number}> =
|
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
|
const now: number = Date.now();
|
|
|
|
const validItems: Record<string, any> = {};
|
|
for (const [processId, item] of Object.entries(cache)) {
|
|
if (now - item.timestamp < this.CACHE_TTL) {
|
|
validItems[processId] = item.process;
|
|
}
|
|
}
|
|
|
|
return validItems;
|
|
}
|
|
|
|
protected static removeItem(key: string, processId: string): void {
|
|
const cache: Record<string, {process: any, timestamp: number}> =
|
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
|
|
|
delete cache[processId];
|
|
|
|
sessionStorage.setItem(key, JSON.stringify(cache));
|
|
}
|
|
}
|