本指南介绍如何使用 Upstash Redis 键值存储。关于 UpstashRedisByteStore 的全部功能与配置,请参阅 API 参考

Overview

UpstashRedisStore 会将数据存放在 Upstash 托管的 Redis 中。如需使用基础版本请参见Redis 指南

Integration details

ClassPackageLocalJS supportDownloadsVersion
UpstashRedisByteStorelangchain-communityPyPI - DownloadsPyPI - Version

设置

首先注册 Upstash 帐号,并创建 Redis 数据库。

凭证

数据库创建完成后,获取带 https:// 的 REST URL 和 Token:
from getpass import getpass

URL = getpass("Enter your Upstash URL")
TOKEN = getpass("Enter your Upstash REST token")

安装

该集成位于 langchain-community 包,同时需要安装 upstash-redis 依赖:
pip install -qU langchain-community upstash-redis

实例化

创建 ByteStore:
from langchain_community.storage import UpstashRedisByteStore
from upstash_redis import Redis

redis_client = Redis(url=URL, token=TOKEN)
kv_store = UpstashRedisByteStore(client=redis_client, ttl=None, namespace="test-ns")

用法

使用 mset 写入:
kv_store.mset(
    [
        ["key1", b"value1"],
        ["key2", b"value2"],
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
[b'value1', b'value2']
使用 mdelete 删除:
kv_store.mdelete(
    [
        "key1",
        "key2",
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
[None, None]

API 参考

更多 UpstashRedisByteStore 内容请参阅 API 文档
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.