const dayjs = require('dayjs');

/**
 *
 * @param {*} key
 * @param {*} value
 */
function setCache(key, value) {
  let param;
  if (!getCache(key)) {
    param = {
      createTime: dayjs().format('YYYY/MM/DD HH:mm:ss'),
      updateTime: '',
      type: typeof value,
      value: value,
    };
  } else {
    param = uni.getStorageSync(key);
    param.type = typeof value;
    param.value = value;
    param.updateTime = dayjs().format('YYYY/MM/DD HH:mm:ss');
  }
  uni.setStorageSync(key, param);
}

/**
 *
 * @param {*} key
 * @returns
 */
function getCache(key) {
  const { value } = uni.getStorageSync(key);
  return value;
}

/**
 *
 * @param {*} key
 */
function removeCache(key) {
  uni.removeStorageSync(key);
}

/**
 *
 * @param {*} key
 */
function clearCache(key) {
  uni.clearStorageSync(key);
}

export { setCache, getCache, removeCache, clearCache };
