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