Skip to content

Commit c19aa42

Browse files
committed
bugs fixes
1 parent d4f8fb3 commit c19aa42

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed

example/fn.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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')

example/test2.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
const _udf = require('../index.js');
22
// const conn = require('./db.js');
33

4+
const cacheManager = require("cache-manager");
5+
var fsStore = require('cache-manager-fs');
6+
47
let nodeUDF = new _udf();
58

69
let {
710
convertToTradingSymbol,
811
validateOrderInput,
912
download,
10-
setgetCache
13+
getsetCache
1114
} = nodeUDF;
1215

1316

@@ -52,10 +55,57 @@ const test2 = async () => {
5255
// const myCache = new NodeCache()
5356

5457
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);
57107

58108
console.log(x);
59109
}
60110

61-
test3()
111+
test4()

misc/cache.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const cacheManager = require("cache-manager");
22
var fsStore = require('cache-manager-fs');
33

4-
const initCache = () =>
4+
const _initCache = () =>
55
new Promise((resolve, reject) => {
66
memoryCache = cacheManager.caching({
77
store: fsStore,
@@ -15,23 +15,25 @@ const initCache = () =>
1515
})
1616
})
1717

18-
19-
2018
class Cache {
2119
constructor(params) {
2220
this.params = params;
2321
}
2422

25-
setgetCache = async (key, ttl, cb) => {
23+
initCache = () => _initCache();
24+
25+
getsetCache = async (key, ttl, cb) => {
26+
let _this = this;
2627
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(), {
2930
ttl: ttl
3031
});
3132
} catch (err) {
32-
return Promise.reject(err);
33+
throw new Error(err);
3334
}
3435
}
36+
3537
}
3638

3739
module.exports = Cache;

0 commit comments

Comments
 (0)