You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
50 lines
1.1 KiB
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
const cache = {
|
|
key: "app_",
|
|
//设置缓存(expire为缓存时效)
|
|
set(key, value, expire) {
|
|
key = this.getKey(key);
|
|
let data = {
|
|
expire: expire ? this.time() + expire : "",
|
|
value
|
|
};
|
|
if (typeof data === "object") {
|
|
data = JSON.stringify(data);
|
|
}
|
|
try {
|
|
common_vendor.index.setStorageSync(key, data);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
},
|
|
get(key) {
|
|
key = this.getKey(key);
|
|
try {
|
|
const data = common_vendor.index.getStorageSync(key);
|
|
if (!data) {
|
|
return null;
|
|
}
|
|
const { value, expire } = JSON.parse(data);
|
|
if (expire && expire < this.time()) {
|
|
common_vendor.index.removeStorageSync(key);
|
|
return null;
|
|
}
|
|
return value;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
},
|
|
//获取当前时间
|
|
time() {
|
|
return Math.round(new Date().getTime() / 1e3);
|
|
},
|
|
remove(key) {
|
|
key = this.getKey(key);
|
|
common_vendor.index.removeStorageSync(key);
|
|
},
|
|
getKey(key) {
|
|
return this.key + key;
|
|
}
|
|
};
|
|
exports.cache = cache;
|
|
|