-import LRUCache from 'mnemonist/lru-map-with-delete';
+import { LRUMapWithDelete as LRUCache } from 'mnemonist'
+import { isEmpty } from 'rambda'
-import type { ChargingStationConfiguration } from '../types/ChargingStationConfiguration';
-import type { ChargingStationTemplate } from '../types/ChargingStationTemplate';
-import Utils from '../utils/Utils';
+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<string, CacheableType>;
+export class SharedLRUCache {
+ private static instance: SharedLRUCache | null = null
+ private readonly lruCache: LRUCache<string, CacheValueType>
- private constructor() {
- this.lruCache = new LRUCache<string, CacheableType>(1000);
+ private constructor () {
+ this.lruCache = new LRUCache<string, CacheValueType>(
+ Bootstrap.getInstance().numberOfChargingStationTemplates +
+ 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(
+ 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) === false &&
- Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false &&
- Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false &&
- Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) === false &&
- Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false &&
- Utils.isEmptyString(chargingStationConfiguration?.configurationHash) === false
- );
+ chargingStationConfiguration.configurationKey != null &&
+ chargingStationConfiguration.stationInfo != null &&
+ chargingStationConfiguration.automaticTransactionGenerator != null &&
+ chargingStationConfiguration.configurationHash != null &&
+ isNotEmptyArray(chargingStationConfiguration.configurationKey) &&
+ !isEmpty(chargingStationConfiguration.stationInfo) &&
+ !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) &&
+ isNotEmptyString(chargingStationConfiguration.configurationHash)
+ )
}
}