Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to latest Redis (aioredis is deprecated) #2204

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

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

This note might need to be updated or even removed altogether if it is no longer needed. How can we hook in fakeredis for testing then though? (Monkey-patching or mocking might work too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

with the latest update I made the redis_from_url function is still used. I added a wrapper function to config.py to change as little as possible. Monkey patching still works.

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
Loading