1 import { LRUMapWithDelete
as LRUCache
} from
'mnemonist'
2 import { isEmpty
} from
'rambda'
4 import type { ChargingStationConfiguration
, ChargingStationTemplate
} from
'../types/index.js'
5 import { isNotEmptyArray
, isNotEmptyString
} from
'../utils/index.js'
6 import { Bootstrap
} from
'./Bootstrap.js'
9 chargingStationTemplate
= 'chargingStationTemplate',
10 chargingStationConfiguration
= 'chargingStationConfiguration'
13 type CacheValueType
= ChargingStationTemplate
| ChargingStationConfiguration
15 export class SharedLRUCache
{
16 private static instance
: SharedLRUCache
| null = null
17 private readonly lruCache
: LRUCache
<string, CacheValueType
>
19 private constructor () {
20 this.lruCache
= new LRUCache
<string, CacheValueType
>(
21 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
22 Bootstrap
.getInstance().numberOfConfiguredChargingStations
26 public static getInstance (): SharedLRUCache
{
27 if (SharedLRUCache
.instance
=== null) {
28 SharedLRUCache
.instance
= new SharedLRUCache()
30 return SharedLRUCache
.instance
33 public hasChargingStationConfiguration (chargingStationConfigurationHash
: string): boolean {
34 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
37 public setChargingStationConfiguration (
38 chargingStationConfiguration
: ChargingStationConfiguration
40 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
42 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
!),
44 chargingStationConfiguration
49 public getChargingStationConfiguration (
50 chargingStationConfigurationHash
: string
51 ): ChargingStationConfiguration
{
53 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
54 ) as ChargingStationConfiguration
57 public deleteChargingStationConfiguration (chargingStationConfigurationHash
: string): void {
58 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
61 public hasChargingStationTemplate (chargingStationTemplateHash
: string): boolean {
62 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
65 public setChargingStationTemplate (chargingStationTemplate
: ChargingStationTemplate
): void {
67 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
68 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
!),
69 chargingStationTemplate
73 public getChargingStationTemplate (chargingStationTemplateHash
: string): ChargingStationTemplate
{
75 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
76 ) as ChargingStationTemplate
79 public deleteChargingStationTemplate (chargingStationTemplateHash
: string): void {
80 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
83 public clear (): void {
87 private getChargingStationConfigurationKey (hash
: string): string {
88 return `${CacheType.chargingStationConfiguration}${hash}`
91 private getChargingStationTemplateKey (hash
: string): string {
92 return `${CacheType.chargingStationTemplate}${hash}`
95 private has (key
: string): boolean {
96 return this.lruCache
.has(key
)
99 private get (key
: string): CacheValueType
| undefined {
100 return this.lruCache
.get(key
)
103 private set (key
: string, value
: CacheValueType
): void {
104 this.lruCache
.set(key
, value
)
107 private delete (key
: string): void {
108 this.lruCache
.delete(key
)
111 private isChargingStationConfigurationCacheable (
112 chargingStationConfiguration
: ChargingStationConfiguration
115 chargingStationConfiguration
.configurationKey
!= null &&
116 chargingStationConfiguration
.stationInfo
!= null &&
117 chargingStationConfiguration
.automaticTransactionGenerator
!= null &&
118 chargingStationConfiguration
.configurationHash
!= null &&
119 isNotEmptyArray(chargingStationConfiguration
.configurationKey
) &&
120 !isEmpty(chargingStationConfiguration
.stationInfo
) &&
121 !isEmpty(chargingStationConfiguration
.automaticTransactionGenerator
) &&
122 isNotEmptyString(chargingStationConfiguration
.configurationHash
)