Skip to content

Commit f918217

Browse files
committed
3.3.0 - Replaced aioredis with redis-py 4.2.0 (breaks python<3.6), fixed some deprecation warnings
1 parent 1c976ce commit f918217

File tree

9 files changed

+1771
-749
lines changed

9 files changed

+1771
-749
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2+
-----------------------------------------------------------------------------------------------------------------------
3+
4+
3.3.0 - Updated for Python 3.9
5+
====================================================================================================================
6+
7+
-----------------------------------------------------------------------------------------------------------------------
8+
9+
Author: Kale (kryogenic)
10+
Date: Thu Oct 10 11:16 AM 2024 +0000
11+
12+
- **privex.helpers.cache**
13+
- Replaced `aioredis` with redis 4.2.0 (breaks Python<3.6)
14+
115
-----------------------------------------------------------------------------------------------------------------------
216

317
3.2.0 - Added MemcachedCache + privex.helpers.cache.extra, plus various other additions

Pipfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,23 @@ semver = "*"
1414
Sphinx = ">=2.1.2"
1515
sphinx-autobuild = ">=0.7.1"
1616
restructuredtext-lint = ">=1.3.0"
17-
sphinx-rtd-theme = ">=0.4.3"
18-
docutils = ">=0.14"
17+
sphinx-rtd-theme = "==3.0.0rc4"
18+
docutils = "*"
1919
wheel = "*"
2020
setuptools = "*"
2121
mypy = "*"
2222
python-dotenv = "*"
2323

2424
[packages]
2525
privex-loghelper = "*"
26-
redis = ">=3.3"
26+
redis = ">=4.2.0"
2727
cryptography = ">=2.8"
2828
dnspython = ">=1.16"
2929
Django = "*"
3030
MarkupSafe = ">=1.1.1"
3131
wheel = "*"
3232
python-dateutil = "*"
3333
sniffio = "*"
34-
aioredis = "*"
3534
async-property = "*"
3635
aiomcache = "*"
3736
hiredis = "*"

Pipfile.lock

Lines changed: 1729 additions & 718 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

privex/helpers/cache/asyncx/AsyncRedisCache.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import pickle
33
from typing import Any, Union, Optional
44

5-
from aioredis.commands import ContextRedis
5+
66
from async_property import async_property
7+
from redis.asyncio import Redis, ConnectionPool
78

89
from privex.helpers.common import empty
910

@@ -16,7 +17,7 @@
1617
# if plugin.HAS_ASYNC_REDIS:
1718
from privex.helpers.plugin import get_redis_async, close_redis_async
1819
from privex.helpers.cache.asyncx.base import AsyncCacheAdapter
19-
import aioredis
20+
from redis import asyncio as aioredis
2021
import logging
2122

2223
log = logging.getLogger(__name__)
@@ -85,8 +86,8 @@ class AsyncRedisCache(AsyncCacheAdapter):
8586
adapter_enter_reconnect: bool = True
8687
adapter_exit_close: bool = True
8788

88-
_redis_conn: Optional[Union[aioredis.Redis, aioredis.ConnectionsPool]]
89-
_redis: Optional[ContextRedis]
89+
_redis_conn: Optional[Union[aioredis.Redis, ConnectionPool]]
90+
_redis: Optional[Redis]
9091

9192
def __init__(self, use_pickle: bool = None, redis_instance: aioredis.Redis = None, *args, **kwargs):
9293
"""
@@ -137,7 +138,10 @@ async def get(self, key: str, default: Any = None, fail: bool = False) -> Any:
137138
async def set(self, key: str, value: Any, timeout: Optional[int] = DEFAULT_CACHE_TIMEOUT):
138139
r: aioredis.Redis = await self.redis
139140
v = pickle.dumps(value) if self.use_pickle else value
140-
return await r.set(str(key), v, expire=timeout)
141+
if timeout:
142+
return await r.setex(str(key), time=timeout, value=v)
143+
else:
144+
return await r.set(str(key), v)
141145

142146
# async def get_or_set(self, key: str, value: VAL_FUNC_CORO, timeout: int = DEFAULT_CACHE_TIMEOUT) -> Any:
143147
# return await self.get_or_set_async(key=key, value=value, timeout=timeout)
@@ -162,7 +166,7 @@ async def update_timeout(self, key: str, timeout: int = DEFAULT_CACHE_TIMEOUT) -
162166
await self.set(key=key, value=v, timeout=timeout)
163167
return v
164168

165-
async def connect(self, *args, **kwargs) -> ContextRedis:
169+
async def connect(self, *args, **kwargs) -> Redis:
166170
if not self._redis_conn:
167171
self._redis_conn = await get_redis_async()
168172
if not self._redis:
@@ -172,10 +176,8 @@ async def connect(self, *args, **kwargs) -> ContextRedis:
172176
async def close(self):
173177
if self._redis is not None:
174178
log.debug("Closing AsyncIO Redis instance %s._redis", self.__class__.__name__)
175-
self._redis.close()
179+
await self._redis.aclose()
176180
self._redis = None
177-
# Closing the Redis connection directly from this method usually leads to problems...
178-
# It's safest to just set it to None and then call close_redis_async()
179181
if self._redis_conn is not None:
180182
log.debug("Clearing AsyncIO Redis connection pool %s._redis_conn", self.__class__.__name__)
181183
self._redis_conn = None

privex/helpers/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,9 +1291,9 @@ def almost(compare: NumberStr, *numbers: NumberStr, tolerance: NumberStr = Decim
12911291
return True
12921292

12931293

1294-
IS_XARGS = re.compile('^\*([a-zA-Z0-9_])+$')
1294+
IS_XARGS = re.compile(r'^\*([a-zA-Z0-9_])+$')
12951295
"""Pre-compiled regex for matching catch-all positional argument parameter names like ``*args``"""
1296-
IS_XKWARGS = re.compile('^\*\*([a-zA-Z0-9_])+$')
1296+
IS_XKWARGS = re.compile(r'^\*\*([a-zA-Z0-9_])+$')
12971297
"""Pre-compiled regex for matching catch-all keyword argument parameter names like ``**args``"""
12981298
T_PARAM = inspect.Parameter
12991299
"""Type alias for :class:`inspect.Parameter`"""

privex/helpers/extras/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from privex.helpers.extras.attrs import *
1717
HAS_ATTRS = True
1818
__all__ += ['AttribDictable']
19-
except ImportError:
19+
except (ImportError, ModuleNotFoundError):
2020
log.debug('privex.helpers.extras __init__ failed to import "attrs", not loading attrs library helpers')
2121

2222
try:

privex/helpers/plugin.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ def close_redis(thread_id=None, close_all=False) -> bool:
255255
continue
256256
log.debug("Closing redis for thread ID %s", t_id)
257257
try:
258-
rd.close()
258+
rd.aclose()
259259
except Exception:
260260
log.exception("Exception while closing redis for thread ID %s", t_id)
261261
return clean_threadstore(name='redis', clean_all=True)
262262

263263
rd: Union[str, redis.Redis] = _get_threadstore('redis', 'NOT_FOUND', thread_id=thread_id)
264264
if rd != 'NOT_FOUND':
265-
rd.close()
265+
rd.aclose()
266266
clean_threadstore(thread_id=thread_id, name='redis')
267267
return True
268268
return False
@@ -291,18 +291,14 @@ def configure_redis(host=settings.REDIS_HOST, port: int = settings.REDIS_PORT, d
291291
'Redis dependent helpers will be disabled. Exception: %s %s', type(e), str(e))
292292

293293
try:
294-
import aioredis
294+
from redis import asyncio as aioredis
295295
HAS_ASYNC_REDIS = True
296296

297297

298298
async def connect_redis_async(**rd_config) -> aioredis.Redis:
299299
from privex.helpers.common import extract_settings
300300
cf = extract_settings('REDIS_', settings, merge_conf=dict(rd_config))
301-
_addr = f"redis://{cf.pop('host', 'localhost')}:{cf.pop('port', 6379)}"
302-
return await aioredis.create_redis_pool(
303-
address=_addr, **cf
304-
)
305-
301+
return await aioredis.Redis(host=cf.pop('host', 'localhost'), port=cf.pop('port', 6379), **cf)
306302

307303
async def get_redis_async(new_connection=False, thread_id=None, **rd_config) -> aioredis.connection:
308304
"""
@@ -342,8 +338,7 @@ async def close_redis_async(thread_id=None, close_all=False) -> bool:
342338
continue
343339
log.debug("Closing aioredis for thread ID %s", t_id)
344340
try:
345-
rd.close()
346-
await rd.wait_closed()
341+
await rd.aclose()
347342
except RuntimeError as err:
348343
log.warning("[ignored] RuntimeError while closing aioredis (thread %s): %s - %s", thread_id, type(err), str(err))
349344
except Exception:
@@ -353,8 +348,7 @@ async def close_redis_async(thread_id=None, close_all=False) -> bool:
353348
rd = _get_threadstore('aioredis', 'NOT_FOUND', thread_id=thread_id)
354349
if rd != 'NOT_FOUND':
355350
try:
356-
rd.close()
357-
await rd.wait_closed()
351+
await rd.aclose()
358352
except RuntimeError as err:
359353
log.warning("[ignored] RuntimeError while closing aioredis (thread %s): %s - %s", thread_id, type(err), str(err))
360354
clean_threadstore(thread_id=thread_id, name='aioredis')

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
filterwarnings =
33
ignore:.*coroutine" decorator is deprecated:DeprecationWarning
44
ignore:.*loop argument is deprecated:DeprecationWarning
5+
markers =
6+
asyncio: asyncio mark
7+
asyncio_mode = auto

requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ python-dateutil
44
sniffio
55

66
# Extra [cache] - file: extras/cache.txt
7-
redis>=3.3
8-
aioredis>=1.3
7+
redis>=4.2.0rc1
98
hiredis
109
aiomcache>=0.6
1110

@@ -39,6 +38,6 @@ semver
3938
Sphinx>=3.1.1
4039
sphinx-autobuild>=0.7.1
4140
restructuredtext-lint>=1.3.0
42-
sphinx-rtd-theme>=0.5.0
41+
sphinx-rtd-theme>=3.0.0rc4
4342
MarkupSafe>=1.1.1
4443
docutils>=0.14

0 commit comments

Comments
 (0)