1 import LRUCache from
'mnemonist/lru-map-with-delete.js';
3 import { Bootstrap
} from
'./Bootstrap.js';
4 import type { ChargingStationConfiguration
, ChargingStationTemplate
} from
'../types/index.js';
10 } from
'../utils/index.js';
13 chargingStationTemplate
= 'chargingStationTemplate',
14 chargingStationConfiguration
= 'chargingStationConfiguration',
17 type CacheValueType
= ChargingStationTemplate
| ChargingStationConfiguration
;
19 export class SharedLRUCache
{
20 private static instance
: SharedLRUCache
| null = null;
21 private readonly lruCache
: LRUCache
<string, CacheValueType
>;
23 private constructor() {
24 this.lruCache
= new LRUCache
<string, CacheValueType
>(
25 Bootstrap
.getInstance().numberOfChargingStationTemplates
+
26 Bootstrap
.getInstance().numberOfChargingStations
,
30 public static getInstance(): SharedLRUCache
{
31 if (SharedLRUCache
.instance
=== null) {
32 SharedLRUCache
.instance
= new SharedLRUCache();
34 return SharedLRUCache
.instance
;
37 public hasChargingStationConfiguration(chargingStationConfigurationHash
: string): boolean {
38 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
41 public setChargingStationConfiguration(
42 chargingStationConfiguration
: ChargingStationConfiguration
,
44 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration
)) {
46 this.getChargingStationConfigurationKey(chargingStationConfiguration
.configurationHash
!),
47 chargingStationConfiguration
,
52 public getChargingStationConfiguration(
53 chargingStationConfigurationHash
: string,
54 ): ChargingStationConfiguration
{
56 this.getChargingStationConfigurationKey(chargingStationConfigurationHash
),
57 ) as ChargingStationConfiguration
;
60 public deleteChargingStationConfiguration(chargingStationConfigurationHash
: string): void {
61 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash
));
64 public hasChargingStationTemplate(chargingStationTemplateHash
: string): boolean {
65 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
68 public setChargingStationTemplate(chargingStationTemplate
: ChargingStationTemplate
): void {
70 this.getChargingStationTemplateKey(chargingStationTemplate
.templateHash
!),
71 chargingStationTemplate
,
75 public getChargingStationTemplate(chargingStationTemplateHash
: string): ChargingStationTemplate
{
77 this.getChargingStationTemplateKey(chargingStationTemplateHash
),
78 ) as ChargingStationTemplate
;
81 public deleteChargingStationTemplate(chargingStationTemplateHash
: string): void {
82 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash
));
85 public clear(): void {
86 this.lruCache
.clear();
89 private getChargingStationConfigurationKey(hash
: string): string {
90 return `${CacheType.chargingStationConfiguration}${hash}`;
93 private getChargingStationTemplateKey(hash
: string): string {
94 return `${CacheType.chargingStationTemplate}${hash}`;
97 private has(key
: string): boolean {
98 return this.lruCache
.has(key
);
101 private get(key
: string): CacheValueType
| undefined {
102 return this.lruCache
.get(key
);
105 private set(key
: string, value
: CacheValueType
): void {
106 this.lruCache
.set(key
, value
);
109 private delete(key
: string): void {
110 this.lruCache
.delete(key
);
113 private isChargingStationConfigurationCacheable(
114 chargingStationConfiguration
: ChargingStationConfiguration
,
117 isNullOrUndefined(chargingStationConfiguration
?.configurationKey
) === false &&
118 isNullOrUndefined(chargingStationConfiguration
?.stationInfo
) === false &&
119 isNullOrUndefined(chargingStationConfiguration
?.automaticTransactionGenerator
) === false &&
120 isNullOrUndefined(chargingStationConfiguration
?.configurationHash
) === false &&
121 isNotEmptyArray(chargingStationConfiguration
?.configurationKey
) === true &&
122 isEmptyObject(chargingStationConfiguration
.stationInfo
!) === false &&
123 isEmptyObject(chargingStationConfiguration
.automaticTransactionGenerator
!) === false &&
124 isNotEmptyString(chargingStationConfiguration
?.configurationHash
) === true