1
jhzh
2025-04-20 d157d0892f1ab5517dbe3a08328ccb9c4e446615
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
 * @description 返回数据时提示的消息
 * @param {String} msg 返回的消息
 * @param {Object} data 返回的数据
 * @returns { { msg,data } }
 */
function msg(msg = '请传递消息', data = null) {
    return { msg, data };
}
let map = new Map(),
    storage = uni.getStorageInfoSync(); //所有数据缓存
/**
 * @description 缓存类
 * @class 缓存类class
 */
class Cache {
    /**
     * @constructor
     * @param {Number} [timeout=86400] 缓存时间默认86400秒等于一天,传0为永久存储
     */
    constructor(timeout = 86400) {
        // 把本地缓存数据存入map中
        storage.keys.forEach((key) => map.set(key, uni.getStorageSync(key)));
        this.map = map; //map数据
        this.timeout = timeout;
    }
    /**
     * @description 设置缓存数据
     * @param {String} key 存储的key
     * @param {Object} data 存储的data数据
     * @param {String} [timeout] 缓存时间,传0为永久存储
     * @returns { {msg,data} } 
     */
    set(key, data, timeout = this.timeout) {
        //data = 数据value值,超时时间,加入缓存时间
        Object.assign(data, { createTime: Date.now(), timeout });
        uni.setStorageSync(key, data); //保存到本地缓存
        this.map.set(key, data); //保存到map
        return msg('保存成功', data);
    }
    /**
     * @description 获取缓存数据
     * @param {String} key 存储的key
     * @returns { {msg,data | data:null} } 
     */
    get(key) {
        let value = this.map.get(key); //取值
        if (!value) return msg('没有key值'); //如果没有值,那就就返回空
        // 数据,超时时间,加入缓存时间           现在时间                 时间差(秒)
        let { timeout, createTime, ...data } = value,
            presentTime = Date.now(),
            tdoa = (presentTime - createTime) / 1000;
        // 超出缓存时间,那么就清除缓存返回空
        if (timeout != 0 && tdoa > timeout) {
            uni.removeStorageSync(key);
            this.map.delete(key); //删除map中对应的key值
            return msg('数据过期');
        } else {
            return msg('ok', data);
        }
    }
    /**
     * @description 清除某个缓存数据
     * @param {String} key 存储的key
     * @returns { {msg,data:null} } 
     */
    remove(key) {
        uni.removeStorageSync(key); //删除缓存的数据
        this.map.delete(key); //删除map中对应的key值
        return msg('删除成功');
    }
    /**
     * @description 清除整个缓存数据
     * @returns { {msg,data:null} } 
     */
    clear() {
        uni.clearStorageSync(); //清空缓存的数据
        this.map.clear(); //清空map
        return msg('清空成功');
    }
    /**
     * @description 获取缓存数据大小
     * @param { function } cb - 缓存大小
     */
    getSize(cb) {
        plus.cache.calculate((size) => {
            let sizeCache = parseInt(size);
            let cacheSize;
            if (sizeCache == 0) {
                cacheSize = '0B';
            } else if (sizeCache < 1024) {
                cacheSize = sizeCache + 'B';
            } else if (sizeCache < 1048576) {
                cacheSize = (sizeCache / 1024).toFixed(2) + 'KB';
            } else if (sizeCache < 1073741824) {
                cacheSize = (sizeCache / 1048576).toFixed(2) + 'MB';
            } else {
                cacheSize = (sizeCache / 1073741824).toFixed(2) + 'GB';
            }
            cb(cacheSize);
        });
    }
}
export default new Cache();