小殊90
小殊90
  • 发布:2021-01-18 14:56
  • 更新:2021-03-30 17:22
  • 阅读:9661

uni-app设置缓存时间 指定缓存时间过期自动移除

分类:uni-app
let cacheMap =  new Map()  
let timeoutDefault = 86400 // 设置默认超时时间,单位秒 3600*24  

function isTimeout (name) {  
  const data = cacheMap.get(name)  
  if (!data) return true  
  if (data.timeout === 0) return false   
  const currentTime = Date.now()  
  const overTime = (currentTime - data.createTime) / 1000  
  if (overTime > data.timeout) {  
    cacheMap.delete(name)  
    if (name.startsWith('_')) {  
      try {  
        uni.removeStorageSync(name)  
      } catch (e) {  
        console.log(e)  
      }  
    }  
    return true  
  }  
  return false  
}  

class CacheCell {  
  constructor (data, timeout) {  
    this.data = data  
    this.timeout = timeout // 设置超时时间,单位秒  
    this.createTime = Date.now() // 对象创建时候的时间  
  }  
}  

class MinCache {  
  constructor (timeout) {  
    try {  
      const res = uni.getStorageInfoSync()  
      res.keys.forEach(name => {  
        try {  
          const value = uni.getStorageSync(name)  
          cacheMap.set(name, value)  
        } catch (e) {  
          console.log(e)  
        }  
      })  
    } catch (e) {  
      console.log(e)  
    }  
    timeoutDefault = timeout  
  }  

  /**  
   * 设置缓存数据  
   * @param {Any} name 缓存的key, 以下划线命名表示永久缓存到Storage  
   * @param {Any} data 缓存的数据  
   * @param {Number} timeout 缓存时间(秒),默认86400秒即一天,0表示永久缓存  
   */  
  set (name, data, timeout = timeoutDefault) {  
    const cachecell = new CacheCell(data, timeout)  
    let cache = null  
    if (name.startsWith('_')) {  
      try {  
        uni.setStorageSync(name, cachecell)  
        cache = cacheMap.set(name, cachecell)  
      } catch (e) {  
        console.log(e)  
      }  
    } else {  
      cache = cacheMap.set(name, cachecell)  
    }  
    return cache  
  }  
  get (name) {  
    return isTimeout(name) ? null : cacheMap.get(name).data  
  }  
  delete (name) {  
    let value = false  
    if (name.startsWith('_')) {  
      try {  
        uni.removeStorageSync(name)  
        value = cacheMap.delete(name)  
      } catch (e) {  
        console.log(e)  
      }  
    } else {  
      value = cacheMap.delete(name)  
    }  
    return value  
  }  
  has (name) {  
    return !isTimeout(name)  
  }  
  clear () {  
    let value = false  
    try {  
      uni.clearStorageSync()  
      cacheMap.clear()  
      value = true  
    } catch (e) {  
      console.log(e)  
    }  
    return value  
  }  
}  

MinCache.install = function (Vue, {timeout = 86400} = {}) {  
  Vue.prototype.$cache = new MinCache(timeout)  
}  

export default MinCache
// 注册缓存器  
Vue.use(MinCache)

使用方法
this.$cache.set('name', '值')

原文链接
https://juejin.cn/post/6844903870448009223

0 关注 分享

要回复文章请先登录注册

小殊90

小殊90 (作者)

回复 5***@qq.com :
看具体需求,缓存可能会存在丢失,也有不安全的隐患。
2021-03-30 17:22
小殊90

小殊90 (作者)

回复 2***@qq.com :
不会每次构造都执行一遍,相当于所有缓存里面的值拷贝了一份到Map函数中,缓存可能会存在丢失,用Map的话访问过一次,只要不强制清除/手动清除,依然可以取到值,后面取值则是通过Map函数键值对查找,取存在Map函数变量中的值。
2021-03-30 17:18
5***@qq.com

5***@qq.com

回复 5***@qq.com :
get有一处bug 少了个v
```
//获取缓存
get(name) {
try {
const storage = uni.getStorageSync(name)
if (storage.exp < (new Date()).getTime()) {
uni.removeStorageSync(name)
return
} else {
return storage.v
}
} catch (e) {
console.log(e)
}

}
```
2021-01-28 15:59
5***@qq.com

5***@qq.com

为什么要这么麻烦?
```
/**
* uni-app缓存 支持过期时间
* 若存储内容为对象 仅支持能够通过 JSON.stringify 序列化的对象
*/
export default {

//设置缓存 过期时间单位为秒
set(name, value, expire = 24 * 3600) {
//毫秒
expire = (new Date()).getTime() + expire * 1000
const storage = {
v: value,
exp: expire
}

try {
uni.setStorageSync(name, storage)
} catch (e) {
console.log(e)
}

},
//获取缓存
get(name) {
try {
const storage = uni.getStorageSync(name)
if (storage.exp < (new Date()).getTime()) {
uni.removeStorageSync(name)
return
} else {
return storage
}
} catch (e) {
console.log(e)
}

}
}
```
2021-01-28 15:48
2***@qq.com

2***@qq.com

写的挺好的,不过有个疑问:MinCache 的构造函数中,每次构造时都要把所有缓存键值对重新插入cacheMap一遍,会不会有性能问题呢?

const res = uni.getStorageInfoSync()
res.keys.forEach(name => {
try {
const value = uni.getStorageSync(name)
cacheMap.set(name, value)
} catch (e) {
console.log(e)
}
2021-01-24 15:00