fix: disable dynamic reload until spurious file change is identified
[e-mobility-charging-stations-simulator.git] / src / charging-station / SharedLRUCache.ts
index b834a69a946fdba25f2dd3b45731597b2fa48ab2..d55f358e18809b19e65972287330047440c3ab8b 100644 (file)
-import ChargingStationConfiguration from '../types/ChargingStationConfiguration';
-import ChargingStationTemplate from '../types/ChargingStationTemplate';
-import LRUCache from 'mnemonist/lru-map-with-delete';
-import Utils from '../utils/Utils';
+import LRUCache from 'mnemonist/lru-map-with-delete.js';
+
+import { Bootstrap } from './Bootstrap';
+import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types';
+import { isEmptyObject, isNotEmptyArray, isNotEmptyString, isNullOrUndefined } from '../utils';
 
 enum CacheType {
-  CHARGING_STATION_TEMPLATE = 'chargingStationTemplate',
-  CHARGING_STATION_CONFIGURATION = 'chargingStationConfiguration',
+  chargingStationTemplate = 'chargingStationTemplate',
+  chargingStationConfiguration = 'chargingStationConfiguration',
 }
 
-type CacheableType = ChargingStationTemplate | ChargingStationConfiguration;
+type CacheValueType = ChargingStationTemplate | ChargingStationConfiguration;
 
-export default class SharedLRUCache {
+export class SharedLRUCache {
   private static instance: SharedLRUCache | null = null;
-  private readonly lruCache: LRUCache<string, CacheableType>;
+  private readonly lruCache: LRUCache<string, CacheValueType>;
 
   private constructor() {
-    this.lruCache = new LRUCache<string, CacheableType>(1000);
+    this.lruCache = new LRUCache<string, CacheValueType>(
+      Bootstrap.getInstance().numberOfChargingStationTemplates +
+        Bootstrap.getInstance().numberOfChargingStations,
+    );
   }
 
   public static getInstance(): SharedLRUCache {
-    if (!SharedLRUCache.instance) {
+    if (SharedLRUCache.instance === null) {
       SharedLRUCache.instance = new SharedLRUCache();
     }
     return SharedLRUCache.instance;
   }
 
   public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean {
-    return this.has(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash);
+    return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
   }
 
   public setChargingStationConfiguration(
-    chargingStationConfiguration: ChargingStationConfiguration
+    chargingStationConfiguration: ChargingStationConfiguration,
   ): void {
     if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) {
       this.set(
-        CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfiguration.configurationHash,
-        chargingStationConfiguration
+        this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!),
+        chargingStationConfiguration,
       );
     }
   }
 
   public getChargingStationConfiguration(
-    chargingStationConfigurationHash: string
+    chargingStationConfigurationHash: string,
   ): ChargingStationConfiguration {
     return this.get(
-      CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash
+      this.getChargingStationConfigurationKey(chargingStationConfigurationHash),
     ) as ChargingStationConfiguration;
   }
 
   public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void {
-    this.delete(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash);
+    this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
   }
 
   public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean {
-    return this.has(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash);
+    return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash));
   }
 
   public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void {
     this.set(
-      CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplate.templateHash,
-      chargingStationTemplate
+      this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!),
+      chargingStationTemplate,
     );
   }
 
   public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate {
     return this.get(
-      CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash
+      this.getChargingStationTemplateKey(chargingStationTemplateHash),
     ) as ChargingStationTemplate;
   }
 
   public deleteChargingStationTemplate(chargingStationTemplateHash: string): void {
-    this.delete(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash);
+    this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash));
   }
 
   public clear(): void {
     this.lruCache.clear();
   }
 
+  private getChargingStationConfigurationKey(hash: string): string {
+    return `${CacheType.chargingStationConfiguration}${hash}`;
+  }
+
+  private getChargingStationTemplateKey(hash: string): string {
+    return `${CacheType.chargingStationTemplate}${hash}`;
+  }
+
   private has(key: string): boolean {
     return this.lruCache.has(key);
   }
 
-  private get(key: string): CacheableType {
+  private get(key: string): CacheValueType | undefined {
     return this.lruCache.get(key);
   }
 
-  private set(key: string, value: CacheableType): void {
+  private set(key: string, value: CacheValueType): void {
     this.lruCache.set(key, value);
   }
 
@@ -94,15 +106,17 @@ export default class SharedLRUCache {
   }
 
   private isChargingStationConfigurationCacheable(
-    chargingStationConfiguration: ChargingStationConfiguration
+    chargingStationConfiguration: ChargingStationConfiguration,
   ): boolean {
     return (
-      !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) &&
-      !Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) &&
-      !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) &&
-      !Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) &&
-      !Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) &&
-      !Utils.isEmptyString(chargingStationConfiguration?.configurationHash)
+      isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false &&
+      isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false &&
+      isNullOrUndefined(chargingStationConfiguration?.automaticTransactionGenerator) === false &&
+      isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false &&
+      isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true &&
+      isEmptyObject(chargingStationConfiguration.stationInfo!) === false &&
+      isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator!) === false &&
+      isNotEmptyString(chargingStationConfiguration?.configurationHash) === true
     );
   }
 }