storage.js
1.6 KB
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
63
"use strict";
const common_vendor = require("../common/vendor.js");
const postfix = "_expiry";
const storage = {
/**
* 设置缓存
* @param {[type]} k [键名]
* @param {[type]} v [键值]
* @param {[type]} t [时间、单位秒]
*/
set(k, v, t) {
common_vendor.index.setStorageSync(k, v);
const seconds = parseInt(t);
if (seconds > 0) {
let timestamp = Date.parse(/* @__PURE__ */ new Date());
timestamp = timestamp / 1e3 + seconds;
common_vendor.index.setStorageSync(k + postfix, timestamp + "");
} else {
common_vendor.index.removeStorageSync(k + postfix);
}
},
/**
* 获取缓存
* @param {[type]} k [键名]
* @param {[type]} def [获取为空时默认]
*/
get(k, def) {
const deadtime = parseInt(common_vendor.index.getStorageSync(k + postfix));
if (deadtime) {
if (parseInt(deadtime) < Date.parse(/* @__PURE__ */ new Date()) / 1e3) {
if (def) {
return def;
} else {
return false;
}
}
}
const res = common_vendor.index.getStorageSync(k);
if (res) {
return res;
}
if (def == void 0 || def == "") {
def = false;
}
return def;
},
/**
* 删除指定缓存
* @param {Object} k
*/
remove(k) {
common_vendor.index.removeStorageSync(k);
common_vendor.index.removeStorageSync(k + postfix);
},
/**
* 清理所有缓存
* @return {[type]} [description]
*/
clear() {
common_vendor.index.clearStorageSync();
}
};
exports.storage = storage;