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