1 import { LRUMapWithDelete
as LRUCache
} from
'mnemonist'
2 import { isEmpty
} from
'rambda'
4 import type { ChargingStationConfiguration
, ChargingStationTemplate
} from
'../types/index.js'
6 import { isNotEmptyArray
, isNotEmptyString
} from
'../utils/index.js'
7 import { Bootstrap
} from
'./Bootstrap.js'
10 chargingStationConfiguration
= 'chargingStationConfiguration',
11 chargingStationTemplate
= 'chargingStationTemplate',
14 type CacheValueType
= ChargingStationConfiguration
| ChargingStationTemplate
16 export class SharedLRUCache
{
17 private static instance
: null | SharedLRUCache
= null
18 private readonly lruCache
: LRUCache
<string, CacheValueType
>
20 private constructor () {
21 this.lruCache
= new LRUCache
<string, CacheValueType
>(
22 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
23 Bootstrap
.getInstance().numberOfConfiguredChargingStations
+
24 Bootstrap
.getInstance().numberOfProvisionedChargingStations
28 public static getInstance (): SharedLRUCache
{
29 if (SharedLRUCache
.instance
=== null) {
30 SharedLRUCache
.instance
= new SharedLRUCache()
32 return SharedLRUCache
.instance
35 private delete (key
: string): void {
36 this.lruCache
.delete(key
)
39 private get (key
: string): CacheValueType
| undefined {
40 return this.lruCache
.get(key
)
43 private getChargingStationConfigurationKey (hash
: string): string {
44 return `${CacheType.chargingStationConfiguration}${hash}`
47 private getChargingStationTemplateKey (hash
: string): string {
48 return `${CacheType.chargingStationTemplate}${hash}`
51 private has (key
: string): boolean {
52 return this.lruCache
.has(key
)
55 private isChargingStationConfigurationCacheable (
56 chargingStationConfiguration
: ChargingStationConfiguration
59 chargingStationConfiguration
.configurationKey
!= null &&
60 chargingStationConfiguration
.stationInfo
!= null &&
61 chargingStationConfiguration
.automaticTransactionGenerator
!= null &&
62 chargingStationConfiguration
.configurationHash
!= null &&
63 isNotEmptyArray(chargingStationConfiguration
.configurationKey
) &&
64 !isEmpty(chargingStationConfiguration
.stationInfo
) &&
65 !isEmpty(chargingStationConfiguration
.automaticTransactionGenerator
) &&
66 isNotEmptyString(chargingStationConfiguration
.configurationHash
)
70 private set (key
: string, value
: CacheValueType
): void {
71 this.lruCache
.set(key
, value
)
74 public clear (): void {
78 public deleteChargingStationConfiguration (chargingStationConfigurationHash
: string): void {
79 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
82 public deleteChargingStationTemplate (chargingStationTemplateHash
: string): void {
83 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
86 public getChargingStationConfiguration (
87 chargingStationConfigurationHash
: string
88 ): ChargingStationConfiguration
{
90 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
91 ) as ChargingStationConfiguration
94 public getChargingStationTemplate (chargingStationTemplateHash
: string): ChargingStationTemplate
{
96 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
97 ) as ChargingStationTemplate
100 public hasChargingStationConfiguration (chargingStationConfigurationHash
: string): boolean {
101 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
))
104 public hasChargingStationTemplate (chargingStationTemplateHash
: string): boolean {
105 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
))
108 public setChargingStationConfiguration (
109 chargingStationConfiguration
: ChargingStationConfiguration
111 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
113 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
114 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
!),
115 chargingStationConfiguration
120 public setChargingStationTemplate (chargingStationTemplate
: ChargingStationTemplate
): void {
122 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
123 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
!),
124 chargingStationTemplate