| 1 | import { LRUMapWithDelete as LRUCache } from 'mnemonist' |
| 2 | import { isEmpty } from 'rambda' |
| 3 | |
| 4 | import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js' |
| 5 | |
| 6 | import { isNotEmptyArray, isNotEmptyString } from '../utils/index.js' |
| 7 | import { Bootstrap } from './Bootstrap.js' |
| 8 | |
| 9 | enum CacheType { |
| 10 | chargingStationConfiguration = 'chargingStationConfiguration', |
| 11 | chargingStationTemplate = 'chargingStationTemplate', |
| 12 | } |
| 13 | |
| 14 | type CacheValueType = ChargingStationConfiguration | ChargingStationTemplate |
| 15 | |
| 16 | export class SharedLRUCache { |
| 17 | private static instance: null | SharedLRUCache = null |
| 18 | private readonly lruCache: LRUCache<string, CacheValueType> |
| 19 | |
| 20 | private constructor () { |
| 21 | this.lruCache = new LRUCache<string, CacheValueType>( |
| 22 | Bootstrap.getInstance().numberOfChargingStationTemplates + |
| 23 | Bootstrap.getInstance().numberOfConfiguredChargingStations + |
| 24 | Bootstrap.getInstance().numberOfProvisionedChargingStations |
| 25 | ) |
| 26 | } |
| 27 | |
| 28 | public static getInstance (): SharedLRUCache { |
| 29 | if (SharedLRUCache.instance === null) { |
| 30 | SharedLRUCache.instance = new SharedLRUCache() |
| 31 | } |
| 32 | return SharedLRUCache.instance |
| 33 | } |
| 34 | |
| 35 | private delete (key: string): void { |
| 36 | this.lruCache.delete(key) |
| 37 | } |
| 38 | |
| 39 | private get (key: string): CacheValueType | undefined { |
| 40 | return this.lruCache.get(key) |
| 41 | } |
| 42 | |
| 43 | private getChargingStationConfigurationKey (hash: string): string { |
| 44 | return `${CacheType.chargingStationConfiguration}${hash}` |
| 45 | } |
| 46 | |
| 47 | private getChargingStationTemplateKey (hash: string): string { |
| 48 | return `${CacheType.chargingStationTemplate}${hash}` |
| 49 | } |
| 50 | |
| 51 | private has (key: string): boolean { |
| 52 | return this.lruCache.has(key) |
| 53 | } |
| 54 | |
| 55 | private isChargingStationConfigurationCacheable ( |
| 56 | chargingStationConfiguration: ChargingStationConfiguration |
| 57 | ): boolean { |
| 58 | return ( |
| 59 | chargingStationConfiguration.configurationKey != null && |
| 60 | chargingStationConfiguration.stationInfo != null && |
| 61 | chargingStationConfiguration.automaticTransactionGenerator != null && |
| 62 | chargingStationConfiguration.configurationHash != null && |
| 63 | isNotEmptyArray(chargingStationConfiguration.configurationKey) && |
| 64 | !isEmpty(chargingStationConfiguration.stationInfo) && |
| 65 | !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) && |
| 66 | isNotEmptyString(chargingStationConfiguration.configurationHash) |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | private set (key: string, value: CacheValueType): void { |
| 71 | this.lruCache.set(key, value) |
| 72 | } |
| 73 | |
| 74 | public clear (): void { |
| 75 | this.lruCache.clear() |
| 76 | } |
| 77 | |
| 78 | public deleteChargingStationConfiguration (chargingStationConfigurationHash: string): void { |
| 79 | this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) |
| 80 | } |
| 81 | |
| 82 | public deleteChargingStationTemplate (chargingStationTemplateHash: string): void { |
| 83 | this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash)) |
| 84 | } |
| 85 | |
| 86 | public getChargingStationConfiguration ( |
| 87 | chargingStationConfigurationHash: string |
| 88 | ): ChargingStationConfiguration { |
| 89 | return this.get( |
| 90 | this.getChargingStationConfigurationKey(chargingStationConfigurationHash) |
| 91 | ) as ChargingStationConfiguration |
| 92 | } |
| 93 | |
| 94 | public getChargingStationTemplate (chargingStationTemplateHash: string): ChargingStationTemplate { |
| 95 | return this.get( |
| 96 | this.getChargingStationTemplateKey(chargingStationTemplateHash) |
| 97 | ) as ChargingStationTemplate |
| 98 | } |
| 99 | |
| 100 | public hasChargingStationConfiguration (chargingStationConfigurationHash: string): boolean { |
| 101 | return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash)) |
| 102 | } |
| 103 | |
| 104 | public hasChargingStationTemplate (chargingStationTemplateHash: string): boolean { |
| 105 | return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash)) |
| 106 | } |
| 107 | |
| 108 | public setChargingStationConfiguration ( |
| 109 | chargingStationConfiguration: ChargingStationConfiguration |
| 110 | ): void { |
| 111 | if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) { |
| 112 | this.set( |
| 113 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 114 | this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!), |
| 115 | chargingStationConfiguration |
| 116 | ) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public setChargingStationTemplate (chargingStationTemplate: ChargingStationTemplate): void { |
| 121 | this.set( |
| 122 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 123 | this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!), |
| 124 | chargingStationTemplate |
| 125 | ) |
| 126 | } |
| 127 | } |