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