|
1 | | -import { ForeignKeyDS } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/data-structures/foreign-key.ds.js'; |
2 | | -import { PrimaryKeyDS } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/data-structures/primary-key.ds.js'; |
3 | | -import { TableStructureDS } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/data-structures/table-structure.ds.js'; |
4 | | -import { Knex } from 'knex'; |
5 | 1 | import { LRUCache } from 'lru-cache'; |
6 | | -import { ConnectionEntity } from '../../entities/connection/connection.entity.js'; |
7 | 2 | import { isSaaS } from '../app/is-saas.js'; |
8 | 3 | import { Constants } from '../constants/constants.js'; |
9 | 4 |
|
10 | | -const knexCache = new LRUCache(Constants.DEFAULT_CONNECTION_CACHE_OPTIONS); |
11 | | -const tunnelCache = new LRUCache(Constants.DEFAULT_TUNNEL_CACHE_OPTIONS); |
12 | | -const driverCache = new LRUCache(Constants.DEFAULT_DRIVER_CACHE_OPTIONS); |
13 | 5 | const invitationCache = new LRUCache(Constants.DEFAULT_INVITATION_CACHE_OPTIONS); |
14 | | -const tableStructureCache = new LRUCache(Constants.DEFAULT_TABLE_STRUCTURE_ELEMENTS_CACHE_OPTIONS); |
15 | | -const tableForeignKeysCache = new LRUCache(Constants.DEFAULT_TABLE_STRUCTURE_ELEMENTS_CACHE_OPTIONS); |
16 | | -const tablePrimaryKeysCache = new LRUCache(Constants.DEFAULT_TABLE_STRUCTURE_ELEMENTS_CACHE_OPTIONS); |
17 | 6 | const tableReadPermissionCache = new LRUCache(Constants.DEFAULT_TABLE_PERMISSIONS_CACHE_OPTIONS); |
18 | 7 |
|
19 | 8 | export class Cacher { |
@@ -76,129 +65,8 @@ export class Cacher { |
76 | 65 | return userInvitations <= 10 && groupInvitations <= 10; |
77 | 66 | } |
78 | 67 |
|
79 | | - public static getCachedKnex(connectionConfig): Knex { |
80 | | - const cachedKnex = knexCache.get(JSON.stringify(connectionConfig)) as Knex; |
81 | | - return cachedKnex ? cachedKnex : null; |
82 | | - } |
83 | | - |
84 | | - public static setKnexCache(connectionConfig, newKnex: Knex): void { |
85 | | - knexCache.set(JSON.stringify(connectionConfig), newKnex); |
86 | | - } |
87 | | - |
88 | | - public static getTunnelCache(connection: ConnectionEntity): any { |
89 | | - const cachedTnl = tunnelCache.get(JSON.stringify(connection)); |
90 | | - return cachedTnl ? cachedTnl : null; |
91 | | - } |
92 | | - |
93 | | - public static setTunnelCache(connection: ConnectionEntity, tnlObj): void { |
94 | | - tunnelCache.set(JSON.stringify(connection), tnlObj); |
95 | | - } |
96 | | - |
97 | | - public static delTunnelCache(connection: ConnectionEntity): void { |
98 | | - tunnelCache.delete(JSON.stringify(connection)); |
99 | | - } |
100 | | - |
101 | | - public static getDriverCache(connection: ConnectionEntity): any { |
102 | | - const cachedDriver = driverCache.get(JSON.stringify(connection)); |
103 | | - return cachedDriver ? cachedDriver : null; |
104 | | - } |
105 | | - |
106 | | - public static setDriverCache(connection: ConnectionEntity, newDriver): void { |
107 | | - driverCache.set(JSON.stringify(connection), newDriver); |
108 | | - } |
109 | | - |
110 | | - public static delDriverCache(connection: ConnectionEntity): void { |
111 | | - driverCache.delete(JSON.stringify(connection)); |
112 | | - } |
113 | | - |
114 | | - public static setTableStructureCache( |
115 | | - connection: ConnectionEntity, |
116 | | - tableName: string, |
117 | | - structure: Array<TableStructureDS>, |
118 | | - ): void { |
119 | | - const connectionCopy = { |
120 | | - ...connection, |
121 | | - }; |
122 | | - const cacheObj = JSON.stringify({ connectionCopy, tableName }); |
123 | | - tableStructureCache.set(cacheObj, structure); |
124 | | - } |
125 | | - |
126 | | - public static getTableStructureCache(connection: ConnectionEntity, tableName: string): Array<TableStructureDS> { |
127 | | - const connectionCopy = { |
128 | | - ...connection, |
129 | | - }; |
130 | | - const cacheObj = JSON.stringify({ connectionCopy, tableName }); |
131 | | - const foundStructure = tableStructureCache.get(cacheObj) as Array<TableStructureDS>; |
132 | | - return foundStructure ? foundStructure : null; |
133 | | - } |
134 | | - |
135 | | - public static setTablePrimaryKeysCache( |
136 | | - connection: ConnectionEntity, |
137 | | - tableName: string, |
138 | | - primaryKeys: Array<PrimaryKeyDS>, |
139 | | - ): void { |
140 | | - const connectionCopy = { |
141 | | - ...connection, |
142 | | - }; |
143 | | - const cacheObj = JSON.stringify({ connectionCopy, tableName }); |
144 | | - tablePrimaryKeysCache.set(cacheObj, primaryKeys); |
145 | | - } |
146 | | - |
147 | | - public static getTablePrimaryKeysCache(connection: ConnectionEntity, tableName: string): Array<PrimaryKeyDS> { |
148 | | - const connectionCopy = { |
149 | | - ...connection, |
150 | | - }; |
151 | | - const cacheObj = JSON.stringify({ connectionCopy, tableName }); |
152 | | - const foundKeys = tablePrimaryKeysCache.get(cacheObj) as Array<PrimaryKeyDS>; |
153 | | - return foundKeys ? foundKeys : null; |
154 | | - } |
155 | | - |
156 | | - public static setTableForeignKeysCache( |
157 | | - connection: ConnectionEntity, |
158 | | - tableName: string, |
159 | | - foreignKeys: Array<ForeignKeyDS>, |
160 | | - ): void { |
161 | | - const connectionCopy = { |
162 | | - ...connection, |
163 | | - }; |
164 | | - const cacheObj = JSON.stringify({ connectionCopy, tableName }); |
165 | | - tableForeignKeysCache.set(cacheObj, foreignKeys); |
166 | | - } |
167 | | - |
168 | | - public static getTableForeignKeysCache(connection: ConnectionEntity, tableName: string): Array<ForeignKeyDS> { |
169 | | - const connectionCopy = { |
170 | | - ...connection, |
171 | | - }; |
172 | | - const cacheObj = JSON.stringify({ connectionCopy, tableName }); |
173 | | - const foundKeys = tableForeignKeysCache.get(cacheObj) as Array<ForeignKeyDS>; |
174 | | - return foundKeys ? foundKeys : null; |
175 | | - } |
176 | | - |
177 | 68 | public static async clearAllCache(): Promise<void> { |
178 | | - const elements = []; |
179 | | - knexCache.forEach((value, key) => { |
180 | | - elements.push({ key, value }); |
181 | | - }); |
182 | | - for (const element of elements) { |
183 | | - await element.value.destroy(); |
184 | | - knexCache.delete(element.key); |
185 | | - } |
186 | | - knexCache.clear(); |
187 | | - |
188 | | - const tunnelElements = []; |
189 | | - tunnelCache.forEach((value, key) => { |
190 | | - tunnelElements.push({ key, value }); |
191 | | - }); |
192 | | - for (const element of tunnelElements) { |
193 | | - await element.value.close(); |
194 | | - tunnelCache.delete(element.key); |
195 | | - } |
196 | | - tunnelCache.clear(); |
197 | | - await driverCache.clear(); |
198 | 69 | await invitationCache.clear(); |
199 | | - await tableStructureCache.clear(); |
200 | | - await tableForeignKeysCache.clear(); |
201 | | - await tablePrimaryKeysCache.clear(); |
202 | 70 | await tableReadPermissionCache.clear(); |
203 | 71 | } |
204 | 72 | } |
0 commit comments