merge test
This commit is contained in:
53
src/utils/cache.js
Normal file
53
src/utils/cache.js
Normal file
@@ -0,0 +1,53 @@
|
||||
let timers = [];
|
||||
|
||||
export default {
|
||||
|
||||
exists(key) {
|
||||
const _ = localStorage.getItem('stored_keys');
|
||||
return _ ? _.split(',')?.indexOf(key) != -1 : false;
|
||||
},
|
||||
|
||||
store(key, value, duration = null) {
|
||||
if (!localStorage.getItem('stored_keys')) localStorage.setItem('stored_keys', key);
|
||||
else localStorage.setItem('stored_keys', localStorage.getItem('stored_keys') + ',' + key);
|
||||
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
console.log('store duration', duration)
|
||||
if (duration) {
|
||||
if (timers[key]) clearTimeout(timers[key]);
|
||||
timers[key] = setTimeout(() => {
|
||||
console.log("clear cache", key)
|
||||
this.clear([key]);
|
||||
}, duration * 1000);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} key
|
||||
* @param {*} cb
|
||||
* @param {*} param2 force 强制更新,调用cb
|
||||
* @returns
|
||||
*/
|
||||
async getList(key, cb = null, { force = false, duration = null } = {}) {
|
||||
if (this.exists(key) && !force) {
|
||||
return JSON.parse(localStorage.getItem(key))
|
||||
} else {
|
||||
const list = await cb();
|
||||
this.store(key, list, duration);
|
||||
return list;
|
||||
}
|
||||
},
|
||||
|
||||
clear(keys) {
|
||||
if (keys && keys.length) {
|
||||
let stored_keys = localStorage.getItem('stored_keys').split(',');
|
||||
keys.forEach((key) => {
|
||||
stored_keys = stored_keys.filter((item) => item != key);
|
||||
});
|
||||
localStorage.setItem('stored_keys', stored_keys);
|
||||
return;
|
||||
}
|
||||
localStorage.removeItem('stored_keys');
|
||||
},
|
||||
}
|
||||
@@ -217,7 +217,7 @@ export function getTime(type) {
|
||||
export function debounce(func, wait, immediate) {
|
||||
let timeout, args, context, timestamp, result
|
||||
|
||||
const later = function() {
|
||||
const later = function () {
|
||||
// 据上一次触发时间间隔
|
||||
const last = +new Date() - timestamp
|
||||
|
||||
@@ -234,7 +234,7 @@ export function debounce(func, wait, immediate) {
|
||||
}
|
||||
}
|
||||
|
||||
return function(...args) {
|
||||
return function (...args) {
|
||||
context = this
|
||||
timestamp = +new Date()
|
||||
const callNow = immediate && !timeout
|
||||
@@ -438,3 +438,7 @@ export function toCamelCase(str, upperCaseFirst) {
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
export function IdToName(targetId, source, prop = 'name') {
|
||||
return source.find(item => item.id === targetId)?.[prop]
|
||||
}
|
||||
Reference in New Issue
Block a user