7f459a4fa1b875bcf1c668d67ac53eaf9a43731f
[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 )
24 }
25
26 public static getInstance (): SharedLRUCache {
27 if (SharedLRUCache.instance === null) {
28 SharedLRUCache.instance = new SharedLRUCache()
29 }
30 return SharedLRUCache.instance
31 }
32
33 public hasChargingStationConfiguration (chargingStationConfigurationHash: string): boolean {
34 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash))
35 }
36
37 public setChargingStationConfiguration (
38 chargingStationConfiguration: ChargingStationConfiguration
39 ): void {
40 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) {
41 this.set(
42 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43 this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash!),
44 chargingStationConfiguration
45 )
46 }
47 }
48
49 public getChargingStationConfiguration (
50 chargingStationConfigurationHash: string
51 ): ChargingStationConfiguration {
52 return this.get(
53 this.getChargingStationConfigurationKey(chargingStationConfigurationHash)
54 ) as ChargingStationConfiguration
55 }
56
57 public deleteChargingStationConfiguration (chargingStationConfigurationHash: string): void {
58 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash))
59 }
60
61 public hasChargingStationTemplate (chargingStationTemplateHash: string): boolean {
62 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash))
63 }
64
65 public setChargingStationTemplate (chargingStationTemplate: ChargingStationTemplate): void {
66 this.set(
67 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
68 this.getChargingStationTemplateKey(chargingStationTemplate.templateHash!),
69 chargingStationTemplate
70 )
71 }
72
73 public getChargingStationTemplate (chargingStationTemplateHash: string): ChargingStationTemplate {
74 return this.get(
75 this.getChargingStationTemplateKey(chargingStationTemplateHash)
76 ) as ChargingStationTemplate
77 }
78
79 public deleteChargingStationTemplate (chargingStationTemplateHash: string): void {
80 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash))
81 }
82
83 public clear (): void {
84 this.lruCache.clear()
85 }
86
87 private getChargingStationConfigurationKey (hash: string): string {
88 return `${CacheType.chargingStationConfiguration}${hash}`
89 }
90
91 private getChargingStationTemplateKey (hash: string): string {
92 return `${CacheType.chargingStationTemplate}${hash}`
93 }
94
95 private has (key: string): boolean {
96 return this.lruCache.has(key)
97 }
98
99 private get (key: string): CacheValueType | undefined {
100 return this.lruCache.get(key)
101 }
102
103 private set (key: string, value: CacheValueType): void {
104 this.lruCache.set(key, value)
105 }
106
107 private delete (key: string): void {
108 this.lruCache.delete(key)
109 }
110
111 private isChargingStationConfigurationCacheable (
112 chargingStationConfiguration: ChargingStationConfiguration
113 ): boolean {
114 return (
115 chargingStationConfiguration.configurationKey != null &&
116 chargingStationConfiguration.stationInfo != null &&
117 chargingStationConfiguration.automaticTransactionGenerator != null &&
118 chargingStationConfiguration.configurationHash != null &&
119 isNotEmptyArray(chargingStationConfiguration.configurationKey) &&
120 !isEmpty(chargingStationConfiguration.stationInfo) &&
121 !isEmpty(chargingStationConfiguration.automaticTransactionGenerator) &&
122 isNotEmptyString(chargingStationConfiguration.configurationHash)
123 )
124 }
125 }