You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.1 KiB
58 lines
1.1 KiB
4 weeks ago
|
/**
|
||
|
* 缓存工具类
|
||
|
*/
|
||
|
class Cache {
|
||
|
/**
|
||
|
* 本地缓存类
|
||
|
*/
|
||
|
local = {
|
||
|
set(key, value) {
|
||
|
window.localStorage.setItem(key, JSON.stringify(value));
|
||
|
},
|
||
|
get(key) {
|
||
|
const value = window.localStorage.getItem(key);
|
||
|
if (value) {
|
||
|
try {
|
||
|
return JSON.parse(value);
|
||
|
} catch (error) {
|
||
|
return value;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
},
|
||
|
remove(key) {
|
||
|
window.localStorage.removeItem(key);
|
||
|
},
|
||
|
clear() {
|
||
|
window.localStorage.clear();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 会话缓存类
|
||
|
*/
|
||
|
session = {
|
||
|
set(key, value) {
|
||
|
window.sessionStorage.setItem(key, JSON.stringify(value));
|
||
|
},
|
||
|
get(key) {
|
||
|
const value = window.sessionStorage.getItem(key);
|
||
|
if (value) {
|
||
|
try {
|
||
|
return JSON.parse(value);
|
||
|
} catch (error) {
|
||
|
return value;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
},
|
||
|
remove(key) {
|
||
|
window.sessionStorage.removeItem(key);
|
||
|
},
|
||
|
clear() {
|
||
|
window.sessionStorage.clear();
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export default new Cache();
|