缓存的机制是根据 appId 和 userId 进行本地缓存,因此在使用缓存前需要使用 MPLogger.setUserId(String userId);
方法设置白名单 ID。
将数据存储在本地缓存中指定的 key 中,会覆盖原来该 key 对应的数据。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的 key |
data | Object/String | 是 | 要缓存的数据 |
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
my.setStorage({
key: 'currentCity',
data: {
cityName: '杭州',
adCode: '330100',
spell: ' hangzhou',
},
success: function() {
my.alert({content: '写入成功'});
}
});
同步将数据存储在本地缓存中指定的 key 中。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的 key |
data | Object/String | 是 | 要缓存的数据 |
my.setStorageSync({
key: 'currentCity',
data: {
cityName: '杭州',
adCode: '330100',
spell: ' hangzhou',
}
});
获取缓存数据。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的 key |
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
名称 | 类型 | 说明 |
---|---|---|
data | Object/String | key 对应的内容 |
my.getStorage({
key: 'currentCity',
success: function(res) {
my.alert({content: '获取成功:' + res.data.cityName});
},
fail: function(res){
my.alert({content: res.errorMessage});
}
});
同步获取缓存数据。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的 key |
名称 | 类型 | 说明 |
---|---|---|
data | Object/String | key 对应的内容 |
let res = my.getStorageSync({ key: 'currentCity' });
my.alert({
content: JSON.stringify(res.data),
});
删除缓存数据。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的 key |
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
my.removeStorage({
key: 'currentCity',
success: function(){
my.alert({content: '删除成功'});
}
});
删除缓存数据。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的 key |
my.removeStorageSync({
key: 'currentCity',
});
清除本地数据缓存。
my.clearStorage()
清除本地数据缓存。
my.clearStorageSync()
异步获取当前 storage 的相关信息。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
名称 | 类型 | 说明 |
---|---|---|
keys | StringArray | 当前 storage 中所有的 key |
currentSize | Number | 当前占用的空间大小, 单位为 KB |
limitSize | Number | 限制的空间大小,单位为 KB |
my.getStorageInfo({
success: function(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})
同步获取当前 storage 的相关信息。
名称 | 类型 | 说明 |
---|---|---|
keys | StringArray | 当前 storage 中所有的 key |
currentSize | Number | 当前占用的空间大小, 单位为 KB |
limitSize | Number | 限制的空间大小,单位为 KB |
var res = my.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
其他信息
在文档使用中是否遇到以下问题
更多建议
匿名提交