Skip to content

Commit

Permalink
Update to latest Redis - aioredis deprecated
Browse files Browse the repository at this point in the history
As of Feb 21, 2023 aioredis-py was archived. See the package repo
here:
https://github.com/aio-libs-abandoned/aioredis-py
  • Loading branch information
kentbull committed Jan 19, 2024
1 parent 4910dd7 commit d4e9c90
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 5 additions & 6 deletions docs/user/tutorial-asgi.rst
Original file line number Diff line number Diff line change
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
3 changes: 2 additions & 1 deletion examples/asgilook/asgilook/cache.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions examples/asgilook/asgilook/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
import pathlib
import uuid

import aioredis
def from_url_wrapper(url):
pool = redis.ConnectionPool.from_url(url)
return redis.Redis.from_pool(pool)


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
DEFAULT_REDIS_FROM_URL = from_url_wrapper

def __init__(self):
self.storage_path = pathlib.Path(
os.environ.get('ASGI_LOOK_STORAGE_PATH', self.DEFAULT_CONFIG_PATH)
)
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

0 comments on commit d4e9c90

Please sign in to comment.