automated_admin/src/cool/utils/storage.ts

82 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-01-09 16:13:14 +08:00
import store from "store";
export default {
// 后缀标识
suffix: "_deadtime",
/**
*
* @param {string} key
*/
get(key: string) {
return store.get(key);
},
/**
*
*/
info() {
const d: any = {};
store.each(function (value: any, key: any) {
d[key] = value;
});
return d;
},
/**
*
* @param {string} key
* @param {*} value
* @param {number} expires
*/
set(key: string, value: any, expires?: any) {
store.set(key, value);
if (expires) {
store.set(`${key}${this.suffix}`, Date.parse(String(new Date())) + expires * 1000);
}
},
/**
*
* @param {string} key
*/
isExpired(key: string) {
return (this.getExpiration(key) || 0) - Date.parse(String(new Date())) <= 2000;
},
/**
*
* @param {string} key
*/
getExpiration(key: string) {
return this.get(key + this.suffix);
},
/**
*
* @param {string} key
*/
remove(key: string) {
store.remove(key);
this.removeExpiration(key);
},
/**
*
* @param {string} key
*/
removeExpiration(key: string) {
store.remove(key + this.suffix);
},
/**
*
*/
clearAll() {
store.clearAll();
}
};