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 {
29 return this.has(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash);
30 }
31
32 public setChargingStationConfiguration(
33 chargingStationConfiguration: ChargingStationConfiguration
34 ): void {
c14729f4 35 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) {
7c72977b
JB
36 this.set(
37 CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfiguration.configurationHash,
38 chargingStationConfiguration
39 );
40 }
41 }
42
43 public getChargingStationConfiguration(
44 chargingStationConfigurationHash: string
45 ): ChargingStationConfiguration {
46 return this.get(
47 CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash
48 ) as ChargingStationConfiguration;
49 }
50
51 public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void {
52 this.delete(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash);
53 }
54
55 public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean {
56 return this.has(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash);
57 }
58
59 public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void {
60 this.set(
61 CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplate.templateHash,
62 chargingStationTemplate
63 );
64 }
65
66 public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate {
67 return this.get(
68 CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash
69 ) as ChargingStationTemplate;
70 }
71
72 public deleteChargingStationTemplate(chargingStationTemplateHash: string): void {
73 this.delete(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash);
74 }
75
76 public clear(): void {
77 this.lruCache.clear();
78 }
79
80 private has(key: string): boolean {
81 return this.lruCache.has(key);
82 }
83
0e89dd9a 84 private get(key: string): CacheableType {
7c72977b
JB
85 return this.lruCache.get(key);
86 }
87
0e89dd9a 88 private set(key: string, value: CacheableType): void {
7c72977b
JB
89 this.lruCache.set(key, value);
90 }
91
92 private delete(key: string): void {
93 this.lruCache.delete(key);
94 }
c14729f4
JB
95
96 private isChargingStationConfigurationCacheable(
97 chargingStationConfiguration: ChargingStationConfiguration
98 ): boolean {
99 return (
100 !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) &&
101 !Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) &&
102 !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) &&
103 !Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) &&
104 !Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) &&
105 !Utils.isEmptyString(chargingStationConfiguration?.configurationHash)
106 );
107 }
7c72977b 108}