
    dhB                     V    S SK JrJrJrJrJrJrJr  S SKJ	r	  S SK
Jr   " S S\	5      rg)    )AnyIteratorListOptionalSequenceTuplecast)	ByteStore)
get_clientc                       \ rS rSrSrSSSSSS.S\S\\   S\\   S\\	   S	\\   S
S4S jjr
S\S
\4S jrS\\   S
\\\      4S jrS\\\\4      S
S4S jrS\\   S
S4S jrSS.S\\   S
\\   4S jjrSrg)
RedisStore   aK  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                    SSK Jn  U(       a  U(       d  U(       a  [        S5      eU(       d  U(       d  [        S5      eU(       a5  [	        X5      (       d"  [        S[        U5      R                   S35      eUnO'U(       d  [        S5      e[        U40 U=(       d    0 D6nXl	        [	        U[        5      (       d  Ub  [        S	[        U5      < S35      eX@l        XPl        g! [         a  n[        S5      UeSnAff = f)
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	            Y/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/storage/redis.py__init__RedisStore.__init__&   s    *	# yM2 
 iUVVf,,1$v,2G2G1H	R  G L  !D}/BDG#s##CcINOO"E  	$ 	s   C( (
D2C>>Dkeyc                 P    SnU R                   (       a  U R                    U U 3$ U$ )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_keyRedisStore._get_prefixed_keya   s-     	>>nn%i[66
    keysc                     [        [        [        [              U R                  R                  U Vs/ sH  o R                  U5      PM     sn5      5      $ s  snf )z.Get the values associated with the given keys.)r	   r   r   bytesr   mgetr)   )r   r,   r%   s      r"   r/   RedisStore.mgeto   sH    %!KKTJTc44S9TJK
 	
Js   Akey_value_pairsc                     U R                   R                  5       nU H.  u  p4UR                  U R                  U5      X@R                  S9  M0     UR                  5         g)zSet the given key-value pairs.)exN)r   pipelinesetr)   r   execute)r   r1   piper%   values        r"   msetRedisStore.msetv   sH    {{##%)JCHHT++C0%HHHE *r+   c                 ~    U Vs/ sH  o R                  U5      PM     nnU R                  R                  " U6   gs  snf )zDelete the given keys.N)r)   r   delete)r   r,   r%   _keyss       r"   mdeleteRedisStore.mdelete~   s6    8<='',=E" >s   :)prefixr@   c             #   \  #    U(       a  U R                  U5      nOU R                  S5      n[        [        [           U R                  R                  US95      nU HJ  nUR                  S5      nU R                  (       a!  U[        U R                  5      S-   S nUv   MF  Uv   ML     g7f)zYield keys in the store.*)matchzutf-8   N)	r)   r	   r   r.   r   	scan_iterdecoder   len)r   r@   patternrE   r%   decoded_keyrelative_keys          r"   
yield_keysRedisStore.yield_keys   s     ,,V4G,,S1G%$++*?*?g*?*NO	C**W-K~~*3t~~+>+B+DE""!! s   B*B,)r   r   r   )r   
__module____qualname____firstlineno____doc__r   r   strdictr   r#   r)   r   r   r.   r/   r   r9   r>   r   rK   __static_attributes__ 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   rT   r+   r"   <module>rX      s"    G G G + :H" H"r+   