微信小程序数据缓存

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

操作 异步方法 同步方法
插入 wx.setStorage wx.setStorageSync
读取 wx.getStorage wx.getStorageSync
删除 wx.removeStorage wx.removeStorageSync
清空 wx.clearStorage wx.clearStorageSync
获取当前缓存信息 wx.getStorageInfo wx.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
}

你可能感兴趣的:(微信小程序)