aredis快速上手:5步用asyncio连接Redis的完整新手教程

📅 2026/8/23 13:21:33
aredis快速上手:5步用asyncio连接Redis的完整新手教程
aredis快速上手5步用asyncio连接Redis的完整新手教程【免费下载链接】aredisredis client for Python asyncio (has support for redis server, sentinel and cluster)项目地址: https://gitcode.com/gh_mirrors/ar/aredisaredis 是一个专为 Python asyncio 打造的异步 Redis 客户端Redis client for Python asyncio移植自广受欢迎的 redis-py同时支持 Redis 单节点、Sentinel 哨兵与 Cluster 集群三种部署模式。本教程面向新手通过 5 个步骤带你完整上手 aredis从安装配置到连接 Redis再到读写数据与进阶玩法照着做即可跑通自己的第一个异步 Redis 程序。 为什么选择 aredis在异步 Python 应用如 aiohttp、FastAPI、Sanic、Tornado 服务中操作 Redis同步客户端会阻塞事件循环拖慢整体性能。aredis 用 asyncio 原生方式实现了完整的 Redis 命令集带来以下好处全异步所有命令均为await协程调用不阻塞事件循环API 与 redis-py 高度一致会 redis-py 就会 aredis迁移成本极低一套代码支持多种架构单节点、哨兵、集群StrictRedisCluster性能加速可选搭配hiredis与uvloop进一步提速见 docs 文档说明第1步准备工作——安装 Redis 服务器与 Python 环境 ⚙️aredis 只是客户端你需要先准备一个正在运行的 Redis 服务器。依赖要求Redis 服务器单节点任意稳定版集群模式需 3.0 以上Python3.5 及以上推荐 3.7确认 Python 版本python --version 小技巧Python 3.8 自带 asyncio 交互模式可以直接在 REPL 里调试 aredispython -m asyncio第2步一行命令安装 aredis 打开终端执行pip3 install aredis[hiredis]hiredis是 C 语言实现的 RESP 解析器能显著提升性能推荐带上如果希望极致性能还可以再装uvloop替换默认事件循环也可以从源码安装克隆仓库后运行python setup.py install仓库地址https://gitcode.com/gh_mirrors/ar/aredis安装完成后验证一下import aredis print(aredis.__version__) # 例如 1.1.8版本信息定义在 aredis/init.py 中。第3步创建 aredis 客户端并连接 Redis 创建客户端只需三行代码。连接采用惰性方式——创建对象时并不真正建立连接首次执行命令时才自动连接所以即使服务器暂时不可用构造StrictRedis也不会报错。import asyncio from aredis import StrictRedis async def main(): client StrictRedis(host127.0.0.1, port6379, db0) await client.set(foo, bar) print(await client.get(foo)) # bbar asyncio.run(main())常用连接参数定义见 aredis/client.py参数说明host/portRedis 地址与端口默认localhost:6379db数据库编号默认 0password密码未设置可不传decode_responses设为True后返回字符串而非字节推荐新手开启想要更底层的连接操作如手动重连、直接发送命令可以参考 examples/connection.py。第4步读写数据——最常用的 5 个操作 ✍️异步 Redis 客户端的核心价值就是流畅的读写体验。下面这组最常用的操作覆盖了日常 80% 的场景await client.set(count, 1) # 写入 print(await client.get(count)) # 读取 → b1 await client.incr(count, 100) # 自增 await client.expire(count, 60) # 设置 60 秒过期 print(await client.ttl(count)) # 查询剩余生存时间⚠️新手注意默认情况下 aredis 返回的是字节类型如b1需要时手动int()或decode()在创建客户端时传decode_responsesTrue则直接返回字符串。更多完整示例可运行 examples/keys.py它演示了 set、get、incr、expire、ttl 的全流程并附带集群版写法。第5步进阶玩法——Pipeline、集群与发布订阅 5.1 Pipeline 批量执行减少网络往返多条命令打包一次发送是提升吞吐的关键手段async with await client.pipeline(transactionTrue) as pipe: await pipe.set(foo, bar) await pipe.keys(*) res await pipe.execute() # 结果按命令顺序返回完整可运行示例见 examples/pipeline.py。5.2 连接 Redis Cluster 集群把StrictRedis换成StrictRedisCluster即可接入集群客户端会自动处理槽位slot路由与 MOVED 重定向from aredis import StrictRedisCluster client StrictRedisCluster(host127.0.0.1, port7001) await client.set(foo, 1) print(await client.cluster_slots())节点管理逻辑位于 aredis/nodemanager.py集群命令封装在 aredis/commands/cluster.py。5.3 Pub/Sub 发布订阅pubsub client.pubsub() await pubsub.subscribe(channel1) await client.publish(channel1, hello aredis) message await pubsub.get_message()完整的双线程收发示例见 examples/pubsub.py。此外aredis 还内置了缓存装饰器aredis/cache.py配合 examples/cache_decorator.py 使用、分布式锁aredis/lock.py和 Stream 流操作examples 与 docs/streams 说明可按需解锁。❓ 新手常见问题FAQQ1连接时报ConnectionError先确认 Redis 服务器已启动redis-cli ping应返回PONG。aredis 在首次执行命令时才真正建连。Q2为什么返回值是b101这样的字节这是 Python 3 的二进制安全设计传decode_responsesTrue即可返回字符串。Q3能和 redis-py 互换吗API 基本一一对应主要区别只是所有命令加await。你的 redis-py 经验可以直接迁移。Q4Web 框架里怎么用aredis 与 Sanic、Tornado 等异步框架天然契合可参考 examples/sanic_server.py 和 examples/tornado_server.py。 总结步骤内容关键命令/类1准备 Redis 服务器 Python 3.5python -m asyncio2安装 aredispip3 install aredis[hiredis]3创建客户端StrictRedis(...)4读写数据set/get/incr/expire5进阶功能pipeline/StrictRedisCluster/pubsubaredis 让你用 asyncio 优雅地连接 Redis单节点、哨兵、集群一套 API 全部搞定。从今天的 5 个步骤出发去 examples 目录examples/里挑一个示例跑一遍你就能快速掌握这个异步 Redis 客户端的全部精髓。【免费下载链接】aredisredis client for Python asyncio (has support for redis server, sentinel and cluster)项目地址: https://gitcode.com/gh_mirrors/ar/aredis创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考