All Products
Search
Document Center

Mobile Platform as a Service:Cache

Last Updated:Mar 16, 2023

The caching mechanism is local caching based on appId and userId, so you need to use the MPLogger.setUserId(String userId); method to set the whitelist ID before using the cache.

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 corresponds to.

Note
  • mPaaS 10.1.32 and later versions support this interface.

  • This is an asynchronous interface.

  • The storage of the embedded webview is supported to be isolated from the storage of the mini program, and the data stored with the specified key in the embedded 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

my.setStorage({
  key: 'currentCity',
  data: {
    cityName: 'Hangzhou',
    adCode: '330100',
    spell: ' hangzhou',
  },
  success: function() {
    my.alert({content: 'Write successful'});
  }
});

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is a synchronous interface.

Input parameter

Name

Type

Required

Description

key

String

Yes

key of the cached data

data

Object/String

Yes

Data to be cached

Code sample

my.setStorageSync({
  key: 'currentCity',
  data: {
    cityName: 'Hangzhou',
    adCode: '330100',
    spell: ' hangzhou',
  }
});

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is an asynchronous interface.

  • Supports the isolation of embedded webview internal cache and mini program cache. Obtaining the cache of the specified key of the embedded webview will not return the cached data under the same key of the mini program 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

my.getStorage({
  key: 'currentCity',
  success: function(res) {
    my.alert({content: 'Successfully got: ' + res.data.cityName});
  },
  fail: function(res){
    my.alert({content: res.errorMessage});
  }
});

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is an asynchronous interface.

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

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

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is an asynchronous interface.

  • When removing the stored data of the embedded 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

my.removeStorage({
  key: 'currentCity',
  success: function(){
    my.alert({content: 'Successfully deleted'});
  }
});

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is a synchronous interface.

Input parameter

Name

Type

Required

Description

key

String

Yes

key of the cached data

Code sample

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

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is an asynchronous interface.

  • When clearing the storage of the embedded webview, the storage data of the current mini program itself will not be cleared at the same time.

Code sample

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is a synchronous interface.

Code sample

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
  • mPaaS 10.1.32 and later versions support this interface.

  • This is an asynchronous interface.

  • Obtaining information about the current storage in the embedded webview will not obtain information about the storage of the current mini program.

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

my.getStorageInfo({
  success: function(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    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.

my.getStorageInfoSync

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

Note
  • mPaaS 10.1.32 and later versions support this interface.

  • This is a synchronous interface.

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

var res = my.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
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.