Skip to content

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。

    PropertiesTypeDefault valueRequiredDescription
    keystring-YesThe specified key in the local cache
    dataany-YesThe content that needs to be stored only supports native types, Date, and objects that can be serialized through JSON.stringify.
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.setStorage({
  key:"key",
  data:"value"
})
js
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:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    keystring-YesThe specified key in the local cache
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.removeStorage({
  key: 'key',
  success (res) {
    console.log(res)
  }
})
js
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:

js
wx.removeStorage({
  key: 'key',
  success (res) {
    console.log(res)
  }
})
js
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。

    PropertiesTypeDefault valueRequiredDescription
    keystring-YesThe specified key in the local cache
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • object.success callback function parameters: Object res

      PropertiesTypeDescription
      dataanyThe content corresponding to key
  • Sample code:

js
wx.getStorage({
  key: 'key',
  success (res) {
    console.log(res.data)
  }
})
js
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:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • object.success callback function parameters: Object object

      PropertiesTypeDescription
      keysArray.<string>All keys in current storage
      currentSizenumberThe size of the space currently occupied, in KB
      limitSizenumberThe limited space size in KB
  • Sample code:

js
wx.getStorageInfo({
  success(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})
js
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

      PropertiesTypeDescription
      keysArray.<string>All keys in current storage
      currentSizenumberThe size of the space currently occupied, in KB
      limitSizenumberThe limited space size in KB
  • Sample code:

js
wx.getStorageInfo({
  success (res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})
js
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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.clearStorage()
js
try {
  wx.clearStorageSync()
} catch(e) {
  // Do something when catch error
}

clearStorageSync

TIP

The API usage is as follows: wx.clearStorageSync()

js
wx.clearStorage()
js
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。

    PropertiesTypeDefault valueRequiredDescription
    kvListArray-Yes[{ key, value }]
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • Array.<Object> kvList
    PropertiesTypeRequiredDescription
    keystringYeskey The key specified in the local cache
    valueanyYesdata The content that needs to be stored. Only supports native types, Date, and objects that can be serialized through JSON.stringify
  • Sample code:

js
wx.setStorage({
  key:"key",
  data:"value"
})
js
// 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。

    PropertiesTypeRequiredDescription
    keystringYeskey The key specified in the local cache
    valueanyYesdata The content that needs to be stored. Only supports native types, Date, and objects that can be serialized through JSON.stringify
  • Sample code:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    kvListArray<string>-YesThe specified key in the local cacheList
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
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:

js
// 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
}