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