mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-22 15:24:55 +00:00
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import BaseModel from 'Common/Models/BaseModel';
|
|
import Dictionary from 'Common/Types/Dictionary';
|
|
import { JSONValue } from 'Common/Types/JSON';
|
|
|
|
export default abstract class LocalCache {
|
|
private static cache: Dictionary<JSONValue | BaseModel> = {};
|
|
|
|
public static setJSON(
|
|
namespace: string,
|
|
key: string,
|
|
value: JSONValue
|
|
): void {
|
|
this.cache[namespace + '.' + key] = value;
|
|
}
|
|
|
|
public static setString(
|
|
namespace: string,
|
|
key: string,
|
|
value: string
|
|
): void {
|
|
this.cache[namespace + '.' + key] = value;
|
|
}
|
|
|
|
public static setNumber(
|
|
namespace: string,
|
|
key: string,
|
|
value: number
|
|
): void {
|
|
this.cache[namespace + '.' + key] = value;
|
|
}
|
|
|
|
public static setModel(
|
|
namespace: string,
|
|
key: string,
|
|
value: BaseModel
|
|
): void {
|
|
this.cache[namespace + '.' + key] = value;
|
|
}
|
|
|
|
public static getModel<TBaseModel extends BaseModel>(
|
|
namespace: string,
|
|
key: string
|
|
): TBaseModel {
|
|
return this.cache[namespace + '.' + key] as TBaseModel;
|
|
}
|
|
|
|
public static getJSON(namespace: string, key: string): JSONValue {
|
|
return this.cache[namespace + '.' + key] as JSONValue;
|
|
}
|
|
|
|
public static getString(namespace: string, key: string): string {
|
|
return this.cache[namespace + '.' + key] as string;
|
|
}
|
|
|
|
public static getNumber(namespace: string, key: string): number {
|
|
return this.cache[namespace + '.' + key] as number;
|
|
}
|
|
|
|
public static async getOrSetString(
|
|
namespace: string,
|
|
key: string,
|
|
getStringFunction: () => Promise<string>
|
|
): Promise<string> {
|
|
if (!LocalCache.getString(namespace, key)) {
|
|
LocalCache.setString(namespace, key, await getStringFunction());
|
|
}
|
|
|
|
return LocalCache.getString(namespace, key);
|
|
}
|
|
|
|
public static hasValue(namespace: string, key: string): boolean {
|
|
return Boolean(this.cache[namespace + '.' + key]);
|
|
}
|
|
}
|