Storage APIs

setStorage

Store data with a specified key in local cache.

Overview

my.setStorage stores data with a specified key in the local cache. This will overwrite the original data using the same key. This is an asynchronous interface that supports the isolation of webview-embedded storage and the Mini Program storage. Specifying key storage data in embedded webview does not overlap the data corresponding to the same key of the Mini Program itself.

Sample Code

my.setStorage({
  key: 'currentCity',
  data: {
    cityName: 'London',
    adCode: '330100',
    spell: ' London',
  },
  success: function() {
    my.alert({content: 'Set Success'});
  }
});

Parameters

PropertyTypeRequiredDescription
keyStringYesCache data key.
dataObject/StringYesData to be cached.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Usage Notes

  • When a single data is converted into a string, the string length is maximum 200*1024
  • For the same user, the cache of one Mini Program has an upper limit 10MB
  • This is an asynchronous interface
  • Storage is isolated between embedded webview and Mini Program
  • Setting data with an existing key will overwrite the previous value