Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 9 additions & 8 deletions nexios_contrib/redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

This module provides Redis client initialization, dependency injection,
"""

from __future__ import annotations

import json
Expand All @@ -16,7 +17,6 @@
from .config import RedisConfig



if TYPE_CHECKING:
from nexios.dependencies import Context

Expand All @@ -31,6 +31,7 @@

class RedisConnectionError(Exception):
"""Raised when there's an error connecting to Redis."""

pass


Expand Down Expand Up @@ -79,11 +80,7 @@ def init_redis(
global _redis_client

config = RedisConfig(
url=url,
db=db,
password=password,
decode_responses=decode_responses,
**kwargs
url=url, db=db, password=password, decode_responses=decode_responses, **kwargs
)

_redis_client = RedisClient(config)
Expand Down Expand Up @@ -113,8 +110,10 @@ async def _close_redis() -> None:
app.on_startup(_init_redis)
app.on_shutdown(_close_redis)

return _redis_client


def get_redis(context: Optional["Context"] = None) -> RedisClient:
def get_redis() -> RedisClient:
"""
Get the Redis client instance from the current context.

Expand Down Expand Up @@ -150,7 +149,9 @@ async def get_cached_data(
"""
global _redis_client
if _redis_client is None:
raise RedisConnectionError("Redis client not initialized. Call init_redis() first.")
raise RedisConnectionError(
"Redis client not initialized. Call init_redis() first."
)
return _redis_client


Expand Down
7 changes: 1 addition & 6 deletions nexios_contrib/redis/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

from __future__ import annotations

from typing import Any, Optional, Union
from nexios.dependencies import Depend, Context
from nexios.http import Request

from .client import RedisClient
from . import get_redis
Expand Down Expand Up @@ -44,7 +42,4 @@ async def get_cached_data(
```
"""

def _wrap(context: Context = Context()) -> RedisClient:
return get_redis(context)

return Depend(_wrap)
return Depend(get_redis)
4 changes: 1 addition & 3 deletions tests/test_redis/test_redis_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ def test_redis_depend_with_context(self, app_with_mock_redis, mock_redis):
redis_dep = RedisDepend()

# Create a mock context
context = Context()

# Call the dependency function
redis_client = redis_dep.dependency(context)
redis_client = redis_dep.dependency()

assert redis_client is not None
assert hasattr(redis_client, 'get')
Expand Down
Loading