-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcache.js
More file actions
47 lines (41 loc) · 866 Bytes
/
cache.js
File metadata and controls
47 lines (41 loc) · 866 Bytes
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
var cache = {}
var CACHE_TTL = 0
var cron = require('node-cron')
cron.schedule('45 * * * *', function() {
console.log('every hour')
let now = new Date().getTime()
for (var item in cache) {
if (now > cache[item].ts + (CACHE_TTL * 1000)) {
delete cache[item]
console.log('[CACHE] removed: ', item)
}
}
})
function setTTL(TTL) {
CACHE_TTL = TTL
}
function hashkey (url) {
return url
let shasum = crypto.createHash('sha1')
shasum.update(url)
return shasum.digest('hex')
}
function save( meta, body) {
const key = hashkey(meta.finalUrl)
cache[key] = {}
cache[key].body = body
cache[key].ts = new Date().getTime()
}
function load(url) {
const key = hashkey(url)
if (key in cache) {
return cache[key].body
}
return null
}
module.exports = {
setTTL: setTTL,
hashkey: hashkey,
save: save,
load: load
};