支付宝小程序API 缓存
支付宝小程序API缓存
my.setStorage
将数据存储在本地缓存中指定的 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: '写入成功'});
}
});
注意:单条数据转换成字符串后,字符串长度最大200*1024。同一个支付宝用户,同一个小程序缓存总上限为10MB
my.setStorageSync
同步将数据存储在本地缓存中指定的 key 中。
入参
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的key |
data | Object/String | 是 | 要缓存的数据 |
my.setStorageSync({
key: 'currentCity',
data: {
cityName: '杭州',
adCode: '330100',
spell: ' hangzhou',
}
});
my.getStorage
获取缓存数据。
入参
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的key |
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
success返回值
名称 | 类型 | 说明 |
---|---|---|
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});
}
});
my.getStorageSync
同步获取缓存数据。
入参
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的key |
返回值
名称 | 类型 | 说明 |
---|---|---|
data | Object/String | key对应的内容 |
代码示例
let res = my.getStorageSync({ key: currentCity });
my.alert({
content: res.data,});
}
my.removeStorage
删除缓存数据。
入参
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的key |
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
代码示例
my.removeStorage({
key: 'currentCity',
success: function(){
my.alert({content: '删除成功'});
}
});
my.removeStorageSync
同步删除缓存数据。
入参
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 缓存数据的key |
my.removeStorageSync({
key: 'currentCity',
});
my.clearStorage
清除本地数据缓存。
代码示例
my.clearStorage()
my.clearStorageSync
同步清除本地数据缓存。
代码示例
my.clearStorageSync()
my.getStorageInfo
异步获取当前storage的相关信息。
入参
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
success | Function | 否 | 调用成功的回调函数 |
fail | Function | 否 | 调用失败的回调函数 |
complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
success返回值
名称 | 类型 | 说明 |
---|---|---|
keys | String Array | 当前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)
}
})
my.getStorageInfoSync
同步获取当前storage的相关信息。
返回值
名称 | 类型 | 说明 |
---|---|---|
keys | String Array | 当前storage中所有的key |
currentSize | Number | 当前占用的空间大小, 单位KB |
limitSize | Number | 限制的空间大小,单位KB |
代码示例
var res = my.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)