1
1
import fs from 'fs' ;
2
2
3
- import Keyv from 'keyv' ;
3
+ import Keyv , { KeyvStoreAdapter } from 'keyv' ;
4
4
import KeyvFile from 'keyv-file' ;
5
5
6
6
import { Singleton } from '@/utils/singleton' ;
7
7
import { SERVER_CONFIG } from '@/config/server' ;
8
8
9
- const { cacheHttpFilePath, cacheDatabaseFilePath, cacheDatabaseDisabled } = SERVER_CONFIG ;
9
+ const { KeyvLruManagedTtl } = require ( 'keyv-lru' ) ;
10
+
11
+ const {
12
+ cacheHttpFilePath,
13
+ cacheDatabaseFilePath,
14
+ cacheDatabaseLruItems,
15
+ cacheHttpLruItems,
16
+ cacheDatabaseDisabled,
17
+ } = SERVER_CONFIG ;
10
18
11
19
// deletes cache for testing
12
20
try {
@@ -15,11 +23,14 @@ try {
15
23
} catch ( error ) { }
16
24
17
25
class CacheDatabaseInstance {
26
+ private static storeAdapter : KeyvStoreAdapter | null = null ;
27
+
18
28
private static createCacheDatabase ( ) : Keyv {
19
- // ttl: undefined - infinite duration
20
- return new Keyv ( {
21
- store : new KeyvFile ( { filename : cacheDatabaseFilePath } ) ,
22
- } ) ;
29
+ return new Keyv ( { store : CacheDatabaseInstance . storeAdapter } ) ;
30
+ }
31
+
32
+ public static setAdapter ( adapter : KeyvStoreAdapter ) : void {
33
+ CacheDatabaseInstance . storeAdapter = adapter ;
23
34
}
24
35
25
36
public static getCacheDatabase ( ) : Keyv {
@@ -29,12 +40,23 @@ class CacheDatabaseInstance {
29
40
}
30
41
}
31
42
43
+ // ttl: undefined - infinite duration
44
+ const _databaseFileAdapter = new KeyvFile ( { filename : cacheDatabaseFilePath } ) ;
45
+
46
+ const databaseLruAdapter = new KeyvLruManagedTtl ( { max : cacheDatabaseLruItems } ) ;
47
+ CacheDatabaseInstance . setAdapter ( databaseLruAdapter ) ;
48
+
32
49
export const getCacheDatabase = CacheDatabaseInstance . getCacheDatabase ;
50
+
33
51
class CacheHttpInstance {
52
+ private static storeAdapter : KeyvStoreAdapter | null = null ;
53
+
34
54
private static createCacheHttp ( ) : Keyv {
35
- return new Keyv ( {
36
- store : new KeyvFile ( { filename : cacheHttpFilePath } ) ,
37
- } ) ;
55
+ return new Keyv ( { store : CacheHttpInstance . storeAdapter } ) ;
56
+ }
57
+
58
+ public static setAdapter ( adapter : KeyvStoreAdapter ) : void {
59
+ CacheHttpInstance . storeAdapter = adapter ;
38
60
}
39
61
40
62
public static getCacheHttp ( ) : Keyv {
@@ -44,6 +66,11 @@ class CacheHttpInstance {
44
66
}
45
67
}
46
68
69
+ const _httpFileAdapter = new KeyvFile ( { filename : cacheHttpFilePath } ) ;
70
+
71
+ const httpLruAdapter = new KeyvLruManagedTtl ( { max : cacheHttpLruItems } ) ;
72
+ CacheHttpInstance . setAdapter ( httpLruAdapter ) ;
73
+
47
74
export const getCacheHttp = CacheHttpInstance . getCacheHttp ;
48
75
49
76
export const cacheDatabaseWrapper = async < T , A extends any [ ] > (
0 commit comments