-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheMetadata.js
62 lines (50 loc) · 1.03 KB
/
cacheMetadata.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
const cacache = require('cacache');
const state = require('./state');
let _untouched = {};
async function put(key, contents) {
if (!state.cache) {
return;
}
await cacache.put(state.cache, key, contents);
}
async function get(key) {
if (!state.cache) {
return;
}
if(_untouched[key]) {
delete _untouched[key];
}
try {
const cacheItem = await cacache.get(state.cache, key);
return cacheItem.data;
} catch (ex) {
// cache misses throw an exception
if (ex.code === 'ENOENT') {
return undefined;
}
throw ex;
}
}
async function trackUntouched() {
if (!state.cache) {
return;
}
_untouched = await cacache.ls(state.cache);
}
async function rmUntouched() {
if (!state.cache) {
return;
}
const keysToRm = Object.keys(_untouched);
_untouched = {};
for (const key of keysToRm) {
console.log(`Cache entry for ${key} appears to be unused. Removing.`);
await cacache.rm(state.cache, key);
}
}
module.exports = {
put,
get,
trackUntouched,
rmUntouched,
};