美文网首页
微信小程序之数据缓存API

微信小程序之数据缓存API

作者: 荒剑离 | 来源:发表于2020-02-04 22:39 被阅读0次
    • wx.setStorage(Object object)
      • 将数据存储在本地缓存中指定的 key 中,且会覆盖掉原来该 key 对应的内容。
      • 除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。
      • 单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
      • 属性:key,data(只支持原生类型、Date、及能够通过JSON.stringify序列化的对象。),success,fail,complete
      • wx.setStorageSync(string key, any data) 是同步版本。
    wx.setStorage({
      key:"key",
      data:"value"
    })
    try {
      wx.setStorageSync('key', 'value')
    } catch (e) { }
    
    wx.removeStorage({
      key: 'key',
      success (res) {
        console.log(res)
      }
    })
    try {
      wx.removeStorageSync('key')
    } catch (e) {
      // Do something when catch error
    }
    
    wx.getStorage({
      key: 'key',
      success (res) {
        console.log(res.data)
      }
    })
    try {
      var value = wx.getStorageSync('key')
      if (value) {
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
    
    wx.clearStorage()
    try {
      wx.clearStorageSync()
    } catch(e) {
      // Do something when catch error
    }
    
    • wx.getStorageInfo(Object object)
      • 异步获取当前storage的相关信息
      • 属性:success,fail,complete
      • success(Object object)回调函数中包含参数
        • keys, Array.<string>, 当前 storage 中所有的 key
        • currentSize, number, 当前占用的空间大小, 单位 KB
        • limitSize, number, 限制的空间大小,单位 KB
      • Object wx.getStorageInfoSync()是同步版本。
    wx.getStorageInfo({
      success (res) {
        console.log(res.keys)
        console.log(res.currentSize)
        console.log(res.limitSize)
      }
    })
    try {
      const res = wx.getStorageInfoSync()
      console.log(res.keys)
      console.log(res.currentSize)
      console.log(res.limitSize)
    } catch (e) {
      // Do something when catch error
    }
    

    相关文章

      网友评论

          本文标题:微信小程序之数据缓存API

          本文链接:https://www.haomeiwen.com/subject/jqndxhtx.html