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
|
}
|