> 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.

# setStorage

将数据写入本地缓存。

## 扫码预览

![扫码预览 setStorage](https://api.qrserver.com/v1/create-qr-code/?size=200x200\&data=doubao%3A%2F%2Fdoubao_apps%3Fapp_id%3Ddb_1McM5Ni%26path%3D%252Fpages%252Fapi%252Fstorage%252Fset-storage%252Findex)

## 支持版本

前端库版本不低于 `0.0.17`。

## 支持平台

<table>
<thead>
<tr><th>平台</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>Android</td><td>支持</td></tr>
<tr><td>iOS</td><td>支持</td></tr>
<tr><td>PC</td><td>不支持</td></tr>
<tr><td>HarmonyOS</td><td>不支持</td></tr>
</tbody>
</table>

## 支持场景

<table>
<thead>
<tr><th>场景</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>页面</td><td>支持</td></tr>
<tr><td>卡片</td><td>支持</td></tr>
</tbody>
</table>

## 使用说明

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

## 调用方式

### 异步 API

```typescript
setStorage<TData = unknown>(params: SetStorageParams<TData>): Promise<object>
```

## 入参

<table>
<thead>
<tr><th>名称</th><th>类型</th><th>必填</th><th>默认值</th><th>约束</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td><code>data</code></td><td><code>TData</code></td><td>是</td><td>-</td><td>-</td><td>待存储的数据</td></tr>
<tr><td><code>key</code></td><td><code>string</code></td><td>是</td><td>-</td><td>-</td><td>本地缓存键名</td></tr>
</tbody>
</table>

## 调用示例

```typescript
import { setStorage } from '@doubao-dev/framework/api';

await setStorage({
  key: 'profile',
  data: { name: 'Tom', age: 18 },
});
```

## 成功返回

无返回字段。

## 错误处理

错误对象和通用错误码见 [通用错误处理](/apps-plugin/docs/js-api/common-errors.md#错误返回)。

### 失败示例

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

### 错误码

<table>
<thead>
<tr><th>errNo</th><th>errMsg</th><th>平台</th><th>触发条件</th><th>处理建议</th></tr>
</thead>
<tbody>
<tr><td><code>1600001</code></td><td><code>exceed storage item max size</code></td><td>Android、iOS</td><td>单个 value 序列化后超过 1MB</td><td>减小单条数据体积后重试。</td></tr>
<tr><td><code>1600002</code></td><td><code>total storage size exceeded</code></td><td>Android、iOS</td><td>写入后本地缓存总量超过 10MB</td><td>先清理无用缓存后重试。</td></tr>
<tr><td><code>1600004</code></td><td><code>key is empty</code></td><td>Android、iOS</td><td>key 为空字符串</td><td>传入非空 key。</td></tr>
<tr><td><code>1600005</code></td><td><code>key exceeds max length</code></td><td>Android、iOS</td><td>key 长度超过 1024</td><td>缩短 key 长度。</td></tr>
</tbody>
</table>

#### 通用错误码

<table>
<thead>
<tr><th>errNo</th><th>errMsg</th><th>说明</th><th>平台</th></tr>
</thead>
<tbody>
<tr><td><code>102</code></td><td><code>internal error</code></td><td>内部错误</td><td>Android</td></tr>
</tbody>
</table>
