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

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

    }

相关推荐

  1. 程序开发入门实战学习

    2024-01-06 16:48:02       49 阅读
  2. 程序开发入门实战学习

    2024-01-06 16:48:02       59 阅读
  3. 程序开发入门实战学习

    2024-01-06 16:48:02       50 阅读
  4. 程序开发入门实战学习

    2024-01-06 16:48:02       60 阅读
  5. 程序开发入门实战学习

    2024-01-06 16:48:02       59 阅读
  6. 程序开发入门实战学习

    2024-01-06 16:48:02       61 阅读
  7. 程序开发入门实战学习

    2024-01-06 16:48:02       59 阅读
  8. 程序开发入门实战学习

    2024-01-06 16:48:02       52 阅读
  9. 程序开发入门实战学习

    2024-01-06 16:48:02       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 16:48:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 16:48:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 16:48:02       82 阅读
  4. Python语言-面向对象

    2024-01-06 16:48:02       91 阅读

热门阅读

  1. 如何停止一个运行中的Docker容器

    2024-01-06 16:48:02       65 阅读
  2. 步进电机调速原理

    2024-01-06 16:48:02       48 阅读
  3. vs c++ qt 叫请求的json 输出到输出终端

    2024-01-06 16:48:02       43 阅读
  4. 优医问诊H5 Vue3+TS+Pinia+Vant源码。

    2024-01-06 16:48:02       46 阅读
  5. 缓冲和缓存的区别

    2024-01-06 16:48:02       58 阅读
  6. 数据结构-怀化学院期末题(489)

    2024-01-06 16:48:02       54 阅读