zhaoxing
2022-08-22 74bb50ce93d0c9cd20a0b84f63db46f433a39e91
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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();
    }
};