forked from totaljs/framework5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
58 lines (48 loc) · 1.16 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Total.js Cache (in-memory)
// The MIT License
// Copyright 2012-2023 (c) Peter Širka <petersirka@gmail.com>
'use strict';
exports.set = function(key, value, expire = '5 minutes') {
global.NOW = new Date();
F.temporary.cache[key] = { value: value, expire: global.NOW.add(expire) };
};
exports.read = exports.get = function(key, def) {
var item = F.temporary.cache[key];
global.NOW = new Date();
if (item && item.expire > global.NOW)
return item.value;
else
return def;
};
exports.remove = function(key) {
if (F.temporary.cache[key])
delete F.temporary.cache[key];
};
exports.reset = function(search) {
var isreg = search instanceof RegExp;
var count = 0;
if (search) {
for (let key in F.temporary.cache) {
if (isreg) {
if (search.test(key)) {
count++;
delete F.temporary.cache[key];
}
} else if (search) {
if (key.indexOf(search) !== -1) {
count++;
delete F.temporary.cache[key];
}
}
}
} else
F.temporary.cache = {};
return count;
};
exports.refresh = function() {
for (let key in F.temporary.cache) {
let item = F.temporary.cache[key];
if (item.expire < NOW)
delete F.temporary.cache[key];
}
};