-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
48 lines (39 loc) · 1.11 KB
/
cache.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
import path from 'path';
import dotenv from 'dotenv';
import level from 'level';
dotenv.config();
const db = level(path.join(process.cwd(), 'store.db'));
export async function wrap(key, fn, ttl /* seconds */) {
try {
const res = await db.get(key);
const data = JSON.parse(res);
const now = +new Date();
if (data.expires >= now) {
return data.data;
}
} catch (e) {
if (e.name !== 'NotFoundError') {
throw e;
}
}
// could not find cached data, fetch it
const newValue = await fn();
if (newValue === undefined) {
await db.put(key, JSON.stringify({
expires: 0,
}));
return undefined;
}
await db.put(key, JSON.stringify({
data: newValue,
expires: (+new Date()) + (ttl * 1000),
}));
// stringify and re-parse so we get the same result as if we had just pulled from the cache
return JSON.parse(JSON.stringify(newValue));
}
export async function get(...args) {
return db.get(...args);
}
export async function put(...args) {
return db.put(...args);
}