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

Detailed graphic explanation of WeChat applet data caching

Mar 28, 2017 pm 01:32 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)