您的位置:首页 > 移动开发 > 微信开发

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

2017-06-06 17:20 330 查看

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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: