-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1.3 KB
/
Copy pathindex.js
File metadata and controls
43 lines (36 loc) · 1.3 KB
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
const days = (num) => (key, entry) => Date.now() - entry.last > (24 * 60 * 60 * 1000 * num);
const hours = (num) => (key, entry) => Date.now() - entry.last > (60 * 60 * 1000 * num);
const minutes = (num) => (key, entry) => Date.now() - entry.last > (60 * 1000 * num);
const seconds = (num) => (key, entry) => Date.now() - entry.last > (1000 * num);
const count = (num) => (key, entry) => entry.callCount >= num;
const copy = (o) => Object.freeze({ ...o });
const prune = (cache, evict) => Object.entries(cache)
.filter(([args, entry]) => evict(copy(args), copy(entry), copy(cache)))
.forEach(([args, _]) => delete cache[args]);
const cache = (f, evict = minutes(1), pruneInterval = 1000 * 60 * 5) => {
const c = {};
setInterval(prune, pruneInterval, c, evict);
return (...args) => {
const entry = c.hasOwnProperty(args) && c[args];
if (!entry || evict(copy(args), copy(entry), copy(c))) {
c[args] = {
value: f(...args),
first: Date.now(),
last: Date.now(),
callCount: 1,
};
} else {
entry.last = Date.now();
entry.callCount++;
}
return c[args].value;
};
};
module.exports = {
cache,
days,
hours,
minutes,
seconds,
count,
};