X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FSharedLRUCache.ts;h=0da4558d4d46f4c4bd3d98f92eb2a7677856517d;hb=8f8f87c4fd9a0863d0aeb4a9a8671d1a2b4308e0;hp=9fd940795bbe33788589c528e74b058672a202a1;hpb=5edd8ba0f8978cfb3ca9d80f299d9748c6c5970e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index 9fd94079..0da4558d 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,122 +1,126 @@ -import LRUCache from 'mnemonist/lru-map-with-delete.js'; +import { LRUMapWithDelete as LRUCache } from 'mnemonist' +import { isEmpty } from 'rambda' -import { Bootstrap } from './Bootstrap'; -import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types'; -import { isEmptyObject, isNotEmptyArray, isNotEmptyString, isNullOrUndefined } from '../utils'; +import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js' +import { isNotEmptyArray, isNotEmptyString } from '../utils/index.js' +import { Bootstrap } from './Bootstrap.js' enum CacheType { chargingStationTemplate = 'chargingStationTemplate', - chargingStationConfiguration = 'chargingStationConfiguration', + chargingStationConfiguration = 'chargingStationConfiguration' } -type CacheValueType = ChargingStationTemplate | ChargingStationConfiguration; +type CacheValueType = ChargingStationTemplate | ChargingStationConfiguration export class SharedLRUCache { - private static instance: SharedLRUCache | null = null; - private readonly lruCache: LRUCache; + private static instance: SharedLRUCache | null = null + private readonly lruCache: LRUCache - private constructor() { + private constructor () { this.lruCache = new LRUCache( Bootstrap.getInstance().numberOfChargingStationTemplates + - Bootstrap.getInstance().numberOfChargingStations, - ); + Bootstrap.getInstance().numberOfConfiguredChargingStations + + Bootstrap.getInstance().numberOfProvisionedChargingStations + ) } - 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( - chargingStationConfiguration: ChargingStationConfiguration, + public setChargingStationConfiguration ( + chargingStationConfiguration: ChargingStationConfiguration ): void { if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { this.set( - this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash), - chargingStationConfiguration, - ); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!), + chargingStationConfiguration + ) } } - public getChargingStationConfiguration( - chargingStationConfigurationHash: string, + public getChargingStationConfiguration ( + chargingStationConfigurationHash: string ): ChargingStationConfiguration { return this.get( - this.getChargingStationConfigurationKey(chargingStationConfigurationHash), - ) as ChargingStationConfiguration; + this.getChargingStationConfigurationKey(chargingStationConfigurationHash) + ) 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), - chargingStationTemplate, - ); + // 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; + this.getChargingStationTemplateKey(chargingStationTemplateHash) + ) 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.chargingStationConfiguration}${hash}`; + private getChargingStationConfigurationKey (hash: string): string { + return `${CacheType.chargingStationConfiguration}${hash}` } - private getChargingStationTemplateKey(hash: string): string { - return `${CacheType.chargingStationTemplate}${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): CacheValueType | undefined { - return this.lruCache.get(key); + private get (key: string): CacheValueType | undefined { + return this.lruCache.get(key) } - private set(key: string, value: CacheValueType): 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( - chargingStationConfiguration: ChargingStationConfiguration, + private isChargingStationConfigurationCacheable ( + chargingStationConfiguration: ChargingStationConfiguration ): boolean { return ( - 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 - ); + chargingStationConfiguration.configurationKey != null && + chargingStationConfiguration.stationInfo != null && + chargingStationConfiguration.automaticTransactionGenerator != null && + chargingStationConfiguration.configurationHash != null && + isNotEmptyArray(chargingStationConfiguration.configurationKey) && + !isEmpty(chargingStationConfiguration.stationInfo) && + !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) && + isNotEmptyString(chargingStationConfiguration.configurationHash) + ) } }