/
Cache.ts
439 lines (393 loc) · 12 KB
/
Cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import { Storage, createStorage, StorageValue, Driver } from 'unstorage';
import fsDriver from 'unstorage/drivers/fs';
import redisDriver from 'unstorage/drivers/redis';
import httpDriver from 'unstorage/drivers/http';
import {
NetworkIdType,
TokenPrice,
TokenPriceSource,
formatTokenAddress,
formatTokenPriceSource,
pushTokenPriceSource,
tokenPriceFromSources,
} from '@sonarwatch/portfolio-core';
import overlayDriver from './overlayDriver';
import memoryDriver, {
DRIVER_SW_MEMORY_NAME,
MemoryDriver,
} from './memoryDriver';
import runInBatch from './utils/misc/runInBatch';
export type TransactionOptions = {
prefix: string;
networkId?: NetworkIdType;
};
const tokenPriceSourcePrefix = 'tokenpricesource';
const tokenPricesCacheTtl = 30 * 1000; // 30 sec
type CachedTokenPrice = {
tp: TokenPrice;
ts: number;
};
export type CacheConfig =
| CacheConfigOverlayHttp
| CacheConfigMemory
| CacheConfigRedis
| CacheConfigFilesystem
| CacheConfigHttp;
export type CacheConfigOverlayHttp = {
type: 'overlayHttp';
params: CacheConfigOverlayHttpParams;
};
export type CacheConfigOverlayHttpParams = {
bases: string[];
};
export type CacheConfigMemory = {
type: 'memory';
params: CacheConfigMemoryParams;
};
export type CacheConfigMemoryParams = {
ttl?: number;
};
export type CacheConfigHttp = {
type: 'http';
params: CacheConfigHttpParams;
};
export type CacheConfigHttpParams = {
base: string;
headers?: Record<string, string>;
};
export type CacheConfigRedis = {
type: 'redis';
params: CacheConfigRedisParams;
};
export type CacheConfigRedisParams = {
url: string;
tls: boolean;
db: number;
};
export type CacheConfigFilesystem = {
type: 'filesystem';
params: CacheConfigFilesystemParams;
};
export type CacheConfigFilesystemParams = {
base: string;
};
export type CacheConfigParams = {
filesystem: {
endpoint: string;
};
redis: {
url: string;
tls: boolean;
db: number;
};
};
export class Cache {
readonly storage: Storage;
readonly driver: Driver;
private tokenPricesCache: Map<string, CachedTokenPrice> = new Map();
constructor(cacheConfig: CacheConfig) {
this.driver = getDriverFromCacheConfig(cacheConfig);
this.storage = createStorage({
driver: this.driver,
});
}
importData(data: Map<string, string>): void {
if (this.driver.name === DRIVER_SW_MEMORY_NAME) {
(this.driver as MemoryDriver).importData(data, 20 * 60 * 1000);
}
}
exportData(): Map<string, string> {
if (this.driver.name === DRIVER_SW_MEMORY_NAME) {
return (this.driver as MemoryDriver).exportData();
}
return new Map();
}
async hasItem(key: string, opts: TransactionOptions): Promise<boolean> {
const item = await this.getItem(key, opts);
return item !== undefined;
}
async hasTokenPrice(address: string, networkId: NetworkIdType) {
const fAddress = formatTokenAddress(address, networkId);
const tokenPrice = await this.getTokenPrice(fAddress, networkId);
return tokenPrice !== undefined;
}
async getItem<K extends StorageValue>(
key: string,
opts: TransactionOptions
): Promise<K | undefined> {
const fullKey = getFullKey(key, opts);
const item = await this.storage.getItem<K>(fullKey).catch(() => null);
return item === null ? undefined : (item as K);
}
private getCachedTokenPrice(address: string, networkId: NetworkIdType) {
const cachedTokenPrice = this.tokenPricesCache.get(
getTokenPriceCacheKey(address, networkId)
);
if (!cachedTokenPrice) {
this.tokenPricesCache.delete(getTokenPriceCacheKey(address, networkId));
return undefined;
}
if (Date.now() > cachedTokenPrice.ts + tokenPricesCacheTtl) {
this.tokenPricesCache.delete(getTokenPriceCacheKey(address, networkId));
return undefined;
}
return cachedTokenPrice.tp;
}
async getTokenPrice(address: string, networkId: NetworkIdType) {
const fAddress = formatTokenAddress(address, networkId);
// Check if in cache
const cTokenPrice = this.getCachedTokenPrice(fAddress, networkId);
if (cTokenPrice) return cTokenPrice;
const sources = await this.getTokenPriceSources(fAddress, networkId);
if (!sources) return undefined;
const tokenPrice = tokenPriceFromSources(sources);
// Set in cache if tokenPrice is valide
if (tokenPrice) {
this.tokenPricesCache.set(getTokenPriceCacheKey(fAddress, networkId), {
tp: tokenPrice,
ts: Date.now(),
});
}
return tokenPrice;
}
async getTokenPrices(addresses: string[], networkId: NetworkIdType) {
const fAddresses = addresses.map((a) => formatTokenAddress(a, networkId));
const ffAddresses = [...new Set(fAddresses)];
const tokenPriceByAddress: Map<string, TokenPrice | undefined> = new Map();
const notCachedAddresses: string[] = [];
ffAddresses.forEach((address) => {
const tokenPrice = this.getCachedTokenPrice(address, networkId);
if (tokenPrice) tokenPriceByAddress.set(address, tokenPrice);
else notCachedAddresses.push(address);
});
const notCachedSources = await this.getTokenPricesSources(
notCachedAddresses,
networkId
);
notCachedSources.forEach((sources, i) => {
const address = notCachedAddresses[i];
if (!sources) tokenPriceByAddress.set(address, undefined);
else {
const tokenPrice = tokenPriceFromSources(sources);
tokenPriceByAddress.set(address, tokenPriceFromSources(sources));
if (tokenPrice) {
this.tokenPricesCache.set(getTokenPriceCacheKey(address, networkId), {
tp: tokenPrice,
ts: Date.now(),
});
}
}
});
return fAddresses.map((address) => tokenPriceByAddress.get(address));
}
private async getTokenPriceSources(
address: string,
networkId: NetworkIdType
) {
return this.getItem<TokenPriceSource[]>(address, {
prefix: tokenPriceSourcePrefix,
networkId,
});
}
private async getTokenPricesSources(
addresses: string[],
networkId: NetworkIdType
) {
return this.getItems<TokenPriceSource[]>(addresses, {
prefix: tokenPriceSourcePrefix,
networkId,
});
}
async getItems<K extends StorageValue>(
keys: string[],
opts: TransactionOptions
): Promise<(K | undefined)[]> {
const fullKeys = keys.map((k) => getFullKey(k, opts));
const res = await this.storage.getItems(fullKeys);
return res.map((r) => r.value as K);
}
async getAllItems<K extends StorageValue>(
opts: TransactionOptions
): Promise<K[]> {
const itemsMap = await this.getAllItemsAsMap<K>(opts);
return Array.from(itemsMap.values());
}
async getAllItemsAsMap<K extends StorageValue>(
opts: TransactionOptions
): Promise<Map<string, K>> {
const keys = await this.getKeys(opts);
const itemsMap: Map<string, K> = new Map();
const items = await this.getItems<K>(keys, opts);
for (let i = 0; i < items.length; i += 1) {
const item = items[i];
if (item !== undefined) itemsMap.set(keys[i], item);
}
return itemsMap;
}
async getAllTokenPrices(networkId: NetworkIdType) {
const addresses = await this.getTokenPriceAddresses(networkId);
const tokenPrices: Map<string, TokenPrice> = new Map();
const results = await runInBatch(
addresses.map((a) => () => this.getTokenPrice(a, networkId)),
20
);
for (let i = 0; i < results.length; i += 1) {
const result = results[i];
if (result.status === 'rejected') continue;
if (result.value !== undefined)
tokenPrices.set(addresses[i], result.value);
}
return tokenPrices;
}
async setItem<K extends StorageValue>(
key: string,
value: K,
opts: TransactionOptions
) {
const fullKey = getFullKey(key, opts);
return this.storage.setItem(fullKey, value);
}
async setTokenPriceSource(source: TokenPriceSource) {
const fSource = formatTokenPriceSource(source);
let cSources = await this.getItem<TokenPriceSource[]>(fSource.address, {
prefix: tokenPriceSourcePrefix,
networkId: fSource.networkId,
});
if (!cSources) cSources = [];
const newSources = pushTokenPriceSource(cSources, fSource);
if (!newSources) {
await this.removeItem(fSource.address, {
prefix: tokenPriceSourcePrefix,
networkId: fSource.networkId,
});
return;
}
await this.setItem(fSource.address, newSources, {
prefix: tokenPriceSourcePrefix,
networkId: fSource.networkId,
});
}
async removeItem(key: string, opts: TransactionOptions) {
const fullKey = getFullKey(key, opts);
return this.storage.removeItem(fullKey);
}
async getKeys(opts: TransactionOptions) {
const fullBase = getFullBase(opts);
const keys = await this.storage.getKeys(fullBase);
return keys.map((s) => s.substring(fullBase.length));
}
async getTokenPriceAddresses(networkId: NetworkIdType) {
return this.getKeys({
prefix: tokenPriceSourcePrefix,
networkId,
});
}
dispose() {
return this.storage.dispose();
}
}
function getTokenPriceCacheKey(
address: string,
networkId: NetworkIdType
): string {
return `${address}-${networkId}`;
}
function getFullKey(key: string, opts: TransactionOptions): string {
const { networkId, prefix } = opts;
const networkIdKeyPrefix = networkId ? `/${networkId.toString()}` : '';
return `/${prefix}${networkIdKeyPrefix}/${key}`;
}
function getFullBase(opts: TransactionOptions) {
const { networkId, prefix } = opts;
const networkIdBasePrefix = networkId ? `${networkId.toString()}:` : '';
const fullBase = `${prefix}:${networkIdBasePrefix}`;
return fullBase;
}
function getDriverFromCacheConfig(cacheConfig: CacheConfig) {
switch (cacheConfig.type) {
case 'overlayHttp':
return overlayDriver({
layers: cacheConfig.params.bases.map((base) => httpDriver({ base })),
}) as Driver;
case 'memory':
return memoryDriver({
ttl: cacheConfig.params.ttl || 60 * 60 * 1000,
}) as Driver;
case 'filesystem':
return fsDriver({
base: cacheConfig.params.base,
}) as Driver;
case 'redis':
return redisDriver({
url: cacheConfig.params.url,
tls: cacheConfig.params.tls ? {} : undefined,
db: cacheConfig.params.db,
}) as Driver;
case 'http':
return httpDriver({
base: cacheConfig.params.base,
headers: cacheConfig.params.headers,
}) as Driver;
default:
throw new Error('CacheConfig type is not valid');
}
}
export function getCacheConfig(): CacheConfig {
switch (process.env['CACHE_CONFIG_TYPE']) {
case 'overlayHttp':
return {
type: 'overlayHttp',
params: {
bases: (
process.env['CACHE_CONFIG_OVERLAY_HTTP_BASES'] ||
'http://localhost:3000/,https://portfolio-api.sonar.watch/v1/portfolio/cache/'
).split(','),
},
};
case 'memory':
return {
type: 'memory',
params: {},
};
case 'filesystem':
return {
type: 'filesystem',
params: {
base: process.env['CACHE_CONFIG_FILESYSTEM_BASE'] || './cache',
},
};
case 'redis':
return {
type: 'redis',
params: {
url: process.env['CACHE_CONFIG_REDIS_URL'] || '127.0.0.1:6379',
tls: process.env['CACHE_CONFIG_REDIS_TLS'] === 'true',
db: parseInt(process.env['CACHE_CONFIG_REDIS_DB'] || '0', 10),
},
};
case 'http':
return {
type: 'http',
params: {
base:
process.env['CACHE_CONFIG_HTTP_BASE'] || 'http://localhost:3000/',
headers: process.env['CACHE_CONFIG_HTTP_BEARER']
? {
Authorization: `Bearer ${process.env['CACHE_CONFIG_HTTP_BEARER']}`,
}
: undefined,
},
};
default:
return {
type: 'filesystem',
params: {
base: process.env['CACHE_CONFIG_FILESYSTEM_BASE'] || './cache',
},
};
}
}
export function getCache() {
const cacheConfig = getCacheConfig();
return new Cache(cacheConfig);
}