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(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
32 public setChargingStationConfiguration(
33 chargingStationConfiguration
: ChargingStationConfiguration
35 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
37 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
),
38 chargingStationConfiguration
43 public getChargingStationConfiguration(
44 chargingStationConfigurationHash
: string
45 ): ChargingStationConfiguration
{
47 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
)
48 ) as ChargingStationConfiguration
;
51 public deleteChargingStationConfiguration(chargingStationConfigurationHash
: string): void {
52 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
55 public hasChargingStationTemplate(chargingStationTemplateHash
: string): boolean {
56 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
59 public setChargingStationTemplate(chargingStationTemplate
: ChargingStationTemplate
): void {
61 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
),
62 chargingStationTemplate
66 public getChargingStationTemplate(chargingStationTemplateHash
: string): ChargingStationTemplate
{
68 this.getChargingStationTemplateKey(chargingStationTemplateHash
)
69 ) as ChargingStationTemplate
;
72 public deleteChargingStationTemplate(chargingStationTemplateHash
: string): void {
73 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
76 public clear(): void {
77 this.lruCache
.clear();
80 private getChargingStationConfigurationKey(hash
: string): string {
81 return CacheType
.CHARGING_STATION_CONFIGURATION
+ hash
;
84 private getChargingStationTemplateKey(hash
: string): string {
85 return CacheType
.CHARGING_STATION_TEMPLATE
+ hash
;
88 private has(key
: string): boolean {
89 return this.lruCache
.has(key
);
92 private get(key
: string): CacheableType
{
93 return this.lruCache
.get(key
);
96 private set(key
: string, value
: CacheableType
): void {
97 this.lruCache
.set(key
, value
);
100 private delete(key
: string): void {
101 this.lruCache
.delete(key
);
104 private isChargingStationConfigurationCacheable(
105 chargingStationConfiguration
: ChargingStationConfiguration
108 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationKey
) &&
109 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.stationInfo
) &&
110 !Utils
.isNullOrUndefined(chargingStationConfiguration
?.configurationHash
) &&
111 !Utils
.isEmptyArray(chargingStationConfiguration
?.configurationKey
) &&
112 !Utils
.isEmptyObject(chargingStationConfiguration
?.stationInfo
) &&
113 !Utils
.isEmptyString(chargingStationConfiguration
?.configurationHash
)