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