微信小程序缓存的放入与取出

15 篇文章 1 订阅
6 篇文章 0 订阅

wx.setStorage(Object object)将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
官方文档给出的示例代码:

wx.setStorage({
  key:"key",
  data:"value"
})
// 开启加密存储
wx.setStorage({
  key: "key",
  data: "value",
  encrypt: true, // 若开启加密存储,setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
  success() {
    wx.getStorage({
      key: "key",
      encrypt: true, // 若开启加密存储,setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
      success(res) {
        console.log(res.data)
      }
    })
  }
})

用在登录时缓存数据:

wx.getUserProfile({
      desc: '获取用户信息',
      success: (res) => {
        console.log(res)
        let user = res.userInfo
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
        })
        var encryptedData = res.encryptedData;
        var iv = res.iv;
        wx.login({
          success: (res) => {
            if (res.code) {
              console.log("res.code is " + res.code)
              wx.request({
                url: '',
                method: 'Get',
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                  code: res.code,
                  encryptedData: encryptedData,
                  iv: iv,
                },
                success(res) {
                  console.log("res.data.openid is",res.data.openid)
                  openid: res.data.openid
                  //请求成功之后,把openid放到储存里面
                  wx.setStorage({
                    key: 'openid',
                    data: res.data.openid
                  })
                }
              })
            } else {
              console.log("登陆失败!" + res.errMsg)
            }
          }
        })
      }
    })

可以看到控制台storage中有相应数值:
在这里插入图片描述

wx.getStorage(Object object)从本地缓存中异步获取指定 key 的内容。
官方文档给出的示例代码:

wx.getStorage({
  key: 'key',
  success (res) {
    console.log(res.data)
  }
})
// 开启加密存储
wx.setStorage({
  key: "key",
  data: "value",
  encrypt: true, // 若开启加密存储,setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
  success() {
    wx.getStorage({
      key: "key",
      encrypt: true, // 若开启加密存储,setStorage 和 getStorage 需要同时声明 encrypt 的值为 true
      success(res) {
        console.log(res.data)
      }
    })
  }
})

实际应用在页面获取缓存中的name,用到wx.getStorage

data: {
	name: ''
},
onLoad: function(options) {
	let that = this
	wx.getStorage({
      key: 'name',
      success(res) {
        console.log(res.data)
        that.setData({
          name: res.data
        })
     })
},

onReady: function() {
	console.log(this.data.name)
}

在用到的地方直接取wx.getStorageSync('key');
官方文档关于数据缓存:https://developers.weixin.qq.com/miniprogram/dev/api/storage/wx.setStorageSync.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值