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