javascript封装cache工具类

/**
 * 设置缓存
 */
export const SET_CACHE = (key,value) => {
  // 将数组、对象类型的数据转化为 JSON 字符串进行存储
  if (typeof value === 'object') {
    value = JSON.stringify(value)
  }
  window.localStorage.setItem(key,value)
}

/**
 * 获取缓存
 */
export const GET_CACHE = key => {
  // 处理复杂类型的数据,基本类型的数据直接返回
  const data = window.localStorage.getItem(key)
  try {
    return JSON.parse(data)
  } catch (err) {
    return data
  }
}

/**
 * 清除制定缓存
  */
 export const CLEAR_ITEM = key => {
   window.localStorage.removeItem(key)
 }


/**
 * 清除所有缓存
 */
export const CLEAR = () => {
  window.localStorage.clear()
}
posted @ 2022-01-21 20:23  杨凌云的博客  阅读(146)  评论(0)    收藏  举报