-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.js
79 lines (66 loc) · 2.12 KB
/
client.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-disable no-console */
const Cache = require('../');
const path = require('path');
/**
* Wait an arbitrary number of milliseconds before resolving. This is super
* helpful for reliably testing long, tedious network requests in async functions
*
* @param {Number} ms number of milliseconds to wait
* @returns {Promise} Promise resolves after a ms milliseconds
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
class APIClient {
constructor() {
const options = {
maxAge: 10 * 60 * 1000, // cache items for 10 minutes
maxStaleness: 60 * 60 * 1000, // Still serve expired cache items for 1 hour
fsCachePath: path.join(__dirname, '.cache') // fallback to a file-based cash located in .cache
};
this.cache = new Cache(options);
}
/**
* Get some data from an API
*
* @async
* @returns {Promise<void>} a Promise that resolves when we are finished getting data from the "API".
*/
async get() {
const now = Date.now();
const skipCache = {}; // don't skip the cache, if you ALWAYS wanted to get new data you can use the following line
// const skipCache = { skipCache: true }
const airShieldLockCode = await this.cache.get('data', skipCache, this.getAirShieldLockCode);
console.log(`Fetched ${JSON.stringify(airShieldLockCode)} in ${Date.now() - now}ms`);
}
/**
* Reset the cache in case we need to clear the current data.
* @returns {Promise} [description]
*/
async reset() {
await this.cache.reset();
}
/**
* @returns {Promise<number>} Promise that resolves to data from an "API".
* @async
*/
async getAirShieldLockCode() {
// simulate a long, slow process that can be cached
await sleep(2 * 1000);
return { code: 123465, planet: 'Druidia' };
}
}
async function makeRequests() {
const client = new APIClient();
// will be slow
await client.get();
// will now be much faster now that we've cached the response
await client.get();
await client.get();
await client.get();
// reset the cache
await client.reset();
// will be slow again
await client.get();
}
makeRequests();