1 import { LRUMapWithDelete
as LRUCache
} from
'mnemonist'
3 import { Bootstrap
} from
'./Bootstrap.js'
4 import type { ChargingStationConfiguration
, ChargingStationTemplate
} from
'../types/index.js'
5 import { isEmptyObject
, isNotEmptyArray
, isNotEmptyString
} from
'../utils/index.js'
8 chargingStationTemplate
= 'chargingStationTemplate',
9 chargingStationConfiguration
= 'chargingStationConfiguration'
12 type CacheValueType
= ChargingStationTemplate
| ChargingStationConfiguration
14 export class SharedLRUCache
{
15 private static instance
: SharedLRUCache
| null = null
16 private readonly lruCache
: LRUCache
<string, CacheValueType
>
18 private constructor () {
19 this.lruCache
= new LRUCache
<string, CacheValueType
>(
20 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
21 Bootstrap
.getInstance().numberOfConfiguredChargingStations
25 public static getInstance (): SharedLRUCache
{
26 if (SharedLRUCache
.instance
=== null) {
27 SharedLRUCache
.instance
= new SharedLRUCache()
29 return SharedLRUCache
.instance
32 public hasChargingStationConfiguration (chargingStationConfigurationHash
: string): boolean {
33 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
36 public setChargingStationConfiguration (
37 chargingStationConfiguration
: ChargingStationConfiguration
39 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
41 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
42 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
!),
43 chargingStationConfiguration
48 public getChargingStationConfiguration (
49 chargingStationConfigurationHash
: string
50 ): ChargingStationConfiguration
{
52 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
53 ) as ChargingStationConfiguration
56 public deleteChargingStationConfiguration (chargingStationConfigurationHash
: string): void {
57 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
60 public hasChargingStationTemplate (chargingStationTemplateHash
: string): boolean {
61 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
64 public setChargingStationTemplate (chargingStationTemplate
: ChargingStationTemplate
): void {
66 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
67 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
!),
68 chargingStationTemplate
72 public getChargingStationTemplate (chargingStationTemplateHash
: string): ChargingStationTemplate
{
74 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
75 ) as ChargingStationTemplate
78 public deleteChargingStationTemplate (chargingStationTemplateHash
: string): void {
79 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
82 public clear (): void {
86 private getChargingStationConfigurationKey (hash
: string): string {
87 return `${CacheType.chargingStationConfiguration}${hash}`
90 private getChargingStationTemplateKey (hash
: string): string {
91 return `${CacheType.chargingStationTemplate}${hash}`
94 private has (key
: string): boolean {
95 return this.lruCache
.has(key
)
98 private get (key
: string): CacheValueType
| undefined {
99 return this.lruCache
.get(key
)
102 private set (key
: string, value
: CacheValueType
): void {
103 this.lruCache
.set(key
, value
)
106 private delete (key
: string): void {
107 this.lruCache
.delete(key
)
110 private isChargingStationConfigurationCacheable (
111 chargingStationConfiguration
: ChargingStationConfiguration
114 chargingStationConfiguration
.configurationKey
!= null &&
115 chargingStationConfiguration
.stationInfo
!= null &&
116 chargingStationConfiguration
.automaticTransactionGenerator
!= null &&
117 chargingStationConfiguration
.configurationHash
!= null &&
118 isNotEmptyArray(chargingStationConfiguration
.configurationKey
) &&
119 !isEmptyObject(chargingStationConfiguration
.stationInfo
) &&
120 !isEmptyObject(chargingStationConfiguration
.automaticTransactionGenerator
) &&
121 isNotEmptyString(chargingStationConfiguration
.configurationHash
)