• 微信小程序-----数据缓存


    微信小程序可以通过wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)对本地缓存进行设置、获取和清理。本地缓存最大为10MB。

    wx.setStorage()---------异步设置缓存

    微信官方给出的属性

    OBJECT参数说明:

    参数类型必填说明
    key String 本地缓存中的指定的 key
    data Object/String 需要存储的内容
    success Function 接口调用成功的回调函数
    fail Function 接口调用失败的回调函数
    complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

    调用方式:

    wx.setStorage({
      key:"key",
      data:"value"
    })

    wx.setStorageSync()---------同步设置缓存

    微信官方给出的属性

    参数说明:

    参数类型必填说明
    key String 本地缓存中的指定的 key
    data Object/String 需要存储的内容

    调用方式:

    try {
        wx.setStorageSync('key', 'value')
    } catch (e) {    
    }

    wx.getStorage()--------异步获取缓存

    微信官方给出的属性

    OBJECT参数说明:

    参数类型必填说明
    key String 本地缓存中的指定的 key
    success Function 接口调用的回调函数,res = {data: key对应的内容}
    fail Function 接口调用失败的回调函数
    complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

    success返回参说明:

    参数类型说明
    data String key对应的内容

    调用方式:

    wx.getStorage({
      key: 'key',
      success: function(res) {
          console.log(res.data)
      } 
    })

    wx.getStorageSync()--------同步获取缓存数据

    微信官方给出的属性说明

    参数说明:

    参数类型必填说明
    key String 本地缓存中的指定的 key

    调用方式:

    try {
      var value = wx.getStorageSync('key')
      if (value) {
          // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }

    wx.getStorageInfo()------异步获取当前缓存的数据

    微信官方给出的属性说明

    OBJECT参数说明:

    参数类型必填说明
    success Function 接口调用的回调函数,详见返回参数说明
    fail Function 接口调用失败的回调函数
    complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

    success返回参数说明:

    参数类型说明
    keys String Array 当前storage中所有的key
    currentSize Number 当前占用的空间大小, 单位kb
    limitSize Number 限制的空间大小,单位kb

    调用方式:

    wx.getStorageInfo({
      success: function(res) {
        console.log(res.keys)
        console.log(res.currentSize)
        console.log(res.limitSize)
      }
    })

    wx.getStorageInfoSync()-------同步获取当前缓存数据

    emmmmm---微信并没有给参数说明

    调用方式:

    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的缓存数据

    微信官方参数说明

    OBJECT参数说明:

    参数类型必填说明
    key String 本地缓存中的指定的 key
    success Function 接口调用的回调函数
    fail Function 接口调用失败的回调函数
    complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

    调用方式:

    wx.removeStorage({
      key: 'key',
      success: function(res) {
        console.log(res.data)
      } 
    })

    wx.removeStorageSync()------同步移除指定key的缓存数据

    微信官方参数说明

    参数说明:

    参数类型必填说明
    key String 本地缓存中的指定的 key

    调用方式:

    try {
      wx.removeStorageSync('key')
    } catch (e) {
      // Do something when catch error
    }

    wx.clearStorage()------异步清理本地缓存

    调用方式:

    wx.clearStorage()

    wx.clearStorageSync()-------同步清理本地缓存

    调用方式:

    try {
        wx.clearStorageSync()
    } catch(e) {
      // Do something when catch error
    }
  • 相关阅读:
    [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
    [Angular] Using ngTemplateOutlet to create dynamic template
    [Angular] Create dynamic content with <tempalte>
    [Postgres] Create a Postgres Table
    [Typescript 2] Nullable Types
    [Ramda] Sort, SortBy, SortWith in Ramda
    [Django] Creating an app, models and database
    .NET通用权限系统快速开发框架
    MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型
    TIME_WAIT引起Cannot assign requested address报错
  • 原文地址:https://www.cnblogs.com/kiimi/p/8621896.html
Copyright © 2020-2023  润新知