Skip to content

Commit e9e57e3

Browse files
refactor(redis): simplify get_redis
1 parent b681b3a commit e9e57e3

3 files changed

Lines changed: 11 additions & 17 deletions

File tree

nexios_contrib/redis/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
This module provides Redis client initialization, dependency injection,
55
"""
6+
67
from __future__ import annotations
78

89
import json
@@ -16,7 +17,6 @@
1617
from .config import RedisConfig
1718

1819

19-
2020
if TYPE_CHECKING:
2121
from nexios.dependencies import Context
2222

@@ -31,6 +31,7 @@
3131

3232
class RedisConnectionError(Exception):
3333
"""Raised when there's an error connecting to Redis."""
34+
3435
pass
3536

3637

@@ -79,11 +80,7 @@ def init_redis(
7980
global _redis_client
8081

8182
config = RedisConfig(
82-
url=url,
83-
db=db,
84-
password=password,
85-
decode_responses=decode_responses,
86-
**kwargs
83+
url=url, db=db, password=password, decode_responses=decode_responses, **kwargs
8784
)
8885

8986
_redis_client = RedisClient(config)
@@ -113,8 +110,10 @@ async def _close_redis() -> None:
113110
app.on_startup(_init_redis)
114111
app.on_shutdown(_close_redis)
115112

113+
return _redis_client
114+
116115

117-
def get_redis(context: Optional["Context"] = None) -> RedisClient:
116+
def get_redis() -> RedisClient:
118117
"""
119118
Get the Redis client instance from the current context.
120119
@@ -150,7 +149,9 @@ async def get_cached_data(
150149
"""
151150
global _redis_client
152151
if _redis_client is None:
153-
raise RedisConnectionError("Redis client not initialized. Call init_redis() first.")
152+
raise RedisConnectionError(
153+
"Redis client not initialized. Call init_redis() first."
154+
)
154155
return _redis_client
155156

156157

nexios_contrib/redis/dependency.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
from __future__ import annotations
99

10-
from typing import Any, Optional, Union
1110
from nexios.dependencies import Depend, Context
12-
from nexios.http import Request
1311

1412
from .client import RedisClient
1513
from . import get_redis
@@ -44,7 +42,4 @@ async def get_cached_data(
4442
```
4543
"""
4644

47-
def _wrap(context: Context = Context()) -> RedisClient:
48-
return get_redis(context)
49-
50-
return Depend(_wrap)
45+
return Depend(get_redis)

tests/test_redis/test_redis_dependencies.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ def test_redis_depend_with_context(self, app_with_mock_redis, mock_redis):
5858
redis_dep = RedisDepend()
5959

6060
# Create a mock context
61-
context = Context()
62-
6361
# Call the dependency function
64-
redis_client = redis_dep.dependency(context)
62+
redis_client = redis_dep.dependency()
6563

6664
assert redis_client is not None
6765
assert hasattr(redis_client, 'get')

0 commit comments

Comments
 (0)