May 19, 2021 WeChat Mini Program Development Document
Each WeChat widget can have its own local cache, which can be set up, acquired, and cleaned through wx.setStorage (wx.setStorageSync), wx.getStorageSync, and wx.clearStorageSync. F or the same WeChat user, the storage limit for the same small program is 10MB. LocalStorage is isolated by user dimensions, and on the same device, A users cannot read B user data.
Note: LocalStorage is permanently stored, but we do not recommend that you have all the critical information in place to prevent users from changing devices.
Storing data in the key specified in the local cache overrides the original key, which is an asynchronous interface.
OBJECT parameter description:
Parameters | Type | Required | Description |
---|---|---|---|
key | String | Is | The specified key in the local cache |
data | Object/String | Is | What needs to be stored |
success | Function | Whether | The interface calls a successful callback function |
fail | Function | Whether | The interface calls the failed callback function |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
The sample code
wx.setStorage({
key:"key"
data:"value"
})
In the KEY specified in the local cache, you will overwrite the original key corresponding to the key, which is a synchronization interface.
Parameter Description:
parameter | type | Required | illustrate |
---|---|---|---|
key | String | Yes | Specify key in the local cache |
data | Object/String | Yes | Need to store content |
Sample code
try {
wx.setStorageSync("key","value")
} catch (e) {
}
Asynchronously obtain the contents corresponding to the specified Key from the local cache.
Object parameter description:
parameter | type | Required | illustrate |
---|---|---|---|
key | String | Yes | Specify key in the local cache |
success | Function | Yes | The callback function of the interface call, res = {data: key corresponding to the content} |
fail | Function | no | Interface call failed callback function |
complete | Function | no | The callback function ended in the interface call (the call is successful, failed) |
SUCCESS Return Parameter Description:
parameter | type | illustrate |
---|---|---|
data | String | Key corresponds to the content |
Example code:
wx.getStorage({
key:'key',
success: function(res){
console.log(res.data)
}
})
Synchronize from the local cache to get the content for the specified key.
Description of the parameters:
Parameters | Type | Required | Description |
---|---|---|---|
key | String | Is | The specified key in the local cache |
Example code:
try {
var value = wx.getStorageSync('key')
if (value) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}
Get information about the current storage asynchronously
OBJECT parameter description:
Parameters | Type | Required | Description |
---|---|---|---|
success | Function | Is | The callback function called by the interface, as detailed in the return parameter description |
fail | Function | Whether | The interface calls the failed callback function |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Success returns a description of the parameters:
Parameters | Type | Description |
---|---|---|
keys | String Array | All keys in the current storage |
currentSize | Number | The current amount of space occupied, in kb |
limitSize | Number | Limited space size in kb |
Example code:
wx.getStorageInfo({
success: function(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})
Sync to get information about the current storage
Example 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
}
Remove the specified key asynchronously from the local cache.
OBJECT parameter description:
Parameters | Type | Required | Description |
---|---|---|---|
key | String | Is | The specified key in the local cache |
success | Function | Is | The callback function called by the interface |
fail | Function | Whether | The interface calls the failed callback function |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Example code:
wx.removeStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})
Remove the specified key from the local cache synchronously.
Description of the parameters:
Parameters | Type | Required | Description |
---|---|---|---|
key | String | Is | The specified key in the local cache |
Example code:
try {
wx.removeStorageSync('key')
} catch (e) {
// Do something when catch error
}
Clean up the local data cache.
Example code:
wx.clearStorage()
Synchronize to clean up the local data cache
Example code:
try {
wx.clearStorageSync()
} catch(e) {
// Do something when catch error
}
tip
The size of the local data store is limited to 10MB