1.设置缓存-有效期8小时,
let setLocal = (key, value) => {
var date = new Date().getTime();
const obj = {
data: value,
time: Date.now(),
expire: date + 1000 * 60 * 60 * 8
};
localStorage.setItem(key, JSON.stringify(obj));
}
2. 拿到缓存
let getLocal = (key) => {
if (localStorage.getItem(key) != null) {
var data = JSON.parse(localStorage.getItem(key));
if (data !== null) {
if (data.expire != null && data.expire < new Date().getTime()) {
localStorage.removeItem(key);
} else {
return data.data;
}
}
}
return null;
}
3.移出缓存
let removeLocal = (key) => {
localStorage.removeItem(key);
}
4.移出全部缓存
let clearLocal = () => {
localStorage.clear();
}