小程序开发-本地缓存使用方式

小程序本地缓存Api

异步

1. wx.setStorage(Object object)

将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容

wx.setStorage({
  key:"key",
  data:"value"
})
2. wx.getStorage(Object object)

从本地缓存中异步获取指定 key 的内容


wx.getStorage({
  key: 'key',
  success: function(res) {
      console.log(res.data)
  }
})
3. wx.getStorageInfo(Object object)

异步获取当前storage的相关信息

wx.getStorageInfo({
  success: function(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})
4. wx.removeStorage(Object object)

从本地缓存中移除指定的key

wx.removeStorage({
  key: 'key',
  success (res) {
    console.log(res)
  }
})

同步

1. wx.setStorageSync(string key, any data)

将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。

2. wx.getStorageSync(string key)

从本地缓存中同步获取指定 key 对应的内容。

3. wx.getStorageInfoSync()

同步获取当前storage的相关信息

4. wx.removeStorageSync(string key)

从本地缓存中同步移除指定 key 。

清理缓存

wx.clearStorage()

清理本地数据缓存。

wx.clearStorageSync()

同步清理本地数据缓存

原文地址:https://www.cnblogs.com/limaostudio/p/13639192.html