微信小程式API 資料快取
阿新 • • 發佈:2018-11-05
每個微信小程式都可以有自己的本地快取,可以通過wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以對本地快取進行設定、獲取和清理。同一個微信使用者,同一個小程式 storage 上限為 10MB。localStorage 以使用者維度隔離,同一臺裝置上,A 使用者無法讀取到 B 使用者的資料。
注意: localStorage是永久儲存的,但是我們不建議將關鍵資訊全部存在localStorage,以防使用者換裝置的情況。
wx.setStorage(OBJECT)
將資料儲存在本地快取中指定的key中,會覆蓋掉原來該key對應的內容,這是一個非同步介面。
OBJECT引數說明:
引數 | 型別 | 必填 | 說明 |
---|---|---|---|
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對應的內容,這是一個同步介面。
引數說明:
引數 | 型別 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地快取中的指定的key |
data | Object/String | 是 | 需要儲存的內容 |
示例程式碼
try {
wx.setStorageSync("key","value")
} catch (e) {
}
wx.getStorage(OBJECT)
從本地快取中非同步獲取指定key對應的內容。
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)
從本地快取中同步獲取指定key對應的內容。
引數說明:
引數 | 型別 | 必填 | 說明 |
---|---|---|---|
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引數說明:
引數 | 型別 | 必填 | 說明 |
---|---|---|---|
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
同步獲取當前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引數說明:
引數 | 型別 | 必填 | 說明 |
---|---|---|---|
key | String | 是 | 本地快取中的指定的 key |
success | Function | 是 | 介面呼叫的回撥函式 |
fail | Function | 否 | 介面呼叫失敗的回撥函式 |
complete | Function | 否 | 介面呼叫結束的回撥函式(呼叫成功、失敗都會執行) |
示例程式碼:
wx.removeStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})
wx.removeStorageSync(KEY)
從本地快取中同步移除指定 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
}
Bug & Tip
tip
: 本地資料儲存的大小限制為 10MB