
    7|hB                     V    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y)    )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y)
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} |r|s|rt        d      |s|st        d      |r1t	        ||      s"t        dt        |      j                   d      |}n|st        d      t        |fi |xs i }|| _	        t	        |t              s|t        d	t        |      d      || _        || _        y# t        $ r}t        d      |d}~ww xY w)
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/test/engine/venv/lib/python3.12/site-packages/langchain_community/storage/redis.py__init__zRedisStore.__init__&   s    *	# yM2 
 iUVVfe,1$v,2G2G1H	R  G L  !D}/BDG#s#CcINOO"E  	$ 	s   B8 8	CCCkeyc                 F    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   s-     	>>nn%i[66
    keysc                     t        t        t        t              | j                  j                  |D cg c]  }| j                  |       c}            S c c}w )z.Get the values associated with the given keys.)r	   r   r   bytesr   mgetr'   )r   r)   r#   s      r!   r,   zRedisStore.mgeto   sF    %!KKTJcd44S9JK
 	
Js   Akey_value_pairsc                     | j                   j                         }|D ]2  \  }}|j                  | j                  |      || j                         4 |j                          y)zSet the given key-value pairs.)exN)r   pipelinesetr'   r   execute)r   r-   piper#   values        r!   msetzRedisStore.msetv   sS    {{##%) 	FJCHHT++C0%DHHHE	Fr(   c                 |    |D cg c]  }| j                  |       }} | j                  j                  |  yc c}w )zDelete the given keys.N)r'   r   delete)r   r)   r#   _keyss       r!   mdeletezRedisStore.mdelete~   s9    8<='',==E" >s   9)prefixr:   c             #   H  K   |r| j                  |      }n| j                  d      }t        t        t           | j                  j                  |            }|D ]C  }|j                  d      }| j                  r |t        | j                        dz   d }| @| E yw)zYield keys in the store.*)matchzutf-8   N)	r'   r	   r   r+   r   	scan_iterdecoder   len)r   r:   patternr?   r#   decoded_keyrelative_keys          r!   
yield_keyszRedisStore.yield_keys   s     ,,V4G,,S1G%$++*?*?g*?*NO	 	"C**W-K~~*3t~~+>+B+DE""!!	"s   B B")r   
__module____qualname____doc__r   r   strdictr   r"   r'   r   r   r+   r,   r   r5   r9   r   rE    r(   r!   r   r      s    @ #'(,!#'9# 9# C=	9#
  ~9# c]9# C=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   rK   r(   r!   <module>rO      s"    G G G + :H" H"r(   