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

# Redis 存储

Agno 支持使用 `RedisStorage` 类将 Redis 作为 Agent 的存储后端。

## 用法

### 运行 Redis

安装 [docker desktop](https://docs.docker.com/desktop/install/mac-install/) 并使用以下命令在端口 **6379** 上运行 **Redis**：

```bash theme={null}
docker run --name my-redis -p 6379:6379 -d redis
```

```python redis_storage_for_agent.py theme={null}
from agno.agent import Agent
from agno.storage.redis import RedisStorage
from agno.tools.duckduckgo import DuckDuckGoTools

# 初始化 Redis 存储，使用默认的本地连接
storage = RedisStorage(
    prefix="agno_test",    # Redis 键的前缀，用于会话命名空间
    host="localhost",      # Redis 主机地址
    port=6379,             # Redis 端口号
)

# 创建带有 Redis 存储的 Agent
agent = Agent(
    storage=storage,
    tools=[DuckDuckGoTools()],
    add_history_to_messages=True,
)

agent.print_response("How many people live in Canada?")

agent.print_response("What is their national anthem called?")

# 验证存储内容
print("\nVerifying storage contents...")
all_sessions = storage.get_all_sessions()
print(f"Total sessions in Redis: {len(all_sessions)}")

if all_sessions:
    print("\nSession details:")
    session = all_sessions[0]
    print(f"Session ID: {session.session_id}")
    print(f"Messages count: {len(session.memory['messages'])}")
```

## 参数

<Snippet file="storage-redis-params.mdx" />

## 开发者资源

* 查看 [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/storage/redis_storage/redis_storage_for_agent.py)
