
    NgB                     ^    d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ  G d de	          ZdS )    )AnyIteratorListOptionalSequenceTuplecast)	ByteStore)
get_clientc                   *   e Zd ZdZdddddddedee         dee         dee         dee         d	dfd
Z	ded	efdZ
dee         d	eee                  fdZdeeeef                  d	dfdZdee         d	dfdZdddee         d	ee         fdZdS )
RedisStorea  BaseStore implementation using Redis as the underlying store.

    Examples:
        Create a RedisStore instance and perform operations on it:

        .. code-block:: python

            # Instantiate the RedisStore with a Redis connection
            from langchain_community.storage import RedisStore
            from langchain_community.utilities.redis import get_client

            client = get_client('redis://localhost:6379')
            redis_store = RedisStore(client=client)

            # Set values for keys
            redis_store.mset([("key1", b"value1"), ("key2", b"value2")])

            # Get values for keys
            values = redis_store.mget(["key1", "key2"])
            # [b"value1", b"value2"]

            # Delete keys
            redis_store.mdelete(["key1"])

            # Iterate over keys
            for key in redis_store.yield_keys():
                print(key)  # noqa: T201
    N)client	redis_urlclient_kwargsttl	namespacer   r   r   r   r   returnc                   	 ddl m} n"# t          $ r}t          d          |d}~ww xY w|r|s|rt          d          |s|st          d          |r8t	          ||          s%t          dt          |          j         d          |}n |st          d          t          |fi |pi }|| _	        t	          |t                    s"| t          d	t          |          d          || _        || _        dS )
a  Initialize the RedisStore with a Redis connection.

        Must provide either a Redis client or a redis_url with optional client_kwargs.

        Args:
            client: A Redis connection instance
            redis_url: redis url
            client_kwargs: Keyword arguments to pass to the Redis client
            ttl: time to expire keys in seconds if provided,
                 if None keys will never expire
            namespace: if provided, all keys will be prefixed with this namespace
        r   )RediszLThe RedisStore requires the redis library to be installed. pip install redisNz`Either a Redis client or a redis_url with optional client_kwargs must be provided, but not both.z6Either a Redis client or a redis_url must be provided.zExpected Redis client, got z	 instead.z$Expected int or None, got type(ttl)=)redisr   ImportError
ValueError
isinstance	TypeErrortype__name__r   r   intr   r   )	selfr   r   r   r   r   r   e_clients	            ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/storage/redis.py__init__zRedisStore.__init__&   s~   *	####### 	 	 	$  	  	y 	M 	2  
  	Wi 	WUVVV 	Efe,, R$v,,2GRRR   GG  L   !DD}/BDDG#s## 	PNcNNNOOO"s   	 
(#(keyc                 2    d}| j         r| j          | | S |S )zGet the key with the namespace prefix.

        Args:
            key (str): The original key.

        Returns:
            str: The key with the namespace prefix.
        /)r   )r   r#   	delimiters      r!   _get_prefixed_keyzRedisStore._get_prefixed_keya   s0     	> 	7n6i6666
    keysc                      t          t          t          t                             j                             fd|D                                 S )z.Get the values associated with the given keys.c                 :    g | ]}                     |          S  r'   .0r#   r   s     r!   
<listcomp>z#RedisStore.mget.<locals>.<listcomp>s   s'    JJJcd44S99JJJr(   )r	   r   r   bytesr   mget)r   r)   s   ` r!   r2   zRedisStore.mgeto   sI    %!KJJJJTJJJKK
 
 	
r(   key_value_pairsc                     | j                                         }|D ]5\  }}|                    |                     |          || j                   6|                                 dS )zSet the given key-value pairs.)exN)r   pipelinesetr'   r   execute)r   r3   piper#   values        r!   msetzRedisStore.msetv   sf    {##%%) 	F 	FJCHHT++C00%DHHEEEEr(   c                 B      fd|D             }  j         j        |  dS )zDelete the given keys.c                 :    g | ]}                     |          S r,   r-   r.   s     r!   r0   z&RedisStore.mdelete.<locals>.<listcomp>   s'    ==='',,===r(   N)r   delete)r   r)   _keyss   `  r!   mdeletezRedisStore.mdelete~   s4    =======E""""r(   )prefixrA   c             #   h  K   |r|                      |          }n|                      d          }t          t          t                   | j                            |                    }|D ]F}|                    d          }| j        r$|t          | j                  dz   d         }|V  B|V  GdS )zYield keys in the store.*)matchzutf-8   N)	r'   r	   r   r1   r   	scan_iterdecoder   len)r   rA   patternrF   r#   decoded_keyrelative_keys          r!   
yield_keyszRedisStore.yield_keys   s       	2,,V44GG,,S11G%$+*?*?g*?*N*NOO	 	" 	"C**W--K~ "*3t~+>+>+B+D+DE"""""!!!!!	" 	"r(   )r   
__module____qualname____doc__r   r   strdictr   r"   r'   r   r   r1   r2   r   r;   r@   r   rL   r,   r(   r!   r   r      s{        @ #'(,!#'9# 9# 9# 9# C=	9#
  ~9# c]9# C=9# 
9# 9# 9# 9#vS S    
# 
4+@ 
 
 
 
HU3:->$? D    #HSM #d # # # #
 59 " " "HSM "Xc] " " " " " "r(   r   N)typingr   r   r   r   r   r   r	   langchain_core.storesr
   #langchain_community.utilities.redisr   r   r,   r(   r!   <module>rU      s    G G G G G G G G G G G G G G G G G G + + + + + + : : : : : :H" H" H" H" H" H" H" H" H" H"r(   