aioredis升级到2.0后,会发现create_redis_pool来创建连接池的方法无法使用,源码中也找不到这个方法了。
通过官方文档查看得知,2.0后已经舍弃了create_redis_pool的连接方式,整个核心和公共 API 已被重新编写,以尽可能地遵循redis-py的实现不再通过调用来连接到 redis await aioredis.create_pool(...)。相反,默认的初始化模式与redis-py相同:
连接示例:
import asyncio import aioredis async def main(): # Redis client bound to single connection (no auto reconnection). redis = aioredis.from_url( "redis://localhost", encoding="utf-8", decode_responses=True ) async with redis.client() as conn: await conn.set("my-key", "value") val = await conn.get("my-key") print(val) async def redis_pool(): # Redis client bound to pool of connections (auto-reconnecting). redis = aioredis.from_url( "redis://localhost", encoding="utf-8", decode_responses=True ) await redis.set("my-key", "value") val = await redis.get("my-key") print(val) if __name__ == "__main__": asyncio.run(main()) asyncio.run(redis_pool())
如果redis更改了端口,密码或者需要指定DB等,像pyredis一样直接指定参数即可
redis = aioredis.from_url( "redis://127.0.0.1", port=44117, password='qwaszx', db=2, encoding="utf-8", decode_responses=True )
参考:
https://aioredis.readthedocs.io/en/latest/migration/#connecting-to-redis