> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Redis 集成

> 使用 LangChain Python 与 Redis 集成。

> [Redis（远程字典服务器）](https://en.wikipedia.org/wiki/Redis) 是一个开源的内存存储，
> 用作分布式内存键值数据库、缓存和消息代理，并支持可选的持久化。
> 由于它将所有数据保存在内存中，并且其设计特点，`Redis` 提供了低延迟的读写操作，
> 这使其特别适用于需要缓存的用例。Redis 是最流行的 NoSQL 数据库，
> 也是整体上最受欢迎的数据库之一。

本页介绍如何在 LangChain 中使用 [Redis](https://redis.com) 生态系统。
内容分为两部分：安装与设置，以及特定 Redis 包装器的参考。

## 安装与设置

安装 Python SDK 和 LangChain Redis 集成：

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install redis langchain-redis
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add redis langchain-redis
  ```
</CodeGroup>

要在本地运行 Redis，可以使用 Docker：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docker run --name langchain-redis -d -p 6379:6379 redis redis-server --save 60 1 --loglevel warning
```

停止容器：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docker stop langchain-redis
```

再次启动：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docker start langchain-redis
```

### 连接

我们需要一个 Redis URL 连接字符串来连接数据库，支持独立 Redis 服务器或带有复制和 Redis 哨兵的高可用性设置。

#### Redis 独立连接 URL

对于独立 `Redis` 服务器，可以使用 Python redis 模块中 `from_url()` 方法描述的官方 Redis 连接 URL 格式，参见 [Redis.from\_url](https://redis-py.readthedocs.io/en/stable/connections.html#redis.Redis.from_url)

示例：`redis_url = "redis://:secret-pass@localhost:6379/0"`

#### Redis 哨兵连接 URL

对于 [Redis 哨兵设置](https://redis.io/docs/management/sentinel/)，连接方案是 "redis+sentinel"。
这是对官方 IANA 注册协议方案的非官方扩展，因为目前没有用于哨兵的连接 URL。

示例：`redis_url = "redis+sentinel://:secret-pass@sentinel-host:26379/mymaster/0"`

格式为 `redis+sentinel://$USERNAME:$PASSWORD@$HOST_OR_IP:$PORT/$SERVICE_NAME/$DB_NUMBER`，
如果未明确设置，"service-name" 默认值为 "mymaster"，"db-number" 默认值为 "0"。
service-name 是在哨兵中配置的 Redis 服务器监控组名称。

当前 URL 格式将连接字符串限制为仅一个哨兵主机（无法提供列表），并且 Redis 服务器和哨兵必须设置相同的密码（如果使用密码）。

#### Redis 集群连接 URL

目前，所有需要 "redis\_url" 参数的方法都不支持 Redis 集群。
使用 Redis 集群的唯一方式是通过接受预配置 Redis 客户端的 LangChain 类，例如 `RedisCache`（示例见下文）。

## 缓存

缓存包装器允许将 [Redis](https://redis.io) 用作 LLM 提示和响应的远程、低延迟、内存缓存。

### 标准缓存

标准缓存是 Redis 在全球 [开源](https://redis.io) 和 [企业](https://redis.com) 用户生产环境中的核心用例。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_redis import RedisCache
```

要将此缓存与你的 LLM 一起使用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.globals import set_llm_cache
import redis

redis_client = redis.Redis.from_url(...)
set_llm_cache(RedisCache(redis_client))
```

### 语义缓存

语义缓存允许用户根据用户输入与先前缓存结果之间的语义相似性来检索缓存的提示。其底层将 Redis 同时用作缓存和向量存储。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_redis import RedisSemanticCache
```

要将此缓存与你的 LLM 一起使用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.globals import set_llm_cache
import redis

# 使用任何嵌入提供程序...
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings

redis_url = "redis://localhost:6379"

set_llm_cache(RedisSemanticCache(
    embedding=FakeEmbeddings(),
    redis_url=redis_url
))
```

## 向量存储

向量存储包装器将 Redis 转换为用于语义搜索或 LLM 内容检索的低延迟[向量数据库](https://redis.com/solutions/use-cases/vector-database/)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.vectorstores import Redis
```

## 检索器

Redis 向量存储检索器包装器将向量存储类泛化，以执行低延迟的文档检索。要创建检索器，只需在基础向量存储类上调用 `.as_retriever()`。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/providers/redis.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
