1 import { LRUMapWithDelete
as LRUCache
} from
'mnemonist'
3 import { Bootstrap
} from
'./Bootstrap.js'
4 import type { ChargingStationConfiguration
, ChargingStationTemplate
} from
'../types/index.js'
10 } from
'../utils/index.js'
13 chargingStationTemplate
= 'chargingStationTemplate',
14 chargingStationConfiguration
= 'chargingStationConfiguration',
17 type CacheValueType
= ChargingStationTemplate
| ChargingStationConfiguration
19 export class SharedLRUCache
{
20 private static instance
: SharedLRUCache
| null = null
21 private readonly lruCache
: LRUCache
<string, CacheValueType
>
23 private constructor () {
24 this.lruCache
= new LRUCache
<string, CacheValueType
>(
25 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
26 Bootstrap
.getInstance().numberOfChargingStations
30 public static getInstance (): SharedLRUCache
{
31 if (SharedLRUCache
.instance
=== null) {
32 SharedLRUCache
.instance
= new SharedLRUCache()
34 return SharedLRUCache
.instance
37 public hasChargingStationConfiguration (chargingStationConfigurationHash
: string): boolean {
38 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
41 public setChargingStationConfiguration (
42 chargingStationConfiguration
: ChargingStationConfiguration
44 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
46 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
47 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
!),
48 chargingStationConfiguration
53 public getChargingStationConfiguration (
54 chargingStationConfigurationHash
: string
55 ): ChargingStationConfiguration
{
57 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
58 ) as ChargingStationConfiguration
61 public deleteChargingStationConfiguration (chargingStationConfigurationHash
: string): void {
62 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
65 public hasChargingStationTemplate (chargingStationTemplateHash
: string): boolean {
66 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
69 public setChargingStationTemplate (chargingStationTemplate
: ChargingStationTemplate
): void {
71 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
!),
73 chargingStationTemplate
77 public getChargingStationTemplate (chargingStationTemplateHash
: string): ChargingStationTemplate
{
79 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
80 ) as ChargingStationTemplate
83 public deleteChargingStationTemplate (chargingStationTemplateHash
: string): void {
84 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
87 public clear (): void {
91 private getChargingStationConfigurationKey (hash
: string): string {
92 return `${CacheType.chargingStationConfiguration}${hash}`
95 private getChargingStationTemplateKey (hash
: string): string {
96 return `${CacheType.chargingStationTemplate}${hash}`
99 private has (key
: string): boolean {
100 return this.lruCache
.has(key
)
103 private get (key
: string): CacheValueType
| undefined {
104 return this.lruCache
.get(key
)
107 private set (key
: string, value
: CacheValueType
): void {
108 this.lruCache
.set(key
, value
)
111 private delete (key
: string): void {
112 this.lruCache
.delete(key
)
115 private isChargingStationConfigurationCacheable (
116 chargingStationConfiguration
: ChargingStationConfiguration
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
)