Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program API data cache


May 19, 2021 WeChat Mini Program Development Document


Table of contents


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.

wx.setStorage(OBJECT)


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"
})

wx.setStorageSync(KEY,DATA)


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) {
}

wx.getStorage(OBJECT)


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)
  } 
})

wx.getStorageSync(KEY)


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
}

wx.getStorageInfo(OBJECT)

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)
  }
})

wx.getStorageInfoSync

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
}

wx.removeStorage(OBJECT)

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)
  } 
})

wx.removeStorageSync(KEY)

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
}

wx.clearStorage()


Clean up the local data cache.

Example code:

wx.clearStorage()

wx.clearStorageSync()


Synchronize to clean up the local data cache

Example code:

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

Bug & Tip

  1. tip The size of the local data store is limited to 10MB