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