Commit | Line | Data |
---|---|---|
8114d10e JB |
1 | import LRUCache from 'mnemonist/lru-map-with-delete'; |
2 | ||
d1c99c59 | 3 | import { Bootstrap } from '../internal'; |
981ebfbe JB |
4 | import type { ChargingStationConfiguration } from '../types/ChargingStationConfiguration'; |
5 | import type { ChargingStationTemplate } from '../types/ChargingStationTemplate'; | |
c14729f4 | 6 | import Utils from '../utils/Utils'; |
7c72977b JB |
7 | |
8 | enum CacheType { | |
9 | CHARGING_STATION_TEMPLATE = 'chargingStationTemplate', | |
10 | CHARGING_STATION_CONFIGURATION = 'chargingStationConfiguration', | |
11 | } | |
12 | ||
0e89dd9a JB |
13 | type CacheableType = ChargingStationTemplate | ChargingStationConfiguration; |
14 | ||
57adbebc JB |
15 | export default class SharedLRUCache { |
16 | private static instance: SharedLRUCache | null = null; | |
0e89dd9a | 17 | private readonly lruCache: LRUCache<string, CacheableType>; |
7c72977b JB |
18 | |
19 | private constructor() { | |
d1c99c59 | 20 | this.lruCache = new LRUCache<string, CacheableType>( |
c3d8778e JB |
21 | Bootstrap.getInstance().numberOfChargingStationTemplates + |
22 | Bootstrap.getInstance().numberOfChargingStations | |
d1c99c59 | 23 | ); |
7c72977b JB |
24 | } |
25 | ||
57adbebc | 26 | public static getInstance(): SharedLRUCache { |
1ca780f9 | 27 | if (SharedLRUCache.instance === null) { |
57adbebc | 28 | SharedLRUCache.instance = new SharedLRUCache(); |
7c72977b | 29 | } |
57adbebc | 30 | return SharedLRUCache.instance; |
7c72977b JB |
31 | } |
32 | ||
33 | public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean { | |
ec690502 | 34 | return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)); |
7c72977b JB |
35 | } |
36 | ||
37 | public setChargingStationConfiguration( | |
38 | chargingStationConfiguration: ChargingStationConfiguration | |
39 | ): void { | |
c14729f4 | 40 | if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { |
7c72977b | 41 | this.set( |
ec690502 | 42 | this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash), |
7c72977b JB |
43 | chargingStationConfiguration |
44 | ); | |
45 | } | |
46 | } | |
47 | ||
48 | public getChargingStationConfiguration( | |
49 | chargingStationConfigurationHash: string | |
50 | ): ChargingStationConfiguration { | |
51 | return this.get( | |
ec690502 | 52 | this.getChargingStationConfigurationKey(chargingStationConfigurationHash) |
7c72977b JB |
53 | ) as ChargingStationConfiguration; |
54 | } | |
55 | ||
56 | public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void { | |
ec690502 | 57 | this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)); |
7c72977b JB |
58 | } |
59 | ||
60 | public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean { | |
ec690502 | 61 | return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)); |
7c72977b JB |
62 | } |
63 | ||
64 | public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void { | |
65 | this.set( | |
ec690502 | 66 | this.getChargingStationTemplateKey(chargingStationTemplate.templateHash), |
7c72977b JB |
67 | chargingStationTemplate |
68 | ); | |
69 | } | |
70 | ||
71 | public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate { | |
72 | return this.get( | |
ec690502 | 73 | this.getChargingStationTemplateKey(chargingStationTemplateHash) |
7c72977b JB |
74 | ) as ChargingStationTemplate; |
75 | } | |
76 | ||
77 | public deleteChargingStationTemplate(chargingStationTemplateHash: string): void { | |
ec690502 | 78 | this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)); |
7c72977b JB |
79 | } |
80 | ||
81 | public clear(): void { | |
82 | this.lruCache.clear(); | |
83 | } | |
84 | ||
ec690502 | 85 | private getChargingStationConfigurationKey(hash: string): string { |
14ecae6a | 86 | return `${CacheType.CHARGING_STATION_CONFIGURATION}${hash}`; |
ec690502 JB |
87 | } |
88 | ||
89 | private getChargingStationTemplateKey(hash: string): string { | |
14ecae6a | 90 | return `${CacheType.CHARGING_STATION_TEMPLATE}${hash}`; |
ec690502 JB |
91 | } |
92 | ||
7c72977b JB |
93 | private has(key: string): boolean { |
94 | return this.lruCache.has(key); | |
95 | } | |
96 | ||
1895299d | 97 | private get(key: string): CacheableType | undefined { |
7c72977b JB |
98 | return this.lruCache.get(key); |
99 | } | |
100 | ||
0e89dd9a | 101 | private set(key: string, value: CacheableType): void { |
7c72977b JB |
102 | this.lruCache.set(key, value); |
103 | } | |
104 | ||
105 | private delete(key: string): void { | |
106 | this.lruCache.delete(key); | |
107 | } | |
c14729f4 JB |
108 | |
109 | private isChargingStationConfigurationCacheable( | |
110 | chargingStationConfiguration: ChargingStationConfiguration | |
111 | ): boolean { | |
112 | return ( | |
a56d48e7 JB |
113 | Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false && |
114 | Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false && | |
115 | Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false && | |
53ac516c | 116 | Utils.isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true && |
a56d48e7 | 117 | Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false && |
5a2a53cf | 118 | Utils.isNotEmptyString(chargingStationConfiguration?.configurationHash) === true |
c14729f4 JB |
119 | ); |
120 | } | |
7c72977b | 121 | } |