Home > WeChat Applet > Mini Program Development > Detailed graphic explanation of WeChat applet data caching

Detailed graphic explanation of WeChat applet data caching

高洛峰
Release: 2017-03-28 13:32:06
Original
2640 people have browsed it

Each WeChat applet can have its own local cache. This article mainly introduces the WeChat applet-detailed explanation of data cache. You can set, obtain and clean the local cache through functions. If you are interested, you can learn more .

Each WeChat applet can have its own local cache, which can be cached locally through wx.setStorage (wx.setStorageSync), wx.getStorage (wx.getStorageSync), wx.clearStorage (wx.clearStorageSync) The cache is set, retrieved and cleaned. The maximum local cache size is 10MB.

Note: localStorage is permanently stored, but we do not recommend storing all key information in localStorage to prevent users from changing devices.

wx.setStorage(OBJECT)

Store the data in the specified key in the local cache, which will overwrite the original content corresponding to the key. This will Is an asynchronous interface.

OBJECT parameter description: Detailed graphic explanation of WeChat applet data caching

Sample code


wx.setStorage({
 key:"key"
 data:"value"
})
Copy after login

wx.setStorageSync(KEY,DATA)

Storing data in the specified key in the local cache will overwrite the original content corresponding to the key. This is a synchronous interface.

OBJECT parameter description: Detailed graphic explanation of WeChat applet data caching

Sample code


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

wx.getStorage(OBJECT)

Asynchronously obtain the content corresponding to the specified key from the local cache.

OBJECT parameter description: Detailed graphic explanation of WeChat applet data caching

Sample code:


wx.getStorage({
 key: 'key',
 success: function(res) {
   console.log(res.data)
 } 
})
Copy after login

wx.getStorageSync(KEY)

Synchronously obtain the content corresponding to the specified key from the local cache.

Parameter description: Detailed graphic explanation of WeChat applet data caching

Sample code:


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

wx.getStorageInfo(OBJECT)

Asynchronously obtain relevant information about the current storage

OBJECT parameter description: Detailed graphic explanation of WeChat applet data caching

Success return parameter description: Detailed graphic explanation of WeChat applet data caching

Sample code :


wx.getStorageInfo({
 success: function(res) {
  console.log(res.keys)
  console.log(res.currentSize)
  console.log(res.limitSize)
 }
})
Copy after login

wx.getStorageInfoSync

Get relevant information of the current storage synchronously

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
}
Copy after login

wx.removeStorage(OBJECT)

Asynchronously remove the specified key from the local cache.

OBJECT parameter description: Detailed graphic explanation of WeChat applet data caching

Sample code:


wx.removeStorage({
 key: 'key',
 success: function(res) {
  console.log(res.data)
 } 
})
Copy after login

wx.removeStorageSync(KEY)

Synchronously remove the specified key from the local cache.

Parameter description:Detailed graphic explanation of WeChat applet data caching

Sample code:


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

wx.clearStorage()
Clear local data cache.

Sample code:


wx.clearStorage()
Copy after login

wx.clearStorageSync()

Clear the local data cache synchronously

Sample code:


try {
  wx.clearStorageSync()
} catch(e) {
 // Do something when catch error
}
Copy after login

The above is the detailed content of Detailed graphic explanation of WeChat applet data caching. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template