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