From dc8d2d430381a82ef52eb4229b023c1cd1dd809e Mon Sep 17 00:00:00 2001 From: Kent Bull <65027257+kentbull@users.noreply.github.com> Date: Sat, 2 Mar 2024 06:22:28 -0700 Subject: [PATCH] chore: update to latest Redis (aioredis is deprecated) (#2204) * Update to latest Redis - aioredis deprecated As of Feb 21, 2023 aioredis-py was archived. See the package repo here: https://github.com/aio-libs-abandoned/aioredis-py * style: update config.py * fix(asgilook): directly use `redis.asyncio.from_url()` --------- Co-authored-by: Vytautas Liuolia --- docs/user/tutorial-asgi.rst | 13 ++++++------- examples/asgilook/asgilook/cache.py | 3 ++- examples/asgilook/asgilook/config.py | 7 +++---- examples/asgilook/requirements/asgilook | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/docs/user/tutorial-asgi.rst b/docs/user/tutorial-asgi.rst index 7d4909a87..65ffa2fa9 100644 --- a/docs/user/tutorial-asgi.rst +++ b/docs/user/tutorial-asgi.rst @@ -685,10 +685,10 @@ small files littering our storage, it consumes CPU resources, and we would soon find our application crumbling under load. Let's mitigate this problem with response caching. We'll use Redis, taking -advantage of `aioredis `_ for async +advantage of `redis `_ for async support:: - pip install aioredis + pip install redis We will also need to serialize response data (the ``Content-Type`` header and the body in the first version); ``msgpack`` should do:: @@ -700,7 +700,7 @@ installing Redis server on your machine, one could also: * Spin up Redis in Docker, eg:: - docker run -p 6379:6379 redis + docker run -p 6379:6379 redis/redis-stack:latest * Assuming Redis is installed on the machine, one could also try `pifpaf `_ for spinning up Redis just @@ -747,9 +747,8 @@ implementations for production and testing. ``self.redis_host``. Such a design might prove helpful for apps that need to create client connections in more than one place. -Assuming we call our new :ref:`configuration ` items -``redis_host`` and ``redis_from_url()``, respectively, the final version of -``config.py`` now reads: +Assuming we call our new :ref:`configuration ` item +``redis_host`` the final version of ``config.py`` now reads: .. literalinclude:: ../../examples/asgilook/asgilook/config.py :language: python @@ -860,7 +859,7 @@ any problems with importing local utility modules or checking code coverage:: $ mkdir -p tests $ touch tests/__init__.py -Next, let's implement fixtures to replace ``uuid`` and ``aioredis``, and inject them +Next, let's implement fixtures to replace ``uuid`` and ``redis``, and inject them into our tests via ``conftest.py`` (place your code in the newly created ``tests`` directory): diff --git a/examples/asgilook/asgilook/cache.py b/examples/asgilook/asgilook/cache.py index 02af68778..e4819d0d0 100644 --- a/examples/asgilook/asgilook/cache.py +++ b/examples/asgilook/asgilook/cache.py @@ -1,4 +1,5 @@ import msgpack +import redis.asyncio as redis class RedisCache: @@ -24,7 +25,7 @@ async def process_startup(self, scope, event): await self._redis.ping() async def process_shutdown(self, scope, event): - await self._redis.close() + await self._redis.aclose() async def process_request(self, req, resp): resp.context.cached = False diff --git a/examples/asgilook/asgilook/config.py b/examples/asgilook/asgilook/config.py index 3e0870fba..e5ac12c5b 100644 --- a/examples/asgilook/asgilook/config.py +++ b/examples/asgilook/asgilook/config.py @@ -1,15 +1,14 @@ import os import pathlib +import redis.asyncio import uuid -import aioredis - class Config: DEFAULT_CONFIG_PATH = '/tmp/asgilook' DEFAULT_MIN_THUMB_SIZE = 64 + DEFAULT_REDIS_FROM_URL = redis.asyncio.from_url DEFAULT_REDIS_HOST = 'redis://localhost' - DEFAULT_REDIS_FROM_URL = aioredis.from_url DEFAULT_UUID_GENERATOR = uuid.uuid4 def __init__(self): @@ -18,7 +17,7 @@ def __init__(self): ) self.storage_path.mkdir(parents=True, exist_ok=True) - self.redis_from_url = Config.DEFAULT_REDIS_FROM_URL self.min_thumb_size = self.DEFAULT_MIN_THUMB_SIZE + self.redis_from_url = Config.DEFAULT_REDIS_FROM_URL self.redis_host = self.DEFAULT_REDIS_HOST self.uuid_generator = Config.DEFAULT_UUID_GENERATOR diff --git a/examples/asgilook/requirements/asgilook b/examples/asgilook/requirements/asgilook index c0081cbf5..7f716db72 100644 --- a/examples/asgilook/requirements/asgilook +++ b/examples/asgilook/requirements/asgilook @@ -1,4 +1,4 @@ aiofiles>=0.4.0 -aioredis>=2.0 +redis>=5.0 msgpack Pillow>=6.0.0