8.4 数据缓存

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

localStorage是永久的本地存储,但为了防止用户在更换设备后丢失信息的情况出现,所以对于重要信息还是建议存储在服务器端,通过请求获取。

数据的存储与读取主要通过“key”与“value”的形式实现,例如存储密码信息采用“password”-“123456”这样的形式,“password”为这组值的key,“123456”为实际存储的值。由于key的值是唯一的,所以在需要读取数据时即可依靠这唯一的值准确获取存储的信息。

wx.setStorage(OBJECT)

将数据存储在本地缓存中指定的key中,会覆盖掉原来该key对应的内容,这是一个异步接口。

OBJECT参数说明(表8-31): 表8-31

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

示例代码:

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

wx.setStorageSync(KEY,DATA)

将 data 存储在本地缓存中指定的key中,会覆盖掉原来该key对应的内容,这是一个同步接口。

参数说明(表8-32): 表8-32

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

示例代码:

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

wx.getStorage(OBJECT)

从本地缓存中异步获取指定key对应的内容。

OBJECT参数说明(表8-33): 表8-33

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

示例代码:

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

wx.getStorageSync(KEY)

从本地缓存中同步获取指定key对应的内容。

参数说明(表8-34): 表8-34

参数 类型 必填 说明
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)

异步获取当前storage的相关信息

OBJECT参数说明(表8-35): 表8-35

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

success返回参数说明(表8-36): 表8-36

参数 类型 说明
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

同步获取当前storage的相关信息

示例代码:

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(OBJECT)

从本地缓存中异步移除指定key。

OBJECT参数说明(表8-37): 表8-37

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

示例代码:

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

wx.removeStorageSync(KEY)

从本地缓存中同步移除指定key。

参数说明(表8-38): 表8-38

参数 类型 必填 说明
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
}

同步与异步的区别在于使用同步的请求必须要在请求完成后才能执行下一步请求,而异步请求不需要等待返回结果就可以执行下一个请求。使用异步请求效率更高,在多数情况下我们都会优先使用异步请求。只有在返回数据会影响请求者状态的情况下,需要使用同步请求。例如我们在本地存储了数据“password”(key)为“123456”(value),然后使用setStorage更改这个数值,接着使用getStorage获取密码,因为是异步操作,所以在发出更改请求后会立即发出获取请求,但数据的更改需要花费时间,这就导致了我们获取的密码可能还是更改前的“123456”。所以在这种情况下就需要使用同步操作,在确保上一个请求完成后再进行下一个。注意在使用同步请求时需要使用“try-catch”语句包裹请求代码。

results matching ""

    No results matching ""