Skip to content

Commit d4f8fb3

Browse files
committed
Added Cache
1 parent 9257e58 commit d4f8fb3

File tree

6 files changed

+305
-82
lines changed

6 files changed

+305
-82
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ yarn-debug.log*
66
yarn-error.log*
77
lerna-debug.log*
88
db.js
9+
cache
910

1011

1112
# Diagnostic reports (https://nodejs.org/api/report.html)

example/test2.js

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

44
let nodeUDF = new _udf();
55

66
let {
77
convertToTradingSymbol,
88
validateOrderInput,
9-
download
9+
download,
10+
setgetCache
1011
} = nodeUDF;
1112

1213

@@ -51,9 +52,10 @@ const test2 = async () => {
5152
// const myCache = new NodeCache()
5253

5354
async function test3() {
54-
let x = await nodeUDF.getsetData(['k'], 100);
55+
let cb = new Date()
56+
let x = await setgetCache('k', 10, cb);
5557

5658
console.log(x);
5759
}
5860

59-
test2()
61+
test3()

misc/cache.js

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
1-
const NodeCache = require("node-cache");
2-
3-
class Cache extends NodeCache {
4-
constructor(params) {
5-
super(params);
6-
this.params = params;
7-
}
8-
9-
getData = (keys) => {
10-
let _this = this;
11-
let cache = Array.isArray(keys) ? _this.mget(keys) : _this.get(keys);
1+
const cacheManager = require("cache-manager");
2+
var fsStore = require('cache-manager-fs');
3+
4+
const initCache = () =>
5+
new Promise((resolve, reject) => {
6+
memoryCache = cacheManager.caching({
7+
store: fsStore,
8+
path: 'cache',
9+
ttl: 30,
10+
preventfill: false,
11+
reviveBuffers: false,
12+
fillcallback: data => {
13+
resolve()
14+
}
15+
})
16+
})
1217

13-
// let cache =
1418

15-
// Object.assign(cache, {
16-
// _key: val
17-
// });
18-
// }
1919

20-
return [cache];
20+
class Cache {
21+
constructor(params) {
22+
this.params = params;
2123
}
2224

23-
24-
getsetData = async (arr, ttl, cb, arg) => {
25-
let _this = this;
26-
let all_data = [await _this.mget(arr)];
27-
console.log(_this.keys());
28-
29-
let _ttl = (i) => Array.isArray(ttl) ? ttl[i] : ttl;
30-
let process_data = all_data.map((data, i) => {
31-
if (Object.keys(data).length == 0) {
32-
//get the data
33-
let _data = new Date();
34-
//cb(arg);
35-
36-
//store this
37-
_this.set(arr[i], _data, _ttl(i));
38-
39-
//return data
40-
return _data;
41-
} else {
42-
return data;
43-
}
44-
})
45-
console.log(_this.keys());
46-
47-
return process_data;
25+
setgetCache = async (key, ttl, cb) => {
26+
try {
27+
await initCache();
28+
return memoryCache.wrap(key, async () => await eval(cb), {
29+
ttl: ttl
30+
});
31+
} catch (err) {
32+
return Promise.reject(err);
33+
}
4834
}
4935
}
5036

0 commit comments

Comments
 (0)