1 import ChargingStationConfiguration from
'../types/ChargingStationConfiguration';
2 import ChargingStationTemplate from
'../types/ChargingStationTemplate';
3 import LRUCache from
'mnemonist/lru-map-with-delete';
4 import Utils from
'../utils/Utils';
7 CHARGING_STATION_TEMPLATE
= 'chargingStationTemplate',
8 CHARGING_STATION_CONFIGURATION
= 'chargingStationConfiguration',
11 type CacheableType
= ChargingStationTemplate
| ChargingStationConfiguration
;
13 export default class SharedLRUCache
{
14 private static instance
: SharedLRUCache
| null = null;
15 private readonly lruCache
: LRUCache
<string, CacheableType
>;
17 private constructor() {
18 this.lruCache
= new LRUCache
<string, CacheableType
>(1000);
21 public static getInstance(): SharedLRUCache
{
22 if (!SharedLRUCache
.instance
) {
23 SharedLRUCache
.instance
= new SharedLRUCache();
25 return SharedLRUCache
.instance
;
28 public hasChargingStationConfiguration(chargingStationConfigurationHash
: string): boolean {
29 return this.has(CacheType
.CHARGING_STATION_CONFIGURATION
+ chargingStationConfigurationHash
);
32 public setChargingStationConfiguration(
33 chargingStationConfiguration
: ChargingStationConfiguration
35 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
37 CacheType
.CHARGING_STATION_CONFIGURATION
+ chargingStationConfiguration
.configurationHash
,
38 chargingStationConfiguration
43 public getChargingStationConfiguration(
44 chargingStationConfigurationHash
: string
45 ): ChargingStationConfiguration
{
47 CacheType
.CHARGING_STATION_CONFIGURATION
+ chargingStationConfigurationHash
48 ) as ChargingStationConfiguration
;
51 public deleteChargingStationConfiguration(chargingStationConfigurationHash
: string): void {
52 this.delete(CacheType
.CHARGING_STATION_CONFIGURATION
+ chargingStationConfigurationHash
);
55 public hasChargingStationTemplate(chargingStationTemplateHash
: string): boolean {
56 return this.has(CacheType
.CHARGING_STATION_TEMPLATE
+ chargingStationTemplateHash
);
59 public setChargingStationTemplate(chargingStationTemplate
: ChargingStationTemplate
): void {
61 CacheType
.CHARGING_STATION_TEMPLATE
+ chargingStationTemplate
.templateHash
,
62 chargingStationTemplate
66 public getChargingStationTemplate(chargingStationTemplateHash
: string): ChargingStationTemplate
{
68 CacheType
.CHARGING_STATION_TEMPLATE
+ chargingStationTemplateHash
69 ) as ChargingStationTemplate
;
72 public deleteChargingStationTemplate(chargingStationTemplateHash
: string): void {
73 this.delete(CacheType
.CHARGING_STATION_TEMPLATE
+ chargingStationTemplateHash
);
76 public clear(): void {
77 this.lruCache
.clear();
80 private has(key
: string): boolean {
81 return this.lruCache
.has(key
);
84 private get(key
: string): CacheableType
{
85 return this.lruCache
.get(key
);
88 private set(key
: string, value
: CacheableType
): void {
89 this.lruCache
.set(key
, value
);
92 private delete(key
: string): void {
93 this.lruCache
.delete(key
);
96 private isChargingStationConfigurationCacheable(
97 chargingStationConfiguration
: ChargingStationConfiguration
100 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationKey
) &&
101 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.stationInfo
) &&
102 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationHash
) &&
103 !Utils
.isEmptyArray(chargingStationConfiguration
?.configurationKey
) &&
104 !Utils
.isEmptyObject(chargingStationConfiguration
?.stationInfo
) &&
105 !Utils
.isEmptyString(chargingStationConfiguration
?.configurationHash
)