Rename class ChargingStatinCache to SharedLRUCache
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 12 Jun 2022 08:29:41 +0000 (10:29 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 12 Jun 2022 08:29:41 +0000 (10:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/SharedLRUCache.ts [moved from src/charging-station/ChargingStationCache.ts with 92% similarity]

index 80735db6c7449551b519c20a6d1fec866463cbdb..772389deabf0c9a24531dfb2991d2ad7f5fb98d1 100644 (file)
@@ -49,7 +49,6 @@ import { AutomaticTransactionGeneratorConfiguration } from '../types/AutomaticTr
 import BaseError from '../exception/BaseError';
 import { ChargePointErrorCode } from '../types/ocpp/ChargePointErrorCode';
 import { ChargePointStatus } from '../types/ocpp/ChargePointStatus';
-import { ChargingStationCache } from './ChargingStationCache';
 import ChargingStationConfiguration from '../types/ChargingStationConfiguration';
 import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
 import ChargingStationInfo from '../types/ChargingStationInfo';
@@ -73,6 +72,7 @@ import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService';
 import OCPPRequestService from './ocpp/OCPPRequestService';
 import { OCPPVersion } from '../types/ocpp/OCPPVersion';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
+import SharedLRUCache from './SharedLRUCache';
 import { SupervisionUrlDistribution } from '../types/ConfigurationData';
 import Utils from '../utils/Utils';
 import crypto from 'crypto';
@@ -107,7 +107,7 @@ export default class ChargingStation {
   private autoReconnectRetryCount: number;
   private stopped: boolean;
   private templateFileWatcher!: fs.FSWatcher;
-  private readonly cache: ChargingStationCache;
+  private readonly sharedLRUCache: SharedLRUCache;
   private automaticTransactionGenerator!: AutomaticTransactionGenerator;
   private webSocketPingSetInterval!: NodeJS.Timeout;
 
@@ -117,7 +117,7 @@ export default class ChargingStation {
     this.stopped = false;
     this.wsConnectionRestarted = false;
     this.autoReconnectRetryCount = 0;
-    this.cache = ChargingStationCache.getInstance();
+    this.sharedLRUCache = SharedLRUCache.getInstance();
     this.authorizedTagsCache = AuthorizedTagsCache.getInstance();
     this.connectors = new Map<number, ConnectorStatus>();
     this.requests = new Map<string, CachedRequest>();
@@ -514,7 +514,7 @@ export default class ChargingStation {
                 this.templateFile
               } file have changed, reload`
             );
-            this.cache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
+            this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
             // Initialize
             this.initialize();
             // Restart the ATG
@@ -561,9 +561,9 @@ export default class ChargingStation {
     if (this.getEnableStatistics()) {
       this.performanceStatistics.stop();
     }
-    this.cache.deleteChargingStationConfiguration(this.configurationFileHash);
+    this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
     this.templateFileWatcher.close();
-    this.cache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
+    this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
     this.bootNotificationResponse = null;
     parentPort.postMessage({
       id: ChargingStationWorkerMessageEvents.STOPPED,
@@ -714,8 +714,8 @@ export default class ChargingStation {
   private getTemplateFromFile(): ChargingStationTemplate | null {
     let template: ChargingStationTemplate = null;
     try {
-      if (this.cache.hasChargingStationTemplate(this.stationInfo?.templateHash)) {
-        template = this.cache.getChargingStationTemplate(this.stationInfo.templateHash);
+      if (this.sharedLRUCache.hasChargingStationTemplate(this.stationInfo?.templateHash)) {
+        template = this.sharedLRUCache.getChargingStationTemplate(this.stationInfo.templateHash);
       } else {
         const measureId = `${FileType.ChargingStationTemplate} read`;
         const beginId = PerformanceStatistics.beginMeasure(measureId);
@@ -727,7 +727,7 @@ export default class ChargingStation {
           .createHash(Constants.DEFAULT_HASH_ALGORITHM)
           .update(JSON.stringify(template))
           .digest('hex');
-        this.cache.setChargingStationTemplate(template);
+        this.sharedLRUCache.setChargingStationTemplate(template);
       }
     } catch (error) {
       FileUtils.handleFileException(
@@ -1172,8 +1172,10 @@ export default class ChargingStation {
     let configuration: ChargingStationConfiguration = null;
     if (this.configurationFile && fs.existsSync(this.configurationFile)) {
       try {
-        if (this.cache.hasChargingStationConfiguration(this.configurationFileHash)) {
-          configuration = this.cache.getChargingStationConfiguration(this.configurationFileHash);
+        if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) {
+          configuration = this.sharedLRUCache.getChargingStationConfiguration(
+            this.configurationFileHash
+          );
         } else {
           const measureId = `${FileType.ChargingStationConfiguration} read`;
           const beginId = PerformanceStatistics.beginMeasure(measureId);
@@ -1182,7 +1184,7 @@ export default class ChargingStation {
           ) as ChargingStationConfiguration;
           PerformanceStatistics.endMeasure(measureId, beginId);
           this.configurationFileHash = configuration.configurationHash;
-          this.cache.setChargingStationConfiguration(configuration);
+          this.sharedLRUCache.setChargingStationConfiguration(configuration);
         }
       } catch (error) {
         FileUtils.handleFileException(
@@ -1220,9 +1222,9 @@ export default class ChargingStation {
           fs.writeFileSync(fileDescriptor, JSON.stringify(configurationData, null, 2), 'utf8');
           fs.closeSync(fileDescriptor);
           PerformanceStatistics.endMeasure(measureId, beginId);
-          this.cache.deleteChargingStationConfiguration(this.configurationFileHash);
+          this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
           this.configurationFileHash = configurationHash;
-          this.cache.setChargingStationConfiguration(configurationData);
+          this.sharedLRUCache.setChargingStationConfiguration(configurationData);
         } else {
           logger.debug(
             `${this.logPrefix()} Not saving unchanged charging station configuration file ${
similarity index 92%
rename from src/charging-station/ChargingStationCache.ts
rename to src/charging-station/SharedLRUCache.ts
index b4e9b9d0d5c0a48e1142f4e10374010d428b4bc1..b834a69a946fdba25f2dd3b45731597b2fa48ab2 100644 (file)
@@ -10,19 +10,19 @@ enum CacheType {
 
 type CacheableType = ChargingStationTemplate | ChargingStationConfiguration;
 
-export class ChargingStationCache {
-  private static instance: ChargingStationCache | null = null;
+export default class SharedLRUCache {
+  private static instance: SharedLRUCache | null = null;
   private readonly lruCache: LRUCache<string, CacheableType>;
 
   private constructor() {
     this.lruCache = new LRUCache<string, CacheableType>(1000);
   }
 
-  public static getInstance(): ChargingStationCache {
-    if (!ChargingStationCache.instance) {
-      ChargingStationCache.instance = new ChargingStationCache();
+  public static getInstance(): SharedLRUCache {
+    if (!SharedLRUCache.instance) {
+      SharedLRUCache.instance = new SharedLRUCache();
     }
-    return ChargingStationCache.instance;
+    return SharedLRUCache.instance;
   }
 
   public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean {