Data Cache
setStorage
TIP
The API usage is as follows: wx.setStorage(Object object)
Functional description: Store the data 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.
Parameters and descriptions: Object object。
Properties Type Default value Required Description key string - Yes The specified key in the local cache data any - Yes The content that needs to be stored only supports native types, Date, and objects that can be serialized through JSON.stringify. success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.setStorage({
key:"key",
data:"value"
})wx.setStorage({
key: 'key',
success (res) {
console.log(res.data)
}
})setStorageSync
TIP
The API usage is as follows: wx.setStorageSync(string key,any data)
说明
storage should only be used for persistent storage of data and should not be used for data transfer or global state management at runtime. Excessive synchronous reading and writing of storage during the startup process will significantly affect the startup time.
Functional description: Store the data 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.
Parameters and descriptions: string key, the key specified in the local cache; any data, the content that needs to be stored. Only native types, Date, and objects that can be serialized through JSON.stringify are supported.
Sample code:
try {
wx.setStorageSync('key', 'value')
} catch (e) { }revokeBufferURL
TIP
The API usage is as follows: wx.revokeBufferURL(string url)
This API is supported by mini programs, but not by mini games.
- Functional description: Destroy data stored in memory based on URL.
- Parameters and descriptions: string url, The binary data URL that needs to be destroyed.
removeStorage
TIP
The API usage is as follows: wx.removeStorage(Object object)
Functional description: Remove the specified key from the local cache
Parameters and descriptions: Object object。
Properties Type Default value Required Description key string - Yes The specified key in the local cache success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.removeStorage({
key: 'key',
success (res) {
console.log(res)
}
})try {
wx.removeStorageSync('key')
} catch (e) {
// Do something when catch error
}removeStorageSync
TIP
The API usage is as follows: wx.removeStorageSync(string key)
Functional description: wx.removeStorage The synchronous version
Parameters and descriptions: string key,The specified key in the local cache.
Sample code:
wx.removeStorage({
key: 'key',
success (res) {
console.log(res)
}
})try {
wx.removeStorageSync('key')
} catch (e) {
// Do something when catch error
}getStorage
TIP
The API usage is as follows: wx.getStorage(Object object)
Functional description: Asynchronously obtains the content of the specified key from the local cache.
Parameters and descriptions: Object object。
Properties Type Default value Required Description key string - Yes The specified key in the local cache success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res
Properties Type Description data any The content corresponding to key
Sample code:
wx.getStorage({
key: 'key',
success (res) {
console.log(res.data)
}
})wx.getStorage({
key: 'key',
success (res) {
console.log(res.data)
}
})createBufferURL
TIP
The API usage is as follows: string wx.createBufferURL(ArrayBuffer|TypedArray buffer)
This API is supported by mini programs, but not by mini games.
- Functional description: Create a unique URL in memory based on the passed buffer
- Parameters and descriptions: ArrayBuffer|TypedArray buffer,Binary data that needs to be stored in memory.
- Return value: string。
getStorageSync
TIP
The API usage is as follows: any wx.getStorageSync(string key)
TIP
storage should only be used for persistent storage of data and should not be used for data transfer or global state management at runtime. Excessive synchronous reading and writing of storage during the startup process will significantly affect the startup time.
Functional description: Synchronously obtain the content of the specified key from the local cache.
Parameters and descriptions: string key, The specified key in the local cache.
Return value: any The content corresponding to key
Sample code:
try {
var value = wx.getStorageSync('key')
if (value) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}getStorageInfo
TIP
The API usage is as follows: wx.getStorageInfo(Object object)
Functional description: Asynchronously obtain relevant information about the current storage.
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object object
Properties Type Description keys Array.< string>All keys in current storage currentSize number The size of the space currently occupied, in KB limitSize number The limited space size in KB
Sample code:
wx.getStorageInfo({
success(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})wx.getStorageInfo({
success(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})getStorageInfoSync
TIP
The API usage is as follows: Object wx.getStorageInfoSync()
Functional description: wx.getStorageInfoThe synchronous version
Return value: Object object。
object.success callback function parameters: Object object
Properties Type Description keys Array.< string>All keys in current storage currentSize number The size of the space currently occupied, in KB limitSize number The limited space size in KB
Sample code:
wx.getStorageInfo({
success (res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})try {
const res = wx.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
} catch (e) {
// Do something when catch error
}clearStorage
TIP
The API usage is as follows: wx.clearStorage(Object object)
Functional description: Clear the local data cache
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.clearStorage()try {
wx.clearStorageSync()
} catch(e) {
// Do something when catch error
}clearStorageSync
TIP
The API usage is as follows: wx.clearStorageSync()
Functional description: wx.clearStorageThe synchronous version
Sample code:
wx.clearStorage()try {
wx.clearStorageSync()
} catch(e) {
// Do something when catch error
}batchSetStorage
TIP
The API usage is as follows: wx.batchSetStorage(Object object)
This API is supported by mini programs, but not by mini games.
Functional description: Store the data 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.
Parameters and descriptions: Object object。
Properties Type Default value Required Description kvList Array - Yes [{ key, value }] success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) - Array.<
Object> kvList
Properties Type Required Description key string Yes key The key specified in the local cache value any Yes data The content that needs to be stored. Only supports native types, Date, and objects that can be serialized through JSON.stringify - Array.<
Sample code:
wx.setStorage({
key:"key",
data:"value"
})// Enable encrypted storage
wx.batchSetStorage({
kvList: [{
key: 'key',
value: 'value',
}],
})batchSetStorageSync
TIP
The API usage is as follows: Array.<any> wx.batchGetStorageSync(Array.<string> keyList)
This API is supported by mini programs, but not by mini games.
Functional description: Store the data 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.
Parameters and descriptions: Array.<
Object> kvList。Properties Type Required Description key string Yes key The key specified in the local cache value any Yes data The content that needs to be stored. Only supports native types, Date, and objects that can be serialized through JSON.stringify Sample code:
try {
wx.batchSetStorageSync([{key: 'key', value: 'value'}])
} catch (e) { }batchGetStorage
TIP
The API usage is as follows: wx.batchGetStorage(Object object)
This API is supported by mini programs, but not by mini games.
Functional description: Asynchronously obtain the content of the specified key from the local cache.
Parameters and descriptions: Object object。
Properties Type Default value Required Description kvList Array< string>- Yes The specified key in the local cacheList success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.batchGetStorage({
keyList: ['key'],
success (res) {
console.log(res)
}
})batchGetStorageSync
TIP
The API usage is as follows: Array.<any> wx.batchGetStorageSync(Array.<string> keyList)
This API is supported by mini programs, but not by mini games.
Functional description: Synchronously obtain the content of the specified key from the local cache
Parameters and descriptions: Array.<string> keyList, the key array specified in the local cache
Return value: Array.<any>, the content corresponding to the key
Sample code:
// For reading multiple keys, batch reading is superior in performance to multiple getStorageSync readings.
try {
var valueList = wx.batchGetStorageSync(['key'])
if (valueList) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}