WeChat Open Platform

Mini Programs Documentation

Data cache

Every WeChat Mini Program can have its own local cache. The local cache can be set using wx.setStorage (wx.setStorageSync), obtained using wx.getStorage (wx.getStorageSync) and cleared using wx.clearStorage (wx.clearStorageSync). The storage limit is 10MB per WeChat user per Mini Program. localStorage is isolated according to the user dimension. User A is unable to read User B's data on the same device.

Note: localStorage is permanent storage, but we do not recommend storing all key information in localStorage, to prevent situations where users change devices.

wx.setStorage(OBJECT)

Stores data in the specified key within the local cache. Will overwrite the original content corresponding to this key. This is an asynchronous interface.

OBJECT parameter descriptions:

Parameter Type Required Description
key String Yes Specified key within the local cache
data Object/String Yes Content that needs to be stored
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 results (will be executed if call succeeds or fails)

Sample code:

wx.setStorage({
  key:"key",
  data:"value"
})

wx.setStorageSync(KEY,DATA)

Stores data in the specified key within the local cache. Will overwrite the original content corresponding to this key. This is a synchronous interface.

Parameter descriptions:

Parameter Type Required Description
key String Yes Specified key within the local cache
data Object/String Yes Content that needs to be stored

Sample code

try {
    wx.setStorageSync('key', 'value')
} catch (e) {    
}

wx.getStorage(OBJECT)

Asynchronously gets content corresponding to the specified key from the local cache.

OBJECT parameter descriptions:

Parameter Type Required Description
key String Yes Specified key within the local cache
success Function Yes Callback function for interface call, res = {data: Content corresponding to key}
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Type Description
data String Content corresponding to key

Sample code:

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

wx.getStorageSync(KEY)

Synchronously gets content corresponding to the specified key from the local cache.

Parameter descriptions:

Parameter Type Required Description
key String Yes Specified key within the local cache

Sample code:

try {
  var value = wx.getStorageSync('key')
  if (value) {
      // Do something with return value
  }
} catch (e) {
  // Do something when catch error
}

wx.getStorageInfo(OBJECT)

Asynchronously gets information related to current storage

OBJECT parameter descriptions:

Parameter Type Required Description
success Function Yes Callback function for interface call, refer to return parameter descriptions
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Type Description
keys String Array All keys currently in storage
currentSize Number Size of space currently occupied, unit kb
limitSize Number Space limit size, unit kb

Sample code:

wx.getStorageInfo({
  success: function(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})

wx.getStorageInfoSync

Synchronously gets information related to current storage

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

Asynchronously removes specified key from the local cache.

OBJECT parameter descriptions:

Parameter Type Required Description
key String Yes Specified key within the local cache
success Function Yes Callback function for interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.removeStorage({
  key: 'key',
  success: function(res) {
    console.log(res.data)
  } 
})

wx.removeStorageSync(KEY)

Synchronously removes specified key from the local cache.

Parameter descriptions:

Parameter Type Required Description
key String Yes Specified key within the local cache

Sample code:

try {
  wx.removeStorageSync('key')
} catch (e) {
  // Do something when catch error
}

wx.clearStorage()

Clears local data cache.

Sample code:

wx.clearStorage()

wx.clearStorageSync()

Synchronously clears local data cache.

Sample code:

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

Bugs & Tips

  1. tip: Local data storage size limit is 10MB.