微信小程序数据缓存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38959210/article/details/80336830

每个微信小程序都可以有自己的本地缓存同一个微信用户,同一个小程序 storage 上限为 10MB。

操作异步方法同步方法
插入wx.setStoragewx.setStorageSync
读取wx.getStoragewx.getStorageSync
删除wx.removeStoragewx.removeStorageSync
清空wx.clearStoragewx.clearStorageSync
获取当前缓存信息wx.getStorageInfowx.getStorageInfoSync

示例代码:

wx.setStorage({
  key:"key",
  data:"value"
})
try {
    wx.setStorageSync('key', 'value')
} catch (e) {    
}
wx.getStorage({
  key: 'key',
  success: function(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.getStorageInfo({
  success: function(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})
try {
  var res = wx.getStorageInfoSync()
  console.log(res.keys)
  console.log(res.currentSize)
  console.log(res.limitSize)
} catch (e) {
  // Do something when catch error
}
wx.removeStorage({
  key: 'key',
  success: function(res) {
    console.log(res.data)
  } 
})
try {
  wx.removeStorageSync('key')
} catch (e) {
  // Do something when catch error
}
wx.clearStorage()
try {
    wx.clearStorageSync()
} catch(e) {
  // Do something when catch error
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页