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