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