1 import LRUCache from
'mnemonist/lru-map-with-delete';
3 import { Bootstrap
} from
'./internal';
4 import type { ChargingStationConfiguration
, ChargingStationTemplate
} from
'../types';
5 import { Utils
} from
'../utils';
8 CHARGING_STATION_TEMPLATE
= 'chargingStationTemplate',
9 CHARGING_STATION_CONFIGURATION
= 'chargingStationConfiguration',
12 type CacheableType
= ChargingStationTemplate
| ChargingStationConfiguration
;
14 export class SharedLRUCache
{
15 private static instance
: SharedLRUCache
| null = null;
16 private readonly lruCache
: LRUCache
<string, CacheableType
>;
18 private constructor() {
19 this.lruCache
= new LRUCache
<string, CacheableType
>(
20 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
21 Bootstrap
.getInstance().numberOfChargingStations
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 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
),
42 chargingStationConfiguration
47 public getChargingStationConfiguration(
48 chargingStationConfigurationHash
: string
49 ): ChargingStationConfiguration
{
51 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
52 ) as ChargingStationConfiguration
;
55 public deleteChargingStationConfiguration(chargingStationConfigurationHash
: string): void {
56 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
59 public hasChargingStationTemplate(chargingStationTemplateHash
: string): boolean {
60 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
63 public setChargingStationTemplate(chargingStationTemplate
: ChargingStationTemplate
): void {
65 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
),
66 chargingStationTemplate
70 public getChargingStationTemplate(chargingStationTemplateHash
: string): ChargingStationTemplate
{
72 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
73 ) as ChargingStationTemplate
;
76 public deleteChargingStationTemplate(chargingStationTemplateHash
: string): void {
77 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
80 public clear(): void {
81 this.lruCache
.clear();
84 private getChargingStationConfigurationKey(hash
: string): string {
85 return `${CacheType.CHARGING_STATION_CONFIGURATION}${hash}`;
88 private getChargingStationTemplateKey(hash
: string): string {
89 return `${CacheType.CHARGING_STATION_TEMPLATE}${hash}`;
92 private has(key
: string): boolean {
93 return this.lruCache
.has(key
);
96 private get(key
: string): CacheableType
| undefined {
97 return this.lruCache
.get(key
);
100 private set(key
: string, value
: CacheableType
): void {
101 this.lruCache
.set(key
, value
);
104 private delete(key
: string): void {
105 this.lruCache
.delete(key
);
108 private isChargingStationConfigurationCacheable(
109 chargingStationConfiguration
: ChargingStationConfiguration
112 Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationKey
) === false &&
113 Utils
.isNullOrUndefined(chargingStationConfiguration
?.stationInfo
) === false &&
114 Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationHash
) === false &&
115 Utils
.isNotEmptyArray(chargingStationConfiguration
?.configurationKey
) === true &&
116 Utils
.isEmptyObject(chargingStationConfiguration
?.stationInfo
) === false &&
117 Utils
.isNotEmptyString(chargingStationConfiguration
?.configurationHash
) === true