1 import LRUCache from
'mnemonist/lru-map-with-delete';
3 import { Bootstrap
} from
'../internal';
4 import type { ChargingStationConfiguration
} from
'../types/ChargingStationConfiguration';
5 import type { ChargingStationTemplate
} from
'../types/ChargingStationTemplate';
6 import Utils from
'../utils/Utils';
9 CHARGING_STATION_TEMPLATE
= 'chargingStationTemplate',
10 CHARGING_STATION_CONFIGURATION
= 'chargingStationConfiguration',
13 type CacheableType
= ChargingStationTemplate
| ChargingStationConfiguration
;
15 export default class SharedLRUCache
{
16 private static instance
: SharedLRUCache
| null = null;
17 private readonly lruCache
: LRUCache
<string, CacheableType
>;
19 private constructor() {
20 this.lruCache
= new LRUCache
<string, CacheableType
>(
21 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
22 Bootstrap
.getInstance().numberOfChargingStations
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 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 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
),
67 chargingStationTemplate
71 public getChargingStationTemplate(chargingStationTemplateHash
: string): ChargingStationTemplate
{
73 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
74 ) as ChargingStationTemplate
;
77 public deleteChargingStationTemplate(chargingStationTemplateHash
: string): void {
78 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
81 public clear(): void {
82 this.lruCache
.clear();
85 private getChargingStationConfigurationKey(hash
: string): string {
86 return `${CacheType.CHARGING_STATION_CONFIGURATION}${hash}`;
89 private getChargingStationTemplateKey(hash
: string): string {
90 return `${CacheType.CHARGING_STATION_TEMPLATE}${hash}`;
93 private has(key
: string): boolean {
94 return this.lruCache
.has(key
);
97 private get(key
: string): CacheableType
| undefined {
98 return this.lruCache
.get(key
);
101 private set(key
: string, value
: CacheableType
): void {
102 this.lruCache
.set(key
, value
);
105 private delete(key
: string): void {
106 this.lruCache
.delete(key
);
109 private isChargingStationConfigurationCacheable(
110 chargingStationConfiguration
: ChargingStationConfiguration
113 Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationKey
) === false &&
114 Utils
.isNullOrUndefined(chargingStationConfiguration
?.stationInfo
) === false &&
115 Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationHash
) === false &&
116 Utils
.isEmptyArray(chargingStationConfiguration
?.configurationKey
) === false &&
117 Utils
.isEmptyObject(chargingStationConfiguration
?.stationInfo
) === false &&
118 Utils
.isNotEmptyString(chargingStationConfiguration
?.configurationHash
) === true