Each applet can have its own local cache. This article mainly introduces the applet-detailed description of the data cache, which can be set, retrieved, and cleared by functions, for more information, see. Each applet can have its own local cache. This article mainly introduces the applet-detailed description of the data cache, which can be set, retrieved, and cleared by functions, for more information, see.
Each applet can have its own local cache. setStorage (wx. setStorageSync), wx. getStorage (wx. getStorageSync), wx. clearStorage (wx. clearStorageSync) allows you to set, retrieve, and clear local caches. The local cache size is up to 10 MB.
Note: localStorage is permanently stored, but it is not recommended that all key information be stored in localStorage to prevent users from changing devices.
Wx. setStorage (OBJECT)
Storing data in the specified key in the local cache overwrites the content corresponding to the original key. this is an asynchronous interface.
OBJECT parameter description:
wx.setStorage({ key:"key" data:"value"})
Wx. setStorageSync (KEY, DATA)
Storing data in the specified key in the local cache will overwrite the content corresponding to the original key, which is a synchronous interface.
OBJECT parameter description:
try { wx.setStorageSync('key', 'value')} catch (e) { }
Wx. getStorage (OBJECT)
Asynchronously retrieves the content of a specified key from the local cache.
OBJECT parameter description:
wx.getStorage({ key: 'key', success: function(res) { console.log(res.data) } })
Wx. getStorageSync (KEY)
Synchronously obtains the content of the specified key from the local cache.
Parameter description:
try { var value = wx.getStorageSync('key') if (value) { // Do something with return value }} catch (e) { // Do something when catch error}
Wx. getStorageInfo (OBJECT)
Asynchronously obtains information about the current storage.
OBJECT parameter description:
wx.getStorageInfo({ success: function(res) { console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) }})
Wx. getStorageInfoSync
Synchronously obtain information about the current storage
Sample code:
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)
Asynchronously removes the specified key from the local cache.
OBJECT parameter description:
wx.removeStorage({ key: 'key', success: function(res) { console.log(res.data) } })
Wx. removeStorageSync (KEY)
Synchronously remove a specified key from the local cache.
Parameter description:
try { wx.removeStorageSync('key')} catch (e) { // Do something when catch error}
Wx. clearStorage ()
Clear the local data cache.
Sample code:
wx.clearStorage()
Wx. clearStorageSync ()
Clean up local data cache synchronously
Sample code:
try { wx.clearStorageSync()} catch(e) { // Do something when catch error}
The above is the detailed description of the small program data cache. For more information, see other related articles in the first PHP community!