33import json
44import logging
55from functools import wraps
6- from typing import Callable , List , Optional , Tuple
6+ from typing import Callable , List , Optional , Tuple , cast
77from urllib .parse import parse_qs , urlencode , urlparse , urlunparse
88
99from pydantic import Field , field_validator
@@ -125,25 +125,21 @@ def validate_port_standalone(cls, v: int) -> int:
125125
126126# Configure only one Redis configuration
127127sentinel_settings = RedisSentinelSettings ()
128- standalone_settings = RedisSettings ()
128+ settings : RedisCommonSettings = cast (
129+ RedisCommonSettings ,
130+ sentinel_settings if sentinel_settings .REDIS_SENTINEL_HOSTS else RedisSettings (),
131+ )
129132
130133
131134def redis_retry (func : Callable ) -> Callable :
132135 """Retry with back-off decorator for Redis connections."""
133- _is_sentinel = True if sentinel_settings .REDIS_SENTINEL_HOSTS else False
134136
135137 @wraps (func )
136138 @retry (
137139 exceptions = (RedisConnectionError , RedisTimeoutError ),
138- tries = sentinel_settings .REDIS_QUERY_RETRIES_NUM
139- if _is_sentinel
140- else standalone_settings .REDIS_QUERY_RETRIES_NUM ,
141- delay = sentinel_settings .REDIS_QUERY_INITIAL_DELAY
142- if _is_sentinel
143- else standalone_settings .REDIS_QUERY_INITIAL_DELAY ,
144- backoff = sentinel_settings .REDIS_QUERY_BACKOFF
145- if _is_sentinel
146- else standalone_settings .REDIS_QUERY_BACKOFF ,
140+ tries = settings .REDIS_QUERY_RETRIES_NUM ,
141+ delay = settings .REDIS_QUERY_INITIAL_DELAY ,
142+ backoff = settings .REDIS_QUERY_BACKOFF ,
147143 logger = logger ,
148144 )
149145 async def wrapper (* args , ** kwargs ):
@@ -156,35 +152,35 @@ async def wrapper(*args, **kwargs):
156152async def _connect_redis_internal () -> Optional [aioredis .Redis ]:
157153 """Return a Redis connection Redis or Redis Sentinel."""
158154 if sentinel_settings .REDIS_SENTINEL_HOSTS :
159- sentinel_nodes = sentinel_settings .get_sentinel_nodes ()
155+ sentinel_nodes = settings .get_sentinel_nodes ()
160156 sentinel = Sentinel (
161157 sentinel_nodes ,
162- decode_responses = sentinel_settings .REDIS_DECODE_RESPONSES ,
158+ decode_responses = settings .REDIS_DECODE_RESPONSES ,
163159 )
164160
165161 redis = sentinel .master_for (
166- service_name = sentinel_settings .REDIS_SENTINEL_MASTER_NAME ,
167- db = sentinel_settings .REDIS_DB ,
168- decode_responses = sentinel_settings .REDIS_DECODE_RESPONSES ,
169- retry_on_timeout = sentinel_settings .REDIS_RETRY_TIMEOUT ,
170- client_name = sentinel_settings .REDIS_CLIENT_NAME ,
171- max_connections = sentinel_settings .REDIS_MAX_CONNECTIONS ,
172- health_check_interval = sentinel_settings .REDIS_HEALTH_CHECK_INTERVAL ,
162+ service_name = settings .REDIS_SENTINEL_MASTER_NAME ,
163+ db = settings .REDIS_DB ,
164+ decode_responses = settings .REDIS_DECODE_RESPONSES ,
165+ retry_on_timeout = settings .REDIS_RETRY_TIMEOUT ,
166+ client_name = settings .REDIS_CLIENT_NAME ,
167+ max_connections = settings .REDIS_MAX_CONNECTIONS ,
168+ health_check_interval = settings .REDIS_HEALTH_CHECK_INTERVAL ,
173169 )
174170 logger .info ("Connected to Redis Sentinel" )
175171
176- elif standalone_settings .REDIS_HOST :
172+ elif settings .REDIS_HOST :
177173 pool = aioredis .ConnectionPool (
178- host = standalone_settings .REDIS_HOST ,
179- port = standalone_settings .REDIS_PORT ,
180- db = standalone_settings .REDIS_DB ,
181- max_connections = standalone_settings .REDIS_MAX_CONNECTIONS ,
182- decode_responses = standalone_settings .REDIS_DECODE_RESPONSES ,
183- retry_on_timeout = standalone_settings .REDIS_RETRY_TIMEOUT ,
184- health_check_interval = standalone_settings .REDIS_HEALTH_CHECK_INTERVAL ,
174+ host = settings .REDIS_HOST ,
175+ port = settings .REDIS_PORT ,
176+ db = settings .REDIS_DB ,
177+ max_connections = settings .REDIS_MAX_CONNECTIONS ,
178+ decode_responses = settings .REDIS_DECODE_RESPONSES ,
179+ retry_on_timeout = settings .REDIS_RETRY_TIMEOUT ,
180+ health_check_interval = settings .REDIS_HEALTH_CHECK_INTERVAL ,
185181 )
186182 redis = aioredis .Redis (
187- connection_pool = pool , client_name = standalone_settings .REDIS_CLIENT_NAME
183+ connection_pool = pool , client_name = settings .REDIS_CLIENT_NAME
188184 )
189185 logger .info ("Connected to Redis" )
190186 else :
@@ -249,9 +245,9 @@ async def save_prev_link(
249245 """Save the current page as the previous link for the next URL."""
250246 if next_url and next_token :
251247 if sentinel_settings .REDIS_SENTINEL_HOSTS :
252- ttl_seconds = sentinel_settings .REDIS_SELF_LINK_TTL
253- elif standalone_settings .REDIS_HOST :
254- ttl_seconds = standalone_settings .REDIS_SELF_LINK_TTL
248+ ttl_seconds = settings .REDIS_SELF_LINK_TTL
249+ elif settings .REDIS_HOST :
250+ ttl_seconds = settings .REDIS_SELF_LINK_TTL
255251 key = get_redis_key (next_url , next_token )
256252 await redis .setex (key , ttl_seconds , current_url )
257253
0 commit comments