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 + |
2f989136 | 22 | Bootstrap.getInstance().numberOfConfiguredChargingStations |
66a7748d | 23 | ) |
7c72977b JB |
24 | } |
25 | ||
66a7748d | 26 | public static getInstance (): SharedLRUCache { |
1ca780f9 | 27 | if (SharedLRUCache.instance === null) { |
66a7748d | 28 | SharedLRUCache.instance = new SharedLRUCache() |
7c72977b | 29 | } |
66a7748d | 30 | return SharedLRUCache.instance |
7c72977b JB |
31 | } |
32 | ||
66a7748d JB |
33 | public hasChargingStationConfiguration (chargingStationConfigurationHash: string): boolean { |
34 | return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) | |
7c72977b JB |
35 | } |
36 | ||
66a7748d JB |
37 | public setChargingStationConfiguration ( |
38 | chargingStationConfiguration: ChargingStationConfiguration | |
7c72977b | 39 | ): void { |
c14729f4 | 40 | if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { |
7c72977b | 41 | this.set( |
66a7748d | 42 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e1d9a0f4 | 43 | this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!), |
66a7748d JB |
44 | chargingStationConfiguration |
45 | ) | |
7c72977b JB |
46 | } |
47 | } | |
48 | ||
66a7748d JB |
49 | public getChargingStationConfiguration ( |
50 | chargingStationConfigurationHash: string | |
7c72977b JB |
51 | ): ChargingStationConfiguration { |
52 | return this.get( | |
66a7748d JB |
53 | this.getChargingStationConfigurationKey(chargingStationConfigurationHash) |
54 | ) as ChargingStationConfiguration | |
7c72977b JB |
55 | } |
56 | ||
66a7748d JB |
57 | public deleteChargingStationConfiguration (chargingStationConfigurationHash: string): void { |
58 | this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) | |
7c72977b JB |
59 | } |
60 | ||
66a7748d JB |
61 | public hasChargingStationTemplate (chargingStationTemplateHash: string): boolean { |
62 | return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)) | |
7c72977b JB |
63 | } |
64 | ||
66a7748d | 65 | public setChargingStationTemplate (chargingStationTemplate: ChargingStationTemplate): void { |
7c72977b | 66 | this.set( |
66a7748d | 67 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e1d9a0f4 | 68 | this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!), |
66a7748d JB |
69 | chargingStationTemplate |
70 | ) | |
7c72977b JB |
71 | } |
72 | ||
66a7748d | 73 | public getChargingStationTemplate (chargingStationTemplateHash: string): ChargingStationTemplate { |
7c72977b | 74 | return this.get( |
66a7748d JB |
75 | this.getChargingStationTemplateKey(chargingStationTemplateHash) |
76 | ) as ChargingStationTemplate | |
7c72977b JB |
77 | } |
78 | ||
66a7748d JB |
79 | public deleteChargingStationTemplate (chargingStationTemplateHash: string): void { |
80 | this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)) | |
7c72977b JB |
81 | } |
82 | ||
66a7748d JB |
83 | public clear (): void { |
84 | this.lruCache.clear() | |
7c72977b JB |
85 | } |
86 | ||
66a7748d JB |
87 | private getChargingStationConfigurationKey (hash: string): string { |
88 | return `${CacheType.chargingStationConfiguration}${hash}` | |
ec690502 JB |
89 | } |
90 | ||
66a7748d JB |
91 | private getChargingStationTemplateKey (hash: string): string { |
92 | return `${CacheType.chargingStationTemplate}${hash}` | |
ec690502 JB |
93 | } |
94 | ||
66a7748d JB |
95 | private has (key: string): boolean { |
96 | return this.lruCache.has(key) | |
7c72977b JB |
97 | } |
98 | ||
66a7748d JB |
99 | private get (key: string): CacheValueType | undefined { |
100 | return this.lruCache.get(key) | |
7c72977b JB |
101 | } |
102 | ||
66a7748d JB |
103 | private set (key: string, value: CacheValueType): void { |
104 | this.lruCache.set(key, value) | |
7c72977b JB |
105 | } |
106 | ||
66a7748d JB |
107 | private delete (key: string): void { |
108 | this.lruCache.delete(key) | |
7c72977b | 109 | } |
c14729f4 | 110 | |
66a7748d JB |
111 | private isChargingStationConfigurationCacheable ( |
112 | chargingStationConfiguration: ChargingStationConfiguration | |
c14729f4 JB |
113 | ): boolean { |
114 | return ( | |
5199f9fd JB |
115 | chargingStationConfiguration.configurationKey != null && |
116 | chargingStationConfiguration.stationInfo != null && | |
117 | chargingStationConfiguration.automaticTransactionGenerator != null && | |
118 | chargingStationConfiguration.configurationHash != null && | |
119 | isNotEmptyArray(chargingStationConfiguration.configurationKey) && | |
38ae4ce2 JB |
120 | !isEmpty(chargingStationConfiguration.stationInfo) && |
121 | !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) && | |
5199f9fd | 122 | isNotEmptyString(chargingStationConfiguration.configurationHash) |
66a7748d | 123 | ) |
c14729f4 | 124 | } |
7c72977b | 125 | } |