這篇文章主要介紹了關(guān)于微信小程序中數(shù)據(jù)緩存的解析,有著一定的參考價(jià)值,現(xiàn)在分享給大家,有需要的朋友可以參考一下
每個(gè)微信小程序都可以有自己的本地緩存,本篇文章主要介紹了微信小程序-詳解數(shù)據(jù)緩存,可以通過(guò)函數(shù)可以對(duì)本地緩存進(jìn)行設(shè)置、獲取和清理,有興趣的可以了解一下。
每個(gè)微信小程序都可以有自己的本地緩存,可以通過(guò) wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以對(duì)本地緩存進(jìn)行設(shè)置、獲取和清理。本地緩存最大為10MB。
注意: localStorage 是永久存儲(chǔ)的,但是我們不建議將關(guān)鍵信息全部存在 localStorage,以防用戶換設(shè)備的情況。
wx.setStorage(OBJECT)
將數(shù)據(jù)存儲(chǔ)在本地緩存中指定的 key 中,會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容,這是一個(gè)異步接口。
OBJECT參數(shù)說(shuō)明:
示例代碼
wx.setStorage({ key:"key" data:"value" })
wx.setStorageSync(KEY,DATA)
將 data 存儲(chǔ)在本地緩存中指定的 key 中,會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容,這是一個(gè)同步接口。
OBJECT參數(shù)說(shuō)明:
示例代碼
try { wx.setStorageSync('key', 'value') } catch (e) { }
wx.getStorage(OBJECT)
從本地緩存中異步獲取指定 key 對(duì)應(yīng)的內(nèi)容。
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.getStorage({ key: 'key', success: function(res) { console.log(res.data) } })
wx.getStorageSync(KEY)
從本地緩存中同步獲取指定 key 對(duì)應(yīng)的內(nèi)容。
參數(shù)說(shuō)明:
示例代碼:
try { var value = wx.getStorageSync('key') if (value) { // Do something with return value } } catch (e) { // Do something when catch error }
wx.getStorageInfo(OBJECT)
異步獲取當(dāng)前storage的相關(guān)信息
OBJECT參數(shù)說(shuō)明:
success返回參數(shù)說(shuō)明:
示例代碼:
wx.getStorageInfo({ success: function(res) { console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) } })
wx.getStorageInfoSync
同步獲取當(dāng)前storage的相關(guān)信息
示例代碼:
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參數(shù)說(shuō)明:
示例代碼:
wx.removeStorage({ key: 'key', success: function(res) { console.log(res.data) } })
wx.removeStorageSync(KEY)
從本地緩存中同步移除指定 key 。
參數(shù)說(shuō)明:
示例代碼:
try { wx.removeStorageSync('key') } catch (e) { // Do something when catch error }
wx.clearStorage()
清理本地?cái)?shù)據(jù)緩存。
示例代碼:
wx.clearStorage()
wx.clearStorageSync()
同步清理本地?cái)?shù)據(jù)緩存
示例代碼:
try { wx.clearStorageSync() } catch(e) { // Do something when catch error }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP中文網(wǎng)!
相關(guān)推薦:
以上就是微信小程序中數(shù)據(jù)緩存的解析的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注有卡有網(wǎng)。