a
    bgB                     @   sP   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 de	ZdS )    )AnyIteratorListOptionalSequenceTuplecast)	ByteStore)
get_clientc                   @   s   e Zd ZdZddddddeee ee ee ee ddddZ	eeddd	Z
ee eee  d
ddZeeeef  ddddZee dd
ddZddee ee dd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	namespace)r   r   r   r   r   returnc          	   
   C   s   zddl m} W n. ty> } ztd|W Y d}~n
d}~0 0 |rT|sL|rTtd|sd|sdtd|rt||stdt|j d|}n |stdt|fi |pi }|| _	t|t
s|durt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)=)Zredisr   ImportError
ValueError
isinstance	TypeErrortype__name__r
   r   intr   r   )	selfr   r   r   r   r   r   eZ_client r   o/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/storage/redis.py__init__&   s<    
zRedisStore.__init__)keyr   c                 C   s    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   	delimiterr   r   r   _get_prefixed_keya   s    	zRedisStore._get_prefixed_key)keysr   c                    s(   t ttt   j fdd|D S )z.Get the values associated with the given keys.c                    s   g | ]}  |qS r   r"   .0r   r   r   r   
<listcomp>s       z#RedisStore.mget.<locals>.<listcomp>)r   r   r   bytesr   mget)r   r#   r   r'   r   r+   o   s    
zRedisStore.mget)key_value_pairsr   c                 C   s<   | j  }|D ] \}}|j| ||| jd q|  dS )zSet the given key-value pairs.)exN)r   Zpipelinesetr"   r   execute)r   r,   piper   valuer   r   r   msetv   s    
zRedisStore.msetc                    s"    fdd|D } j j|  dS )zDelete the given keys.c                    s   g | ]}  |qS r   r$   r%   r'   r   r   r(      r)   z&RedisStore.mdelete.<locals>.<listcomp>N)r   delete)r   r#   Z_keysr   r'   r   mdelete~   s    zRedisStore.mdelete)prefix)r5   r   c                c   st   |r|  |}n
|  d}ttt | jj|d}|D ]8}|d}| jrh|t| jd d }|V  q6|V  q6dS )zYield keys in the store.*)matchzutf-8   N)	r"   r   r   r*   r   	scan_iterdecoder   len)r   r5   patternr9   r   Zdecoded_keyZrelative_keyr   r   r   
yield_keys   s    

zRedisStore.yield_keys)r   
__module____qualname____doc__r   r   strdictr   r   r"   r   r   r*   r+   r   r2   r4   r   r=   r   r   r   r   r      s&    ;r   N)typingr   r   r   r   r   r   r   Zlangchain_core.storesr	   Z#langchain_community.utilities.redisr
   r   r   r   r   r   <module>   s   $