Add helper in LRU cache to get cache key
[e-mobility-charging-stations-simulator.git] / src / charging-station / SharedLRUCache.ts
1 import ChargingStationConfiguration from '../types/ChargingStationConfiguration';
2 import ChargingStationTemplate from '../types/ChargingStationTemplate';
3 import LRUCache from 'mnemonist/lru-map-with-delete';
4 import Utils from '../utils/Utils';
5
6 enum CacheType {
7 CHARGING_STATION_TEMPLATE = 'chargingStationTemplate',
8 CHARGING_STATION_CONFIGURATION = 'chargingStationConfiguration',
9 }
10
11 type CacheableType = ChargingStationTemplate | ChargingStationConfiguration;
12
13 export default class SharedLRUCache {
14 private static instance: SharedLRUCache | null = null;
15 private readonly lruCache: LRUCache<string, CacheableType>;
16
17 private constructor() {
18 this.lruCache = new LRUCache<string, CacheableType>(1000);
19 }
20
21 public static getInstance(): SharedLRUCache {
22 if (!SharedLRUCache.instance) {
23 SharedLRUCache.instance = new SharedLRUCache();
24 }
25 return SharedLRUCache.instance;
26 }
27
28 public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean {
29 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
30 }
31
32 public setChargingStationConfiguration(
33 chargingStationConfiguration: ChargingStationConfiguration
34 ): void {
35 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) {
36 this.set(
37 this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash),
38 chargingStationConfiguration
39 );
40 }
41 }
42
43 public getChargingStationConfiguration(
44 chargingStationConfigurationHash: string
45 ): ChargingStationConfiguration {
46 return this.get(
47 this.getChargingStationConfigurationKey(chargingStationConfigurationHash)
48 ) as ChargingStationConfiguration;
49 }
50
51 public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void {
52 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
53 }
54
55 public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean {
56 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash));
57 }
58
59 public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void {
60 this.set(
61 this.getChargingStationTemplateKey(chargingStationTemplate.templateHash),
62 chargingStationTemplate
63 );
64 }
65
66 public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate {
67 return this.get(
68 this.getChargingStationTemplateKey(chargingStationTemplateHash)
69 ) as ChargingStationTemplate;
70 }
71
72 public deleteChargingStationTemplate(chargingStationTemplateHash: string): void {
73 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash));
74 }
75
76 public clear(): void {
77 this.lruCache.clear();
78 }
79
80 private getChargingStationConfigurationKey(hash: string): string {
81 return CacheType.CHARGING_STATION_CONFIGURATION + hash;
82 }
83
84 private getChargingStationTemplateKey(hash: string): string {
85 return CacheType.CHARGING_STATION_TEMPLATE + hash;
86 }
87
88 private has(key: string): boolean {
89 return this.lruCache.has(key);
90 }
91
92 private get(key: string): CacheableType {
93 return this.lruCache.get(key);
94 }
95
96 private set(key: string, value: CacheableType): void {
97 this.lruCache.set(key, value);
98 }
99
100 private delete(key: string): void {
101 this.lruCache.delete(key);
102 }
103
104 private isChargingStationConfigurationCacheable(
105 chargingStationConfiguration: ChargingStationConfiguration
106 ): boolean {
107 return (
108 !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) &&
109 !Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) &&
110 !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) &&
111 !Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) &&
112 !Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) &&
113 !Utils.isEmptyString(chargingStationConfiguration?.configurationHash)
114 );
115 }
116 }