15815213711
2022-03-07 56f8b51c26bd1fb7e1fdc62acab5151cdf83c860
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
interface cacheI {
    set(key: string, value: string): void;
 
    get(key: string): string | null;
 
    setJSON(key: string, jsonValue: any): void;
 
    getJSON(key: string): any | undefined;
 
    remove(key: string): void;
}
 
const sessionCache: cacheI = {
    get(key: string): string | null {
        return sessionStorage?.getItem(key);
    },
    getJSON(key: string): any {
        let value = this.get(key);
        return value ? JSON.parse(value) : null
    },
    remove(key: string): void {
        sessionStorage.removeItem(key)
    },
    set(key: string, value: string): void {
        sessionStorage?.setItem(key, value)
    },
    setJSON(key: string, jsonValue: any): void {
        this.set(key, JSON.stringify(jsonValue))
    }
}
const localCache: cacheI = {
    get(key: string): string | null {
        return localStorage?.getItem(key);
    },
    getJSON(key: string): any {
        let value = this.get(key);
        return value ? JSON.parse(value) : null
    },
    remove(key: string): void {
        localStorage.removeItem(key)
    },
    set(key: string, value: string): void {
        localStorage?.setItem(key, value)
    },
    setJSON(key: string, jsonValue: any): void {
        this.set(key, JSON.stringify(jsonValue))
    }
}
 
export default {
    /**
     * 会话级缓存
     */
    session: sessionCache,
    /**
     * 本地缓存
     */
    local: localCache
}