All Products
Search
Document Center

Cache

Last Updated: Feb 05, 2021

my.setStorage

This API is used to store the data in the specified key in local cache, which will override the original data that the key corresopnds to.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is an asynchronous API.
  • The storage of the built-in webview is supported to be isolated from the storage of the mini program. The stored data of the specified key in the built-in webview will not overwrite the data corresponding to the same key of the mini program itself.

Input parameter

Name Type Required Description
key String Yes key of the cached data
data Object/String Yes Data to be cached
success Function No Callback function for call success
fail Function No Callback function for call failure
complete Function No Callback function for call end (executed regardless of whether the call is successful or failed)

Code sample

  1. my.setStorage({
  2. key: 'currentCity',
  3. data: {
  4. cityName: 'Hangzhou',
  5. adCode: '330100',
  6. spell: ' hangzhou',
  7. },
  8. success: function() {
  9. my.alert({content: 'Write successful'});
  10. }
  11. });

Note: After converting a single piece of data into a string, the maximum length of the string is 200×1024 characters. For the same mPaaS mini program user, the total cache limit for a single mini program is 10 MB.

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.setStorageSync

This API is used to synchronously store data in the specified key in local cache.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is a synchronous API.

Input parameter

Name Type Required Description
key String Yes key of the cached data
data Object/String Yes Data to be cached

Code sample

  1. my.setStorageSync({
  2. key: 'currentCity',
  3. data: {
  4. cityName: 'Hangzhou',
  5. adCode: '330100',
  6. spell: ' hangzhou',
  7. }
  8. });

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.getStorage

This API is used to get the cached data.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is an asynchronous API.
  • The storage of the built-in webview is supported to be isolated from the storage of the mini program. When obtaining the cache of the specified key of the built-in webview, the cached data under the same key of the mini program will not be returned at the same time.

Input parameter

Name Type Required Description
key String Yes key of the cached data
success Function No Callback function for call success
fail Function No Callback function for call failed
complete Function No Callback function for call end (executed regardless of whether the call is successful or failed)

success return value

Name Type Description
data Object/String Content that the key corresponds to

Code sample

  1. my.getStorage({
  2. key: 'currentCity',
  3. success: function(res) {
  4. my.alert({content: 'Successfully got: ' + res.data.cityName});
  5. },
  6. fail: function(res){
  7. my.alert({content: res.errorMessage});
  8. }
  9. });

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.getStorageSync

This API is used to synchronously get the cached data.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is a synchronous API.

Input parameter

Name Type Required Description
key String Yes key of the cached data

success return value

Name Type Description
data Object/String Content that the key corresponds to

Code sample

  1. let res = my.getStorageSync({ key: 'currentCity' });
  2. my.alert({
  3. content: JSON.stringify(res.data),
  4. });

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.removeStorage

This API is used to delete cached data.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is an asynchronous API.
  • When removing the stored data of the built-in webview, the stored data of the current mini program will not be removed.

Input parameter

Name Type Required Description
key String Yes key of the cached data
success Function No Callback function for call successful
fail Function No Callback function for call failed
complete Function No Callback function for call end (executed regardless of whether the call is successful or failed)

Code sample

  1. my.removeStorage({
  2. key: 'currentCity',
  3. success: function(){
  4. my.alert({content: 'Successfully deleted'});
  5. }
  6. });

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.removeStorageSync

This API is used to synchronously delete cached data.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is a synchronous API.

Input parameter

Name Type Required Description
key String Yes key of the cached data

Code sample

  1. my.removeStorageSync({
  2. key: 'currentCity',
  3. });

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.clearStorage

This API is used to clear local data cache.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is an asynchronous API.
  • Clearing the storage of the built-in webview will not clear the stored data of the current mini program itself.

Code sample

  1. my.clearStorage()

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.clearStorageSync

This API is used to synchronously clear local data cache.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is a synchronous API.

Code sample

  1. my.clearStorageSync()

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.getStorageInfo

This API is used to asynchronously get the information about the current storage.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is an asynchronous API.
  • When obtaining information about the current storage in the built-in webview, the information about the storage of the current mini program will not be obtained.

Input parameter

Name Type Required Description
success Function No Callback function for call successful
fail Function No Callback function for call failed
complete Function No Callback function for call end (executed regardless of whether the call is successful or failed)

success return value

Name Type Description
keys String Array All keys in the current storage
currentSize Number The size of space currently occupied, in KB
limitSize Number The size limit of the space, in KB

Code sample

  1. my.getStorageInfo({
  2. success: function(res) {
  3. console.log(res.keys)
  4. console.log(res.currentSize)
  5. console.log(res.limitSize)
  6. }
  7. })

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.

my.getStorageInfoSync

This API is used to synchronously get the information about the current storage.

Note:
  • This API is supported in mPaaS 10.1.32 and later versions.
  • This is a synchronous API.

success return value

Name Type Description
keys String Array All keys in the current storage
currentSize Number The size of space currently occupied, in KB
limitSize Number The size limit of the space, in KB

Code sample

  1. var res = my.getStorageInfoSync()
  2. console.log(res.keys)
  3. console.log(res.currentSize)
  4. console.log(res.limitSize)

Other information

  • Cached data is encrypted and stored locally and will be automatically decrypted and returned when read through API.
  • iTunes backup supported in iOS client.