/**
 * 设置缓存,持久化到本地
 * @param {String} key 缓存key
 * @param {Object} value 缓存数据
 */
export const setCache = (key, value) => {
    uni.setStorageSync(key, value);
}

/**
 * 获取缓存
 * @param {String} key 缓存key
 * @returns {Object} 缓存数据
 */
export const getCache = (key) => {
    return uni.getStorageSync(key);
}

/**
 * 删除缓存
 * @param {String} key 缓存key
 */
export const removeCache = (key) => {
    uni.removeStorageSync(key);
}

/**
 * 本地缓存
 */
let localCache = {};

/**
 * 设置缓存，非持久化
 * @param {String} key 缓存key
 * @param {Object} value 缓存数据
 * @param {Number} time 缓存时间，秒
 */
export const setLocalStorage = (key, value, time = 0) => {
    localCache[key] = {
        value: value,
        expireTime: time > 0 ? Date.now() + time * 1000 : 0,
        createTime: Date.now()
    }
}

/**
 * 获取缓存，非持久化
 * @param {String} key 缓存key
 * @returns {Object} 缓存数据
 */
export const getLocalStorage = (key) => {
    if (localCache[key]) {
        if (localCache[key].expireTime > 0 && localCache[key].expireTime > Date.now()) {
            return localCache[key].value;
        }
    }
    return null;
}

/**
 * 删除缓存，非持久化
 * @param {String} key 缓存key
 */
export const removeLocalStorage = (key) => {
    delete localCache[key];
}