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