Repository: falconry/falcon

Issue:
Update asgi look examples to use aioredis 2
A new major vesion of aioredis was released, and it has some api changes.
This is the changelog https://github.com/aio-libs/aioredis-py/blob/master/CHANGELOG.md


Discussion:
Myself and [Mihai Todor](https://github.com/mihaitodor) can pick this up and work on it, if nobody else is!
Awesome @the-bets & @mihaitodor, go ahead!
(Bonus points if you find and fix any other funky things in that tutorial.)
Just FYI, I believe we figured out the required changes and we'll try to push a polished PR in the next few days.
Good to hear @mihaitodor! Let us know if you run into any issues.

Resolving patch:
diff --git a/examples/asgilook/asgilook/cache.py b/examples/asgilook/asgilook/cache.py
--- a/examples/asgilook/asgilook/cache.py
+++ b/examples/asgilook/asgilook/cache.py
@@ -9,10 +9,7 @@ class RedisCache:
 
     def __init__(self, config):
         self._config = config
-
-        # NOTE(vytas): To be initialized upon application startup (see the
-        #   method below).
-        self._redis = None
+        self._redis = self._config.redis_from_url(self._config.redis_host)
 
     async def _serialize_response(self, resp):
         data = await resp.render_body()
@@ -24,8 +21,10 @@ def _deserialize_response(self, resp, data):
         resp.context.cached = True
 
     async def process_startup(self, scope, event):
-        if self._redis is None:
-            self._redis = await self._config.create_redis_pool(self._config.redis_host)
+        await self._redis.ping()
+
+    async def process_shutdown(self, scope, event):
+        await self._redis.close()
 
     async def process_request(self, req, resp):
         resp.context.cached = False
@@ -51,4 +50,4 @@ async def process_response(self, req, resp, resource, req_succeeded):
             await self._redis.delete(key)
         elif not resp.context.cached:
             data = await self._serialize_response(resp)
-            await self._redis.set(key, data, expire=self.TTL)
+            await self._redis.set(key, data, ex=self.TTL)
diff --git a/examples/asgilook/asgilook/config.py b/examples/asgilook/asgilook/config.py
--- a/examples/asgilook/asgilook/config.py
+++ b/examples/asgilook/asgilook/config.py
@@ -9,7 +9,7 @@ class Config:
     DEFAULT_CONFIG_PATH = '/tmp/asgilook'
     DEFAULT_MIN_THUMB_SIZE = 64
     DEFAULT_REDIS_HOST = 'redis://localhost'
-    DEFAULT_REDIS_POOL = aioredis.create_redis_pool
+    DEFAULT_REDIS_FROM_URL = aioredis.from_url
     DEFAULT_UUID_GENERATOR = uuid.uuid4
 
     def __init__(self):
@@ -18,7 +18,7 @@ def __init__(self):
         )
         self.storage_path.mkdir(parents=True, exist_ok=True)
 
-        self.create_redis_pool = Config.DEFAULT_REDIS_POOL
+        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


Test patch:
diff --git a/examples/asgilook/tests/conftest.py b/examples/asgilook/tests/conftest.py
--- a/examples/asgilook/tests/conftest.py
+++ b/examples/asgilook/tests/conftest.py
@@ -39,8 +39,8 @@ def storage_path(tmpdir_factory):
 @pytest.fixture
 def client(predictable_uuid, storage_path):
     config = Config()
-    config.create_redis_pool = fakeredis.aioredis.create_redis_pool
-    config.redis_host = None
+    config.redis_from_url = fakeredis.aioredis.FakeRedis.from_url
+    config.redis_host = 'redis://localhost'
     config.storage_path = storage_path
     config.uuid_generator = predictable_uuid