ty.setStorage
将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
需引入
BaseKit,且在>=1.2.10版本才可使用
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| key | string | 是 | 本地缓存中指定的 key | |
| data | string | 是 | key 对应的内容 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 |
函数定义示例
/**
* 将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
*/
export function setStorage(params: {
/** 本地缓存中指定的 key */
key: string;
/** key对应的内容 */
data: string;
complete?: () => void;
success?: (params: null) => void;
fail?: (params: {
errorMsg: string;
errorCode: string | number;
innerError: {
errorCode: string | number;
errorMsg: string;
};
}) => void;
}): void;
ty.setStorageSync
将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。此方法是同步方法。
需引入
BaseKit,且在>=1.2.10版本才可使用ty.setStorage 的同步版本
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| key | string | 是 | 本地缓存中指定的 key | |
| data | string | 是 | key 对应的内容 |
函数定义示例
/**
* 将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
*/
export function setStorageSync(params: {
/** 本地缓存中指定的 key */
key: string;
/** key对应的内容 */
data: string;
}): null;
ty.getStorage
从本地缓存中异步获取指定 key 的内容
需引入
BaseKit,且在>=1.2.10版本才可使用
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| key | string | 是 | 本地缓存中指定的 key | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 |
object.success 回调参数
参数
Object res
| 属性 | 类型 | 说明 |
|---|---|---|
| data | string | key 对应的内容 |
object.fail 回调参数
参数
Object res
| 属性 | 类型 | 说明 |
|---|---|---|
| errorMsg | string | 插件错误信息 |
| errorCode | string | 错误码 |
| innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
函数定义示例
/**
* 从本地缓存中异步获取指定 key 的内容
*/
export function getStorage(params: {
/** 本地缓存中指定的 key */
key: string;
complete?: () => void;
success?: (params: {
/** key对应的内容 */
data?: string;
}) => void;
fail?: (params: {
errorMsg: string;
errorCode: string | number;
innerError: {
errorCode: string | number;
errorMsg: string;
};
}) => void;
}): void;
ty.getStorageSync
从本地缓存中异步获取指定 key 的内容同步方法
需引入
BaseKit,且在>=1.2.10版本才可使用ty.getStorage 的同步版本
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| key | string | 是 | 本地缓存中指定的 key |
返回值
| 属性 | 类型 | 说明 |
|---|---|---|
| data | string | key 对应的内容 |
函数定义示例
/**
* 从本地缓存中异步获取指定 key 的内容
*/
export function getStorageSync(params: {
/** 本地缓存中指定的 key */
key: string;
}): {
/** key对应的内容 */
data?: string;
};
ty.clearStorage
清理本地数据缓存
需引入
BaseKit,且在>=1.2.10版本才可使用
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 |
函数定义示例
/**
* 清理本地数据缓存
*/
export function clearStorage(params?: {
complete?: () => void;
success?: (params: null) => void;
fail?: (params: {
errorMsg: string;
errorCode: string | number;
innerError: {
errorCode: string | number;
errorMsg: string;
};
}) => void;
}): void;
ty.clearStorageSync
清理本地数据缓存同步方法
需引入
BaseKit,且在>=1.2.10版本才可使用ty.clearStorage 的同步版本
函数定义示例
/**
* 清理本地数据缓存
*/
export function clearStorageSync(): null;
ty.removeStorage
清理本地数据缓存
需引入
BaseKit,且在>=1.2.10版本才可使用
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| key | string | 是 | 本地缓存中指定的 key | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 |
函数定义示例
/**
* 清理本地数据缓存
*/
export function removeStorage(params: {
/** 本地缓存中指定的 key */
key: string;
complete?: () => void;
success?: (params: null) => void;
fail?: (params: {
errorMsg: string;
errorCode: string | number;
innerError: {
errorCode: string | number;
errorMsg: string;
};
}) => void;
}): void;
ty.removeStorageSync
清理本地数据缓存同步方法
需引入
BaseKit,且在>=1.2.10版本才可使用ty.removeStorage 的同步版本
参数
Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| key | string | 是 | 本地缓存中指定的 key |
函数定义示例
/**
* 清理本地数据缓存
*/
export function removeStorageSync(params: {
/** 本地缓存中指定的 key */
key: string;
}): null;
👉 立即开发。
本文介绍了Ty库中的四个与本地缓存相关的API:setStorage用于存储数据,setStorageSync提供同步版本,getStorage和getStorageSync用于异步获取数据,以及clearStorage和removeStorage用于清理缓存。这些方法在BaseKit1.2.10及以上版本可用。
1117

被折叠的 条评论
为什么被折叠?



