X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FSharedLRUCache.ts;h=7f459a4fa1b875bcf1c668d67ac53eaf9a43731f;hb=38ae4ce2dba0f31e0f21c20490be0e7d376ce47c;hp=b834a69a946fdba25f2dd3b45731597b2fa48ab2;hpb=57adbebc01e0a47b922d8376dd5bf8673e7dbb01;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index b834a69a..7f459a4f 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,108 +1,125 @@ -import ChargingStationConfiguration from '../types/ChargingStationConfiguration'; -import ChargingStationTemplate from '../types/ChargingStationTemplate'; -import LRUCache from 'mnemonist/lru-map-with-delete'; -import Utils from '../utils/Utils'; +import { LRUMapWithDelete as LRUCache } from 'mnemonist' +import { isEmpty } from 'rambda' + +import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js' +import { isNotEmptyArray, isNotEmptyString } from '../utils/index.js' +import { Bootstrap } from './Bootstrap.js' 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 { - private static instance: SharedLRUCache | null = null; - private readonly lruCache: LRUCache; +export class SharedLRUCache { + private static instance: SharedLRUCache | null = null + private readonly lruCache: LRUCache - private constructor() { - this.lruCache = new LRUCache(1000); + private constructor () { + this.lruCache = new LRUCache( + Bootstrap.getInstance().numberOfChargingStationTemplates + + Bootstrap.getInstance().numberOfConfiguredChargingStations + ) } - public static getInstance(): SharedLRUCache { - if (!SharedLRUCache.instance) { - SharedLRUCache.instance = new SharedLRUCache(); + public static getInstance (): SharedLRUCache { + if (SharedLRUCache.instance === null) { + SharedLRUCache.instance = new SharedLRUCache() } - return SharedLRUCache.instance; + return SharedLRUCache.instance } - public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean { - return this.has(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash); + public hasChargingStationConfiguration (chargingStationConfigurationHash: string): boolean { + return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) } - public setChargingStationConfiguration( + public setChargingStationConfiguration ( chargingStationConfiguration: ChargingStationConfiguration ): void { if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { this.set( - CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfiguration.configurationHash, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!), chargingStationConfiguration - ); + ) } } - public getChargingStationConfiguration( + public getChargingStationConfiguration ( chargingStationConfigurationHash: string ): ChargingStationConfiguration { return this.get( - CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash - ) as ChargingStationConfiguration; + this.getChargingStationConfigurationKey(chargingStationConfigurationHash) + ) as ChargingStationConfiguration } - public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void { - this.delete(CacheType.CHARGING_STATION_CONFIGURATION + chargingStationConfigurationHash); + public deleteChargingStationConfiguration (chargingStationConfigurationHash: string): void { + this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) } - public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean { - return this.has(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash); + public hasChargingStationTemplate (chargingStationTemplateHash: string): boolean { + return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)) } - public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void { + public setChargingStationTemplate (chargingStationTemplate: ChargingStationTemplate): void { this.set( - CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplate.templateHash, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!), chargingStationTemplate - ); + ) } - public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate { + public getChargingStationTemplate (chargingStationTemplateHash: string): ChargingStationTemplate { return this.get( - CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash - ) as ChargingStationTemplate; + this.getChargingStationTemplateKey(chargingStationTemplateHash) + ) as ChargingStationTemplate + } + + public deleteChargingStationTemplate (chargingStationTemplateHash: string): void { + this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)) + } + + public clear (): void { + this.lruCache.clear() } - public deleteChargingStationTemplate(chargingStationTemplateHash: string): void { - this.delete(CacheType.CHARGING_STATION_TEMPLATE + chargingStationTemplateHash); + private getChargingStationConfigurationKey (hash: string): string { + return `${CacheType.chargingStationConfiguration}${hash}` } - public clear(): void { - this.lruCache.clear(); + private getChargingStationTemplateKey (hash: string): string { + return `${CacheType.chargingStationTemplate}${hash}` } - private has(key: string): boolean { - return this.lruCache.has(key); + private has (key: string): boolean { + return this.lruCache.has(key) } - private get(key: string): CacheableType { - return this.lruCache.get(key); + private get (key: string): CacheValueType | undefined { + return this.lruCache.get(key) } - private set(key: string, value: CacheableType): void { - this.lruCache.set(key, value); + private set (key: string, value: CacheValueType): void { + this.lruCache.set(key, value) } - private delete(key: string): void { - this.lruCache.delete(key); + private delete (key: string): void { + this.lruCache.delete(key) } - private isChargingStationConfigurationCacheable( + private isChargingStationConfigurationCacheable ( 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) - ); + chargingStationConfiguration.configurationKey != null && + chargingStationConfiguration.stationInfo != null && + chargingStationConfiguration.automaticTransactionGenerator != null && + chargingStationConfiguration.configurationHash != null && + isNotEmptyArray(chargingStationConfiguration.configurationKey) && + !isEmpty(chargingStationConfiguration.stationInfo) && + !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) && + isNotEmptyString(chargingStationConfiguration.configurationHash) + ) } }