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