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