Skip to content

Commit f05c979

Browse files
authored
Avoid with await redis_pool (#330)
1 parent 31c6bbc commit f05c979

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

aiohttp_session/redis_storage.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,17 @@ async def load_session(self, request: web.Request) -> Session:
5555
if cookie is None:
5656
return Session(None, data=None, new=True, max_age=self.max_age)
5757
else:
58-
with await self._redis as conn:
59-
key = str(cookie)
60-
data = await conn.get(self.cookie_name + '_' + key)
61-
if data is None:
62-
return Session(None, data=None,
63-
new=True, max_age=self.max_age)
64-
data = data.decode('utf-8')
65-
try:
66-
data = self._decoder(data)
67-
except ValueError:
68-
data = None
69-
return Session(key, data=data, new=False, max_age=self.max_age)
58+
key = str(cookie)
59+
data = await self._redis.get(self.cookie_name + '_' + key)
60+
if data is None:
61+
return Session(None, data=None,
62+
new=True, max_age=self.max_age)
63+
data = data.decode('utf-8')
64+
try:
65+
data = self._decoder(data)
66+
except ValueError:
67+
data = None
68+
return Session(key, data=data, new=False, max_age=self.max_age)
7069

7170
async def save_session(
7271
self,
@@ -89,7 +88,8 @@ async def save_session(
8988
max_age=session.max_age)
9089

9190
data = self._encoder(self._get_session_data(session))
92-
with await self._redis as conn:
93-
max_age = session.max_age
94-
expire = max_age if max_age is not None else 0
95-
await conn.set(self.cookie_name + '_' + key, data, expire=expire)
91+
max_age = session.max_age
92+
expire = max_age if max_age is not None else 0
93+
await self._redis.set(self.cookie_name + '_' + key,
94+
data,
95+
expire=expire)

tests/test_redis_storage.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ async def make_cookie(
4040
}
4141
value = json.dumps(session_data)
4242
key = uuid.uuid4().hex
43-
with await redis as conn:
44-
await conn.set('AIOHTTP_SESSION_' + key, value)
43+
await redis.set('AIOHTTP_SESSION_' + key, value)
4544
# Ignoring type until aiohttp#4252 is released
4645
client.session.cookie_jar.update_cookies(
4746
{'AIOHTTP_SESSION': key} # type: ignore
@@ -53,8 +52,7 @@ async def make_cookie_with_bad_value(
5352
redis: aioredis.commands.Redis
5453
) -> None:
5554
key = uuid.uuid4().hex
56-
with await redis as conn:
57-
await conn.set('AIOHTTP_SESSION_' + key, '')
55+
await redis.set('AIOHTTP_SESSION_' + key, '')
5856
# Ignoring type until aiohttp#4252 is released
5957
client.session.cookie_jar.update_cookies(
6058
{'AIOHTTP_SESSION': key} # type: ignore
@@ -67,11 +65,10 @@ async def load_cookie(
6765
) -> Any:
6866
cookies = client.session.cookie_jar.filter_cookies(client.make_url('/'))
6967
key = cookies['AIOHTTP_SESSION']
70-
with await redis as conn:
71-
encoded = await conn.get('AIOHTTP_SESSION_' + key.value)
72-
s = encoded.decode('utf-8')
73-
value = json.loads(s)
74-
return value
68+
encoded = await redis.get('AIOHTTP_SESSION_' + key.value)
69+
s = encoded.decode('utf-8')
70+
value = json.loads(s)
71+
return value
7572

7673

7774
async def test_create_new_session(
@@ -207,9 +204,8 @@ async def handler(request: web.Request) -> web.StreamResponse:
207204
morsel = resp.cookies['AIOHTTP_SESSION']
208205
assert morsel['httponly']
209206
assert morsel['path'] == '/'
210-
with await redis as conn:
211-
exists = await conn.exists('AIOHTTP_SESSION_' + morsel.value)
212-
assert exists
207+
exists = await redis.exists('AIOHTTP_SESSION_' + morsel.value)
208+
assert exists
213209

214210

215211
async def test_set_ttl_on_session_saving(
@@ -228,8 +224,7 @@ async def handler(request: web.Request) -> web.StreamResponse:
228224

229225
key = resp.cookies['AIOHTTP_SESSION'].value
230226

231-
with await redis as conn:
232-
ttl = await conn.ttl('AIOHTTP_SESSION_'+key)
227+
ttl = await redis.ttl('AIOHTTP_SESSION_'+key)
233228

234229
assert ttl > 9
235230
assert ttl <= 10
@@ -252,8 +247,7 @@ async def handler(request: web.Request) -> web.StreamResponse:
252247

253248
key = resp.cookies['AIOHTTP_SESSION'].value
254249

255-
with await redis as conn:
256-
ttl = await conn.ttl('AIOHTTP_SESSION_'+key)
250+
ttl = await redis.ttl('AIOHTTP_SESSION_'+key)
257251

258252
assert ttl > 9
259253
assert ttl <= 10
@@ -341,9 +335,8 @@ async def test_redis_from_create_pool(
341335
async def handler(request: web.Request) -> web.StreamResponse:
342336
pass
343337

344-
redis = await aioredis.create_pool(**redis_params)
345-
with pytest.warns(DeprecationWarning):
346-
create_app(handler=handler, redis=redis)
338+
redis = await aioredis.create_redis(**redis_params)
339+
create_app(handler=handler, redis=redis)
347340

348341

349342
async def test_not_redis_provided_to_storage() -> None:

0 commit comments

Comments
 (0)