From 4ac663bd2c81668f24fd63af30a01d40a7f622bf Mon Sep 17 00:00:00 2001 From: Kent Bull Date: Wed, 17 Jan 2024 08:44:37 -0700 Subject: [PATCH] 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 --- docs/user/tutorial-asgi.rst | 11 +++++------ examples/asgilook/asgilook/cache.py | 4 +++- examples/asgilook/asgilook/config.py | 4 ---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/docs/user/tutorial-asgi.rst b/docs/user/tutorial-asgi.rst index 7d4909a87..12b9682c0 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 diff --git a/examples/asgilook/asgilook/cache.py b/examples/asgilook/asgilook/cache.py index 02af68778..7a9a0123b 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: @@ -9,7 +10,8 @@ class RedisCache: def __init__(self, config): self._config = config - self._redis = self._config.redis_from_url(self._config.redis_host) + pool = redis.ConnectionPool.from_url(self._config.redis_host) + self._redis = redis.Redis.from_pool(pool) async def _serialize_response(self, resp): data = await resp.render_body() diff --git a/examples/asgilook/asgilook/config.py b/examples/asgilook/asgilook/config.py index 3e0870fba..ecacf823c 100644 --- a/examples/asgilook/asgilook/config.py +++ b/examples/asgilook/asgilook/config.py @@ -2,14 +2,11 @@ import pathlib import uuid -import aioredis - class Config: DEFAULT_CONFIG_PATH = '/tmp/asgilook' DEFAULT_MIN_THUMB_SIZE = 64 DEFAULT_REDIS_HOST = 'redis://localhost' - DEFAULT_REDIS_FROM_URL = aioredis.from_url DEFAULT_UUID_GENERATOR = uuid.uuid4 def __init__(self): @@ -18,7 +15,6 @@ 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_host = self.DEFAULT_REDIS_HOST self.uuid_generator = Config.DEFAULT_UUID_GENERATOR