File tree Expand file tree Collapse file tree 3 files changed +115
-11
lines changed Expand file tree Collapse file tree 3 files changed +115
-11
lines changed Original file line number Diff line number Diff line change
1
+ const cacheManager = require ( "cache-manager" ) ;
2
+ let fsStore = require ( 'cache-manager-fs' ) ;
3
+
4
+ const _cache = require ( '../misc/cache.js' ) ;
5
+ let cache = new _cache ( ) ;
6
+
7
+ const initCache2 = ( ) =>
8
+ new Promise ( ( resolve , reject ) => {
9
+ memoryCache = cacheManager . caching ( {
10
+ store : fsStore ,
11
+ path : 'cache' ,
12
+ ttl : 30 ,
13
+ preventfill : false ,
14
+ reviveBuffers : false ,
15
+ fillcallback : data => {
16
+ resolve ( )
17
+ }
18
+ } )
19
+ } )
20
+
21
+
22
+ getsetCache2 = async ( key , ttl , cb ) => {
23
+ let _this = this ;
24
+ try {
25
+ await initCache2 ( ) ;
26
+ return memoryCache . wrap ( key , async ( ) => await cb ( ) , {
27
+ ttl : ttl
28
+ } ) ;
29
+ } catch ( err ) {
30
+ throw new Error ( err ) ;
31
+ }
32
+ }
33
+
34
+
35
+ let cb = ( ) => {
36
+ console . log ( 'called' ) ;
37
+ return new Date ( ) ;
38
+ }
39
+
40
+ async function test_class ( key ) {
41
+ let x = await cache . getsetCache ( key , 10 , cb ) ;
42
+ console . log ( key , x ) ;
43
+ }
44
+
45
+ async function test_fn ( key ) {
46
+ let x = await getsetCache2 ( key , 10 , cb ) ;
47
+ console . log ( key , x ) ;
48
+ }
49
+
50
+
51
+ test_class ( 'test_class' ) ;
52
+ test_fn ( 'test_fn' )
Original file line number Diff line number Diff line change 1
1
const _udf = require ( '../index.js' ) ;
2
2
// const conn = require('./db.js');
3
3
4
+ const cacheManager = require ( "cache-manager" ) ;
5
+ var fsStore = require ( 'cache-manager-fs' ) ;
6
+
4
7
let nodeUDF = new _udf ( ) ;
5
8
6
9
let {
7
10
convertToTradingSymbol,
8
11
validateOrderInput,
9
12
download,
10
- setgetCache
13
+ getsetCache
11
14
} = nodeUDF ;
12
15
13
16
@@ -52,10 +55,57 @@ const test2 = async () => {
52
55
// const myCache = new NodeCache()
53
56
54
57
async function test3 ( ) {
55
- let cb = new Date ( )
56
- let x = await setgetCache ( 'k' , 10 , cb ) ;
58
+ let cb = ( ) => {
59
+ console . log ( 'called' ) ;
60
+ return new Date ( ) ;
61
+ }
62
+
63
+ let x = await getsetCache ( 'k' , 10 , cb ) ;
64
+
65
+ console . log ( x ) ;
66
+ }
67
+
68
+ test3 ( )
69
+
70
+ const initCache2 = ( ) =>
71
+ new Promise ( ( resolve , reject ) => {
72
+ memoryCache = cacheManager . caching ( {
73
+ store : fsStore ,
74
+ path : 'cache' ,
75
+ ttl : 30 ,
76
+ preventfill : false ,
77
+ reviveBuffers : false ,
78
+ fillcallback : data => {
79
+ resolve ( )
80
+ }
81
+ } )
82
+ } )
83
+
84
+
85
+ getsetCache2 = async ( key , ttl , cb ) => {
86
+ let _this = this ;
87
+ try {
88
+ await _this . initCache ( ) ;
89
+ return memoryCache . wrap ( key , async ( ) => await cb ( ) , {
90
+ ttl : ttl
91
+ } ) ;
92
+ } catch ( err ) {
93
+ throw new Error ( err ) ;
94
+ }
95
+ }
96
+
97
+
98
+
99
+ async function test4 ( ) {
100
+ let cb = ( ) => {
101
+ console . log ( 'called' ) ;
102
+ return new Date ( ) ;
103
+ }
104
+
105
+ await initCache2 ( ) ;
106
+ let x = await getsetCache2 ( 'k' , 10 , cb ) ;
57
107
58
108
console . log ( x ) ;
59
109
}
60
110
61
- test3 ( )
111
+ test4 ( )
Original file line number Diff line number Diff line change 1
1
const cacheManager = require ( "cache-manager" ) ;
2
2
var fsStore = require ( 'cache-manager-fs' ) ;
3
3
4
- const initCache = ( ) =>
4
+ const _initCache = ( ) =>
5
5
new Promise ( ( resolve , reject ) => {
6
6
memoryCache = cacheManager . caching ( {
7
7
store : fsStore ,
@@ -15,23 +15,25 @@ const initCache = () =>
15
15
} )
16
16
} )
17
17
18
-
19
-
20
18
class Cache {
21
19
constructor ( params ) {
22
20
this . params = params ;
23
21
}
24
22
25
- setgetCache = async ( key , ttl , cb ) => {
23
+ initCache = ( ) => _initCache ( ) ;
24
+
25
+ getsetCache = async ( key , ttl , cb ) => {
26
+ let _this = this ;
26
27
try {
27
- await initCache ( ) ;
28
- return memoryCache . wrap ( key , async ( ) => await eval ( cb ) , {
28
+ await _this . initCache ( ) ;
29
+ return memoryCache . wrap ( key , async ( ) => await cb ( ) , {
29
30
ttl : ttl
30
31
} ) ;
31
32
} catch ( err ) {
32
- return Promise . reject ( err ) ;
33
+ throw new Error ( err ) ;
33
34
}
34
35
}
36
+
35
37
}
36
38
37
39
module . exports = Cache ;
You can’t perform that action at this time.
0 commit comments