Files
dify-docs/plugin_dev_zh/0411-persistent-storage-kv.zh.mdx
2025-05-16 19:54:25 +08:00

86 lines
2.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
dimensions:
type:
primary: reference
detail: core
level: beginner
standard_title: Persistent Storage KV
language: zh
title: 持久化存储
description: 本文档介绍了Dify插件中的持久化存储功能详细说明了如何在插件中使用KV数据库来储存、获取和删除数据。这一功能使插件能够在相同Workspace内持久化存储数据满足跨会话数据保存的需求。
---
如果单独审视插件中的 Tool 及 Endpoint不难发现大多数情况下其只能完成单轮交互请求后返回数据任务结束。
如果有需要长期储存的数据,如实现持久化的记忆,需要插件具备持久化存储能力。**持久化存储机制能够让插件具备在相同 Workspace 持久存储数据的能力**,目前通过提供 KV 数据库满足存储需求,未来可能会根据实际的使用情况推出更灵活更强大的储存接口。
### 储存 Key
#### **入口**
```python
self.session.storage
```
#### **接口**
```python
def set(self, key: str, val: bytes) -> None:
pass
```
可以注意到传入的是一个 bytes因此实际上你可以在其中储存文件。
### 获取 Key
#### **入口**
```python
self.session.storage
```
#### **接口**
```python
def get(self, key: str) -> bytes:
pass
```
### 删除 Key
#### **入口**
```python
self.session.storage
```
#### **接口**
```python
def delete(self, key: str) -> None:
pass
```
{/*
Contributing Section
DO NOT edit this section!
It will be automatically generated by the script.
*/}
<CardGroup cols="2">
<Card
title="编辑此页面"
icon="pen-to-square"
href="https://github.com/langgenius/dify-docs-mintlify/edit/main/plugin_dev_zh/0411-persistent-storage-kv.zh.mdx"
>
通过直接提交修改来帮助改进文档内容
</Card>
<Card
title="提交问题"
icon="github"
href="https://github.com/langgenius/dify-docs-mintlify/issues/new?title=文档问题%3A%20persistent-storage-kv&body=%23%23%20问题描述%0A%3C%21--%20请简要描述您发现的问题%20--%3E%0A%0A%23%23%20页面链接%0Ahttps%3A%2F%2Fgithub.com%2Flanggenius%2Fdify-docs-mintlify%2Fblob%2Fmain%2Fplugin_dev_zh%2F0411-persistent-storage-kv.zh.mdx%0A%0A%23%23%20建议修改%0A%3C%21--%20如果有具体的修改建议请在此说明%20--%3E%0A%0A%3C%21--%20感谢您对文档质量的关注%20--%3E"
>
发现错误或有改进建议?请提交问题反馈
</Card>
</CardGroup>