
    k
i*                         d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ  ej        e          Z e
de	          Z G d
 deee                   ZdS )z(Base synchronous Redis middleware class.    N)ABCabstractmethod)AnyGenericOptionalTypeVar)Redis   )MiddlewareConfigRedisClientType)boundc                       e Zd ZU dZeed<   eed<   eed<   eed<   eed<   ej	        ed<   ded	d
fdZ
edd            ZddZddZddZdee         dee         dee         d	d
fdZd
S )BaseRedisMiddlewarea0  Abstract base class for synchronous Redis middleware.

    This class provides common functionality for all Redis-based middleware:
    - Redis client lifecycle management
    - Lazy initialization with thread-safe setup
    - Graceful degradation on Redis errors
    - Context manager support

    Subclasses must implement:
    - _setup_sync(): Called once during initialization to set up resources

    Example:
        ```python
        class MyMiddleware(BaseRedisMiddleware):
            def _setup_sync(self) -> None:
                # Initialize resources
                self._cache = SemanticCache(redis_client=self._redis)

        config = MiddlewareConfig(redis_url="redis://localhost:6379")
        with MyMiddleware(config) as middleware:
            # Use middleware
            pass
        ```
    _redis_config_owns_client_graceful_degradation_initialized
_init_lockconfigreturnNc                 &   || _         |j        | _        d| _        t	          j                    | _        |j        |j        | _        d| _	        dS |j
        .|j        pi }t          j        |j
        fi || _        d| _	        dS t          d          )zInitialize the middleware.

        Args:
            config: Middleware configuration with Redis connection details.

        Raises:
            ValueError: If neither redis_url nor redis_client is provided.
        FNTz1Either redis_url or redis_client must be provided)r   graceful_degradationr   r   	threadingLockr   redis_clientr   r   	redis_urlconnection_argsr	   from_url
ValueError)selfr   r   s      C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langgraph/middleware/redis/base.py__init__zBaseRedisMiddleware.__init__2   s     %+%@"!#.** * -DK %D)$4:O.)9MM_MMDK $DPQQQ    c                     dS )zSet up middleware resources.

        Called once during lazy initialization. Subclasses should override
        this to initialize caches, indices, or other resources.
        N r!   s    r"   _setup_synczBaseRedisMiddleware._setup_syncK   s	     	r$   c                     | j         rdS | j        5  | j         s|                                  d| _         ddd           dS # 1 swxY w Y   dS )zxEnsure middleware is initialized (thread-safe).

        Uses double-checked locking pattern for thread safety.
        NT)r   r   r(   r'   s    r"   _ensure_initialized_syncz,BaseRedisMiddleware._ensure_initialized_syncT   s    
  	F_ 	) 	)$ )  """$(!	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   #AAAc                     | j         rat          | d          rS	 | j                                         dS # t          $ r(}t
                              d|            Y d}~dS d}~ww xY wdS dS )z7Close the Redis connection if owned by this middleware.r   zError closing Redis client: N)r   hasattrr   close	Exceptionloggerwarning)r!   es     r"   r-   zBaseRedisMiddleware.closea   s     	Cx!8!8 	CC!!##### C C CAaAABBBBBBBBBC	C 	C 	C 	Cs   4 
A&A!!A&$BaseRedisMiddleware[RedisClientType]c                     | S )zEnter context manager.r&   r'   s    r"   	__enter__zBaseRedisMiddleware.__enter__i   s    r$   exc_typeexc_valexc_tbc                 .    |                                   dS )z)Exit context manager and close resources.N)r-   )r!   r5   r6   r7   s       r"   __exit__zBaseRedisMiddleware.__exit__m   s     	

r$   )r   N)r   r2   )__name__
__module____qualname____doc__r   __annotations__r   boolr   r   r#   r   r(   r*   r-   r4   r   typeBaseExceptionr   r9   r&   r$   r"   r   r      s<         2 R/ RD R R R R2    ^) ) ) )C C C C   4. -( 	
 
     r$   r   )r=   loggingr   abcr   r   typingr   r   r   r   redisr	   typesr   	getLoggerr:   r/   r   r   r&   r$   r"   <module>rH      s    . .      # # # # # # # # 2 2 2 2 2 2 2 2 2 2 2 2       # # # # # #		8	$	$'+5999c c c c c#w7 c c c c cr$   