-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
expire.js
59 lines (49 loc) · 1.2 KB
/
expire.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
var namespace = 'expire_mixin'
module.exports = expirePlugin
function expirePlugin() {
var expirations = this.createStore(this.storage, null, this._namespacePrefix+namespace)
return {
set: expire_set,
get: expire_get,
remove: expire_remove,
getExpiration: getExpiration,
removeExpiredKeys: removeExpiredKeys
}
function expire_set(super_fn, key, val, expiration) {
if (!this.hasNamespace(namespace)) {
expirations.set(key, expiration)
}
return super_fn()
}
function expire_get(super_fn, key) {
if (!this.hasNamespace(namespace)) {
_checkExpiration.call(this, key)
}
return super_fn()
}
function expire_remove(super_fn, key) {
if (!this.hasNamespace(namespace)) {
expirations.remove(key)
}
return super_fn()
}
function getExpiration(_, key) {
return expirations.get(key)
}
function removeExpiredKeys(_) {
var keys = []
this.each(function(val, key) {
keys.push(key)
})
for (var i=0; i<keys.length; i++) {
_checkExpiration.call(this, keys[i])
}
}
function _checkExpiration(key) {
var expiration = expirations.get(key, Number.MAX_VALUE)
if (expiration <= new Date().getTime()) {
this.raw.remove(key)
expirations.remove(key)
}
}
}