微信小程序数据缓存API整理

xiaoxiao2021-02-27  153

1、wx.getStorage(OBJECT)

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

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

2、wx.setStorageSync(KEY,DATA)

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

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

3、wx.getStorage(OBJECT)

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

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

4、wx.getStorageSync(KEY)

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

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

5、wx.getStorageInfo(OBJECT)

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

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

6、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 }

7、wx.removeStorage(OBJECT)

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

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

8、wx.removeStorageSync(KEY)

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

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

9、wx.clearStorage()

清理本地数据缓存

wx.clearStorage()

10、wx.clearStorageSync()

同步清理本地数据缓存

try { wx.clearStorageSync() } catch(e) { // Do something when catch error } 本地数据缓存大小限制为10MB。

转载请注明原文地址: https://www.6miu.com/read-16604.html

最新回复(0)