Home > WeChat Applet > Mini Program Development > Let's talk about how to operate local storage synchronously or asynchronously in a mini program

Let's talk about how to operate local storage synchronously or asynchronously in a mini program

青灯夜游
Release: 2021-11-22 19:28:41
forward
3528 people have browsed it

This article will take you to understand the synchronous and asynchronous storage in the WeChat applet, and introduce the methods of synchronous operation of local storage and asynchronous operation of local storage. I hope it will be helpful to everyone!

Let's talk about how to operate local storage synchronously or asynchronously in a mini program

1. Synchronous operation of local storage

Unless necessary, try to use the synchronous method, especially for novices, it is recommended to use the synchronous method unless the synchronous method cannot solve the problem Question consider using async method. [Related learning recommendations: Mini Program Development Tutorial]

wx.setStorageSync Synchronous Storage:

wx.setStorageSync('key', 'value')
Copy after login

The effect can be seen in the WeChat Mini Program debugger as follows

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

wx.getStorageSync synchronously obtains:

wx.getStorageSync('key')
console.log(wx.getStorageSync('key'))//value
Copy after login

wx.getStorageInfoSync() The information in the current storage

const res = wx.getStorageInfoSync()
console.log(res.keys)//["logs", "key"]
//res.keys当前 storage 中所有的 key
console.log(res.currentSize)//1
//res.currentSize当前占用的空间大小, 单位 KB
console.log(res.limitSize)//10240
//res.limitSize限制的空间大小,单位 KB
Copy after login

wx.removeStorageSync synchronously removes one:

wx.removeStorageSync('key')
Copy after login

After removal, the storage called key will disappear

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

wx.clearStorageSync clears everything synchronously:

wx.clearStorageSync()
Copy after login

Use clearStorageSync as follows Together with the previous logs, they will be cleared.

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

2. Asynchronous operation of local storage

1.wx.setStorage asynchronous storage value;

will The data is stored in the local cache at the specified key. Will overwrite the original content corresponding to the key. Unless the user actively deletes it or it is cleared by the system due to storage space reasons, the data will always be available. The maximum data length allowed to be stored in a single key is 1MB, and the upper limit of all data storage is 10MB.

wx.setStorage({
    key:"key2",
    data:"value2"
})
Copy after login

After we store the value, we can see the effect in the debug bar of the WeChat applet. Except for the different operations of access and execution, the results of synchronous and asynchronous are the same. The result of saving and getting the value is the same, except that synchronization is executed sequentially, while asynchronous operation will not cause the interface to stagnate. However, this can almost be ignored, so it is recommended that you use synchronization if it is not necessary.

Lets talk about how to operate local storage synchronously or asynchronously in a mini program

2.wx.removeStorage() removes the specified value

Removes the specified key from the local cache.

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

3.wx.getStorage(); Get the value

Asynchronously obtain the content of the specified key from the local cache.

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

4.wx.getStorageInfo Gets the information in the current storage

wx.getStorageInfo({
   success (res) {
   console.log(res.keys)//["logs", "key"]
   //当前 storage 中所有的 key
   console.log(res.currentSize)//1
   //当前占用的空间大小, 单位 KB
   console.log(res.limitSize)//10240
   //限制的空间大小,单位 KB
}})
Copy after login

5.wx.clearStorage(); Clear all keys

wx.clearStorage()
Copy after login

More programming related knowledge, Please visit: Introduction to Programming! !

The above is the detailed content of Let's talk about how to operate local storage synchronously or asynchronously in a mini program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.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