-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
43 lines (36 loc) · 1008 Bytes
/
example.js
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
'use strict';
const Cache = require('./lib/cache');
let cache = Cache.stats.register(new Cache('redis', {maxAge: 30 * 1000, reset: {interval: 100 * 1000}}), 'mycache');
let opts = {
'columns': ['id', 'price']
};
let fetchData = function fetchData(ids, opts, cb) {
let response = {};
ids.forEach((id) => {
response[id] = {
'price': 100 * id,
'id': id
};
});
let t = Math.floor(Math.random() * 5 + 1);
console.log('Responding after Time: ', t, ' For ids: ', ids);
setTimeout(() => {
cb(null, response);
}, t * 1000);
};
let fastFetchData = cache.wrap(function fastFetchData(ids, opts, cb) {
fetchData(ids, opts, (err, data) => {
if(err) {
return cb(err);
}
cb(err, data);
});
});
fastFetchData([123, 124, 125], opts, (err, res) => {
console.log('First Response ', err, res);
setTimeout(() => {
fastFetchData([123, 124, 125, 118, 12], opts, (err, res) => {
console.log('Second Response ', err, res);
});
}, 2 * 1000);
});