1 import LRUCache from
'mnemonist/lru-map-with-delete';
3 import type ChargingStationConfiguration from
'../types/ChargingStationConfiguration';
4 import type ChargingStationTemplate from
'../types/ChargingStationTemplate';
5 import Utils from
'../utils/Utils';
8 CHARGING_STATION_TEMPLATE
= 'chargingStationTemplate',
9 CHARGING_STATION_CONFIGURATION
= 'chargingStationConfiguration',
12 type CacheableType
= ChargingStationTemplate
| ChargingStationConfiguration
;
14 export default 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
>(1000);
22 public static getInstance(): SharedLRUCache
{
23 if (SharedLRUCache
.instance
=== null) {
24 SharedLRUCache
.instance
= new SharedLRUCache();
26 return SharedLRUCache
.instance
;
29 public hasChargingStationConfiguration(chargingStationConfigurationHash
: string): boolean {
30 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
33 public setChargingStationConfiguration(
34 chargingStationConfiguration
: ChargingStationConfiguration
36 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
38 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
),
39 chargingStationConfiguration
44 public getChargingStationConfiguration(
45 chargingStationConfigurationHash
: string
46 ): ChargingStationConfiguration
{
48 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
49 ) as ChargingStationConfiguration
;
52 public deleteChargingStationConfiguration(chargingStationConfigurationHash
: string): void {
53 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
56 public hasChargingStationTemplate(chargingStationTemplateHash
: string): boolean {
57 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
60 public setChargingStationTemplate(chargingStationTemplate
: ChargingStationTemplate
): void {
62 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
),
63 chargingStationTemplate
67 public getChargingStationTemplate(chargingStationTemplateHash
: string): ChargingStationTemplate
{
69 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
70 ) as ChargingStationTemplate
;
73 public deleteChargingStationTemplate(chargingStationTemplateHash
: string): void {
74 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
77 public clear(): void {
78 this.lruCache
.clear();
81 private getChargingStationConfigurationKey(hash
: string): string {
82 return CacheType
.CHARGING_STATION_CONFIGURATION
+ hash
;
85 private getChargingStationTemplateKey(hash
: string): string {
86 return CacheType
.CHARGING_STATION_TEMPLATE
+ hash
;
89 private has(key
: string): boolean {
90 return this.lruCache
.has(key
);
93 private get(key
: string): CacheableType
{
94 return this.lruCache
.get(key
);
97 private set(key
: string, value
: CacheableType
): void {
98 this.lruCache
.set(key
, value
);
101 private delete(key
: string): void {
102 this.lruCache
.delete(key
);
105 private isChargingStationConfigurationCacheable(
106 chargingStationConfiguration
: ChargingStationConfiguration
109 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationKey
) &&
110 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.stationInfo
) &&
111 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationHash
) &&
112 !Utils
.isEmptyArray(chargingStationConfiguration
?.configurationKey
) &&
113 !Utils
.isEmptyObject(chargingStationConfiguration
?.stationInfo
) &&
114 !Utils
.isEmptyString(chargingStationConfiguration
?.configurationHash
)