《微信小程序开发从入门到实战》学习七十三

2024-01-07 18:15:11

6.7数据缓存API

6.7.2?获取数据API

使用wx.getStorageSync和wx.getStorage接口可从本地缓存读取指定key中的数据。使用方式如下:

????//?异步接口,可以使用三回调函数

????wx.getStorage({

??????key:?'key',

??????success(res)?{

????????console.log(res.data)?//?读取的数据保存到data属性中,如果数据不存在则data为null

??????}

????})

????//?同步接口

????try{

??????const?value?=?wx.getStorageSync('key')

??????if(value){

????????//?Do?something?with?return?value

??????}

????}?catch?(e)?{

??????//?Do?something?when?catch?error

????}

6.7.3?查询缓存信息API

使用wx.getStorageInfoSync和wx.getStorageInfo接口可查询当前本地缓存中所有数据情况。使用方式如下:

//?异步接口,可以使用三回调函数

????wx.getStorageInfo({

??????success(res)?{

????????console.log(res.keys)?//?string[]?类型,包含当前本地存储中所有的key

????????console.log(res.currentSize)?//?当前占用的空间大小,单位为KB

????????console.log(res.limitSize)?//?限制的空间大小,单位为KB

??????}

????})

????//?同步接口

????try{

??????const?res?=?wx.getStorageInfoSync()

??????console.log(res.keys)?//?string[]?类型,包含当前本地存储中所有的key

??????console.log(res.currentSize)?//?当前占用的空间大小,单位为KB

??????console.log(res.limitSize)?//?限制的空间大小,单位为KB

????}?catch?(e)?{

??????//?Do?something?when?catch?error

????}

6.7.4?删除数据API

使用wx.removeStorageSync或wx.removeStorage接口可从本地缓存中删除指定key中的数据。使用方式如下:

//?异步接口,可以使用三回调函数

????wx.removeStorage({

??????key:?'key',

??????success(res)?{

????????console.log(res)

??????}

????})

????//?同步接口

????try{

??????wx.removeStorageSync('key')

????}?catch?(e)?{

??????//?Do?something?when?catch?error

????}

6.7.4?缓存清空API

使用wx.clearStorageSync或wx.clearStorage接口可以清空本地缓存中的所有数据。使用方式如下:

//?异步接口,可以使用三回调函数

????wx.clearStorage()

????//?同步接口

????try{

??????wx.clearStorageSync()

????}?catch?(e)?{

??????//?Do?something?when?catch?error

????}

文章来源:https://blog.csdn.net/xy2937131682/article/details/135419586
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。