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
+
23 Bootstrap
.getInstance().numberOfProvisionedChargingStations
27 public static getInstance (): SharedLRUCache
{
28 if (SharedLRUCache
.instance
=== null) {
29 SharedLRUCache
.instance
= new SharedLRUCache()
31 return SharedLRUCache
.instance
34 public hasChargingStationConfiguration (chargingStationConfigurationHash
: string): boolean {
35 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
38 public setChargingStationConfiguration (
39 chargingStationConfiguration
: ChargingStationConfiguration
41 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
43 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
44 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
!),
45 chargingStationConfiguration
50 public getChargingStationConfiguration (
51 chargingStationConfigurationHash
: string
52 ): ChargingStationConfiguration
{
54 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
55 ) as ChargingStationConfiguration
58 public deleteChargingStationConfiguration (chargingStationConfigurationHash
: string): void {
59 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
62 public hasChargingStationTemplate (chargingStationTemplateHash
: string): boolean {
63 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
66 public setChargingStationTemplate (chargingStationTemplate
: ChargingStationTemplate
): void {
68 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
69 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
!),
70 chargingStationTemplate
74 public getChargingStationTemplate (chargingStationTemplateHash
: string): ChargingStationTemplate
{
76 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
77 ) as ChargingStationTemplate
80 public deleteChargingStationTemplate (chargingStationTemplateHash
: string): void {
81 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
84 public clear (): void {
88 private getChargingStationConfigurationKey (hash
: string): string {
89 return `${CacheType.chargingStationConfiguration}${hash}`
92 private getChargingStationTemplateKey (hash
: string): string {
93 return `${CacheType.chargingStationTemplate}${hash}`
96 private has (key
: string): boolean {
97 return this.lruCache
.has(key
)
100 private get (key
: string): CacheValueType
| undefined {
101 return this.lruCache
.get(key
)
104 private set (key
: string, value
: CacheValueType
): void {
105 this.lruCache
.set(key
, value
)
108 private delete (key
: string): void {
109 this.lruCache
.delete(key
)
112 private isChargingStationConfigurationCacheable (
113 chargingStationConfiguration
: ChargingStationConfiguration
116 chargingStationConfiguration
.configurationKey
!= null &&
117 chargingStationConfiguration
.stationInfo
!= null &&
118 chargingStationConfiguration
.automaticTransactionGenerator
!= null &&
119 chargingStationConfiguration
.configurationHash
!= null &&
120 isNotEmptyArray(chargingStationConfiguration
.configurationKey
) &&
121 !isEmpty(chargingStationConfiguration
.stationInfo
) &&
122 !isEmpty(chargingStationConfiguration
.automaticTransactionGenerator
) &&
123 isNotEmptyString(chargingStationConfiguration
.configurationHash
)