forked from brainfoolong/web-ftp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
41 lines (36 loc) · 746 Bytes
/
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
'use strict'
/**
* Simple cached value management
*/
const cache = {}
/**
* The values
* @type {{}}
* @private
*/
cache._values = {}
/**
* Get a value from cache
* @param {string} key
* @returns {*}
*/
cache.get = function (key) {
if (typeof cache._values[key] === 'undefined') {
return null
}
if (cache._values[key].time > new Date().getTime() / 1000) {
return cache._values[key].value
}
delete cache._values[key]
return null
}
/**
* Set a value in cache
* @param {string} key
* @param {*} value
* @param {number} lifetime Lifetime in seconds
*/
cache.set = function (key, value, lifetime) {
cache._values[key] = {'value': value, 'time': new Date().getTime() / 1000 + lifetime}
}
module.exports = cache