setStorage
该 API 使用方法为 wx.setStorage(Object object)
功能说明:将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为1MB,所有数据存储上限为10MB 。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
key | string | - | 是 | 本地缓存中指定的 key |
data | any | - | 是 | 需要存储的内容。只支持原生类型、Date、及能够通过 JSON.stringify 序列化的对象 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.setStorage({key:"key",data:"value"})
wx.setStorage({key: 'key',success (res) {console.log(res.data)}})
setStorageSync
该 API 使用方法为 wx.setStorageSync(string key,any data)
说明:
storage 应只用来进行数据的持久化存储,不应用于运行时的数据传递或全局状态管理。启动过程中过多的同步读写存储,会显著影响启动耗时。
功能说明:将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
参数及说明:string key,本地缓存中指定的 key;any data,需要存储的内容。只支持原生类型、Date、及能够通过 JSON.stringify 序列化的对象。
示例代码:
try {wx.setStorageSync('key', 'value')} catch (e) { }
revokeBufferURL
该 API 使用方法为 wx.revokeBufferURL(string url)
功能说明:根据 URL 销毁存在内存中的数据。
参数及说明:string url,需要销毁的二进制数据 URL。
removeStorage
该 API 使用方法为 wx.removeStorage(Object object)
功能说明:从本地缓存中移除指定 key 。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
key | string | - | 是 | 本地缓存中指定的 key |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.removeStorage({key: 'key',success (res) {console.log(res)}})
try {wx.removeStorageSync('key')} catch (e) {// Do something when catch error}
removeStorageSync
该 API 使用方法为 wx.removeStorageSync(string key)
功能说明:wx.removeStorage 的同步版本。
参数及说明:string key,本地缓存中指定的 key。
示例代码:
wx.removeStorage({key: 'key',success (res) {console.log(res)}})
try {wx.removeStorageSync('key')} catch (e) {// Do something when catch error}
getStorage
该 API 使用方法为 wx.getStorage(Object object)
功能说明:从本地缓存中异步获取指定 key 的内容。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
key | string | - | 是 | 本地缓存中指定的 key |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Object.success 回调函数参数:Object res。
属性 | 类型 | 说明 |
data | any | key 对应的内容 |
示例代码:
wx.getStorage({key: 'key',success (res) {console.log(res.data)}})
wx.getStorage({key: 'key',success (res) {console.log(res.data)}})
createBufferURL
该 API 使用方法为 string wx.createBufferURL(ArrayBuffer|TypedArray buffer)
功能说明:根据传入的 buffer 创建一个唯一的 URL 存在内存中 。
参数及说明:ArrayBuffer|TypedArray buffer,需要存入内存的二进制数据。
返回值:string。
getStorageSync
该 API 使用方法为 any wx.getStorageSync(string key)
功能说明:从本地缓存中同步获取指定 key 的内容 。
参数及说明:string key,本地缓存中指定的 key。
返回值:any key,对应的内容。
示例代码:
try {var value = wx.getStorageSync('key')if (value) {// Do something with return value}} catch (e) {// Do something when catch error}
getStorageInfo
该 API 使用方法为 wx.getStorageInfo(Object object)
功能说明:异步获取当前 storage 的相关信息。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.success 回调函数参数:Object object。
属性 | 类型 | 说明 |
keys | Array.<string> | 当前 storage 中所有的 key |
currentSize | number | 当前占用的空间大小, 单位 KB |
limitSize | number | 限制的空间大小,单位 KB |
示例代码:
wx.getStorageInfo({success(res) {console.log(res.keys)console.log(res.currentSize)console.log(res.limitSize)}})
wx.getStorageInfo({success(res) {console.log(res.keys)console.log(res.currentSize)console.log(res.limitSize)}})
getStorageInfoSync
该 API 使用方法为 Object wx.getStorageInfoSync()
功能说明:wx.getStorageInfo 的同步版本 。
返回值:Object object。
属性 | 类型 | 说明 |
keys | Array. | 当前 storage 中所有的 key |
currentSize | number | 当前占用的空间大小, 单位 KB |
limitSize | number | 限制的空间大小,单位 KB |
示例代码:
wx.getStorageInfo({success (res) {console.log(res.keys)console.log(res.currentSize)console.log(res.limitSize)}})
try {const res = wx.getStorageInfoSync()console.log(res.keys)console.log(res.currentSize)console.log(res.limitSize)} catch (e) {// Do something when catch error}
clearStorage
该 API 使用方法为 wx.clearStorage(Object object)
功能说明:清理本地数据缓存 。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.clearStorage()
try {wx.clearStorageSync()} catch(e) {// Do something when catch error}
clearStorageSync
该 API 使用方法为 wx.clearStorageSync()
功能说明:wx.clearStorage 的同步版本 。
示例代码:
wx.clearStorage()
try {wx.clearStorageSync()} catch(e) {// Do something when catch error}
batchSetStorage
该 API 使用方法为 wx.batchSetStorage(Object object)
功能说明:将数据批量存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
参数及说明:Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
kvList | Array | - | 是 | [{ key, value }] |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码
wx.setStorage({key:"key",data:"value"})
// 开启加密存储wx.batchSetStorage({kvList: [{key: 'key',value: 'value',}],})
batchSetStorageSync
该 API 使用方法为 Array.<any> wx.batchGetStorageSync(Array.<string> keyList)
功能说明:将数据批量存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
参数及说明:Array.<Object> kvList。
属性 | 类型 | 默认值 | 必填 | 说明 |
key | string | - | 是 | key 本地缓存中指定的 key。 |
value | any | - | 是 | data 需要存储的内容。只支持原生类型、Date、及能够通过 JSON.stringify 序列化的对象。 |
示例代码
try {wx.batchSetStorageSync([{key: 'key', value: 'value'}])} catch (e) { }
batchGetStorage
该 API 使用方法为 wx.batchGetStorage(Object object)
功能说明:从本地缓存中异步批量获取指定 key 的内容。
参数及说明:Object object。
属性 | 类型 | 默认值 | 必填 | 说明 |
keyList | Array.<string> | - | 是 | 本地缓存中指定的 keyList |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码
wx.batchGetStorage({keyList: ['key'],success (res) {console.log(res)}})
batchGetStorageSync
该 API 使用方法为 Array.<any> wx.batchGetStorageSync(Array.<string> keyList)
功能说明:从本地缓存中同步批量获取指定 key 的内容。
参数及说明:Array.<string> keyList,本地缓存中指定的 key 数组。
返回值:Array.<any>,key对应的内容。
示例代码
//对于多个key的读取, 批量读取在性能上优于多次 getStorageSync 读取try {var valueList = wx.batchGetStorageSync(['key'])if (valueList) {// Do something with return value}} catch (e) {// Do something when catch error}