Skip to content

Commit

Permalink
chore: update to latest Redis (aioredis is deprecated) (#2204)
Browse files Browse the repository at this point in the history
* 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 <vytautas.liuolia@gmail.com>
  • Loading branch information
kentbull and vytas7 committed Mar 2, 2024
1 parent 4910dd7 commit dc8d2d4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
13 changes: 6 additions & 7 deletions docs/user/tutorial-asgi.rst
Expand Up @@ -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 <https://github.com/aio-libs/aioredis>`_ for async
advantage of `redis <https://redis.readthedocs.io/en/stable/examples/asyncio_examples.html>`_ 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::
Expand All @@ -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 <https://github.com/jd/pifpaf>`_ for spinning up Redis just
Expand Down Expand Up @@ -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 <asgi_tutorial_config>` items
``redis_host`` and ``redis_from_url()``, respectively, the final version of
``config.py`` now reads:
Assuming we call our new :ref:`configuration <asgi_tutorial_config>` item
``redis_host`` the final version of ``config.py`` now reads:

.. literalinclude:: ../../examples/asgilook/asgilook/config.py
:language: python
Expand Down Expand Up @@ -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):

Expand Down
3 changes: 2 additions & 1 deletion examples/asgilook/asgilook/cache.py
@@ -1,4 +1,5 @@
import msgpack
import redis.asyncio as redis


class RedisCache:
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions 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):
Expand All @@ -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
2 changes: 1 addition & 1 deletion examples/asgilook/requirements/asgilook
@@ -1,4 +1,4 @@
aiofiles>=0.4.0
aioredis>=2.0
redis>=5.0
msgpack
Pillow>=6.0.0

2 comments on commit dc8d2d4

@kloczek
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to release new version because of this commit? 🤔

@CaselIT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only an example, there is no change to the falcon library here

Please sign in to comment.