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