For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt, and this page is available as Markdown at /apps-plugin/docs/js-api/storage/setStorageSync.md.

setStorageSync

将数据写入本地缓存。

扫码预览

扫码预览 setStorageSync

支持版本

前端库版本不低于 0.0.19。

支持平台

平台支持情况
Android支持
iOS支持
PC不支持
HarmonyOS不支持

支持场景

场景支持情况
页面支持
卡片支持

使用说明

  • 数据按登录用户与应用插件双重隔离存储;单个 value 序列化后不超过 1MB,且全部数据总量不超过 10MB,超出会写入失败。

调用方式

同步 API

setStorageSync<TData = unknown>(params: SetStorageParams<TData>): object

入参

名称类型必填默认值约束说明
dataTData是--待存储的数据
keystring是--本地缓存键名

调用示例

import { setStorageSync } from '@doubao-dev/framework/api';

setStorageSync({
  key: 'profile',
  data: { name: 'Tom', age: 18 },
});

返回值

同步写入完成后返回。

错误处理

同步调用失败时抛出错误。错误对象和通用错误码见 通用错误处理。

失败示例

{
  "errNo": 1600001,
  "errMsg": "exceed storage item max size"
}

错误码

errNoerrMsg平台触发条件处理建议
1600001exceed storage item max sizeAndroid、iOS单个 value 序列化后超过 1MB减小单条数据体积后重试。
1600002total storage size exceededAndroid、iOS写入后本地缓存总量超过 10MB先清理无用缓存后重试。
1600004key is emptyAndroid、iOSkey 为空字符串传入非空 key。
1600005key exceeds max lengthAndroid、iOSkey 长度超过 1024缩短 key 长度。

通用错误码

errNoerrMsg说明平台
102internal error内部错误Android