redis 4.2.0 merged aioredis-迁移小计

背景

Add Async Support #1899

https://github.com/redis/redis-py/pull/1899

Version 4.2.0-rc1

https://github.com/redis/redis-py/releases/tag/v4.2.0rc1



迁移

官方做法

https://github.com/aio-libs/aioredis-py

Aioredis is now in redis-py 4.2.0rc1+
To install, just do pip install redis>=4.2.0rc1. The code is almost the exact same. You will just need to import like so:

from redis import asyncio as aioredis

This way you don’t have to change all your code, just the imports.

我的做法

统一使用redis库,去掉aioredis库及概念,适用于仅用异步方式使用redis场景。

aioredis【迁移前】
1
2
3
4
5
6
7
8
# 引入库
import aioredis

from aioredis.exceptions import DataError, ResponseError


pool = aioredis.ConnectionPool.from_url(url, decode_responses=True)
connection = aioredis.Redis(connection_pool=pool)
redis 【迁移后】
1
2
3
4
5
6
7
import redis.asyncio as redis  

from redis.exceptions import DataError, ResponseError


pool = redis.ConnectionPool.from_url(url, decode_responses=True)
connection = redis.Redis(connection_pool=pool)