X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FSharedLRUCache.ts;h=b50f134206eef6f42472deb2c7a0a3b463a1df24;hb=54510a60e9114c2076c86e6c90fe73c0dfefdb45;hp=a40de80293ad9090904be33cd5213e349bfe06fe;hpb=1ca780f9d385bcf96a016ab5ba57ca0f19c94b74;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index a40de802..b50f1342 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,117 +1,124 @@ -import LRUCache from 'mnemonist/lru-map-with-delete'; +import { LRUMapWithDelete as LRUCache } from 'mnemonist' -import ChargingStationConfiguration from '../types/ChargingStationConfiguration'; -import ChargingStationTemplate from '../types/ChargingStationTemplate'; -import Utils from '../utils/Utils'; +import { Bootstrap } from './Bootstrap.js' +import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js' +import { isEmptyObject, isNotEmptyArray, isNotEmptyString } from '../utils/index.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().numberOfChargingStations + ) } - public static getInstance(): SharedLRUCache { + public static getInstance (): SharedLRUCache { if (SharedLRUCache.instance === null) { - SharedLRUCache.instance = new SharedLRUCache(); + SharedLRUCache.instance = new SharedLRUCache() } - return SharedLRUCache.instance; + return SharedLRUCache.instance } - public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean { - return this.has(this.getChargingStationConfigurationKey(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( - this.getChargingStationConfigurationKey(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( this.getChargingStationConfigurationKey(chargingStationConfigurationHash) - ) as ChargingStationConfiguration; + ) as ChargingStationConfiguration } - public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void { - this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)); + public deleteChargingStationConfiguration (chargingStationConfigurationHash: string): void { + this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) } - public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean { - return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)); + public hasChargingStationTemplate (chargingStationTemplateHash: string): boolean { + return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)) } - public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void { + public setChargingStationTemplate (chargingStationTemplate: ChargingStationTemplate): void { this.set( - this.getChargingStationTemplateKey(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( this.getChargingStationTemplateKey(chargingStationTemplateHash) - ) as ChargingStationTemplate; + ) as ChargingStationTemplate } - public deleteChargingStationTemplate(chargingStationTemplateHash: string): void { - this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)); + public deleteChargingStationTemplate (chargingStationTemplateHash: string): void { + this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)) } - public clear(): void { - this.lruCache.clear(); + public clear (): void { + this.lruCache.clear() } - private getChargingStationConfigurationKey(hash: string): string { - return CacheType.CHARGING_STATION_CONFIGURATION + hash; + private getChargingStationConfigurationKey (hash: string): string { + return `${CacheType.chargingStationConfiguration}${hash}` } - private getChargingStationTemplateKey(hash: string): string { - return CacheType.CHARGING_STATION_TEMPLATE + hash; + 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) && + !isEmptyObject(chargingStationConfiguration.stationInfo) && + !isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator) && + isNotEmptyString(chargingStationConfiguration.configurationHash) + ) } }