-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkeyv.ts
97 lines (72 loc) · 2.71 KB
/
keyv.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
// import fs from 'fs';
import Keyv, { KeyvStoreAdapter } from 'keyv';
import KeyvFile from 'keyv-file';
import { Singleton } from '@/utils/singleton';
import { SERVER_CONFIG } from '@/config/server';
const { KeyvLruManagedTtl } = require('keyv-lru');
const {
cacheHttpFilePath,
cacheDatabaseFilePath,
cacheDatabaseLruItems,
cacheHttpLruItems,
cacheDatabaseDisabled,
} = SERVER_CONFIG;
// deletes cache for testing
try {
// fs.unlinkSync(cacheHttpFilePath);
// fs.unlinkSync(cacheDatabaseFilePath);
} catch (error) {}
/*-------------------------------- database cache ------------------------------*/
class CacheDatabaseInstance {
private static storeAdapter: KeyvStoreAdapter | null = null;
private static createCacheDatabase(): Keyv {
return new Keyv({ store: CacheDatabaseInstance.storeAdapter });
}
public static setAdapter(adapter: KeyvStoreAdapter): void {
CacheDatabaseInstance.storeAdapter = adapter;
}
public static getCacheDatabase(): Keyv {
return Singleton.getInstance<Keyv>('CacheDatabaseInstance', () =>
CacheDatabaseInstance.createCacheDatabase()
);
}
}
// ttl: undefined - infinite duration
const _databaseFileAdapter = new KeyvFile({ filename: cacheDatabaseFilePath });
const databaseLruAdapter = new KeyvLruManagedTtl({ max: cacheDatabaseLruItems });
CacheDatabaseInstance.setAdapter(databaseLruAdapter);
export const getCacheDatabase = CacheDatabaseInstance.getCacheDatabase;
/*-------------------------------- http cache ------------------------------*/
class CacheHttpInstance {
private static storeAdapter: KeyvStoreAdapter | null = null;
private static createCacheHttp(): Keyv {
return new Keyv({ store: CacheHttpInstance.storeAdapter });
}
public static setAdapter(adapter: KeyvStoreAdapter): void {
CacheHttpInstance.storeAdapter = adapter;
}
public static getCacheHttp(): Keyv {
return Singleton.getInstance<Keyv>('CacheHttpInstance', () =>
CacheHttpInstance.createCacheHttp()
);
}
}
const _httpFileAdapter = new KeyvFile({ filename: cacheHttpFilePath });
const httpLruAdapter = new KeyvLruManagedTtl({ max: cacheHttpLruItems });
CacheHttpInstance.setAdapter(httpLruAdapter);
export const getCacheHttp = CacheHttpInstance.getCacheHttp;
export const cacheDatabaseWrapper = async <T, A extends any[]>(
key: string,
func: (...args: A) => T,
...args: A
): Promise<T> => {
if (!cacheDatabaseDisabled) {
const cachedResult = await getCacheDatabase().get<T>(key);
if (cachedResult) return cachedResult;
}
const dbResult = func(...args);
await getCacheDatabase().set(key, dbResult);
return dbResult;
};
export const getDynamicCacheKey = (key: string, param: string): string =>
Boolean(param) ? `${key}--${param}` : key;