o
    Ph*                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ ddlm	Z	 ddl
mZ dd	lmZ d
dlmZmZ G dd deZdS )    N)OrderedDict)datetime)Union)ConnectionError   )ConnectionInterrupted)HashRing)CacheKey   )DEFAULT_TIMEOUTDefaultClientc                       s  e Zd ZedejZ fddZdd ZdEddZ	d	d
 Z
dd Zeddf fdd	ZdF fdd	ZdGddZedddf fdd	ZedfddZdHddZdH fdd	ZdH fdd	ZdH fdd 	ZdH fd!d"	ZdH fd#d$	ZdH fd%d&	ZdHd'eeef f fd(d)ZdHd'eeef f fd*d+Z			,			-dI fd.d/	ZdGd0d1ZdJd3d4Z dJ fd5d6	Z!dJ fd7d8	Z"dGd9d:Z#dGd;d<Z$	dKd=d>Z%d?d@ Z&eddf fdAdB	Z'dGdCdDZ(  Z)S )LShardClientz.*\{(.*)\}.*c                    sF   t  j|i | t| jttfs| jg| _t| j| _|  | _	d S N)
super__init__
isinstance_serverlisttupler   _ringconnect_serverdictselfargskwargs	__class__ g/var/www/html/Testing_prj/Navya-Bakers/venv/lib/python3.10/site-packages/django_redis/client/sharded.pyr      s
   
zShardClient.__init__c                 O   s   t r   NotImplementedErrorr   r   r   r   
get_client   s   zShardClient.get_clientr   c                 C   s$   i }| j D ]
}| j|||< q|S r   )r   connection_factoryr   )r   indexconnection_dictnamer   r   r   r      s   
zShardClient.connectc                 C   sH   t |}| j|}|d urt| dkr| d }| j|}|S )Nr   )str	_findhashmatchlengroupsr   get_node)r   _keykeygr&   r   r   r   get_server_name#   s   zShardClient.get_server_namec                 C   s   |  |}| j| S r   )r0   r   )r   r.   r&   r   r   r   
get_server+   s   

zShardClient.get_serverNc                    s6   |d u r| j ||d}| |}t j|||||dS )Nversion)r.   valuer3   clienttimeout)make_keyr1   r   add)r   r.   r4   r6   r3   r5   r   r   r   r8   /   s   

zShardClient.addc                    4   |d u r| j ||d}| |}t j||||dS )Nr2   )r.   defaultr3   r5   )r7   r1   r   get)r   r.   r:   r3   r5   r   r   r   r;   8      
zShardClient.getc           	         sn   |si S t  } fdd|D }tt||}|D ]} |} j||d}|d u r.q|||| < q|S )Nc                       g | ]	} j |d qS r2   r7   .0r.   r   r3   r   r   
<listcomp>E       z(ShardClient.get_many.<locals>.<listcomp>r.   r3   r5   )r   dictzipr1   r;   )	r   keysr3   recovered_datanew_keysmap_keysr.   r5   r4   r   rB   r   get_many?   s   
zShardClient.get_manyFc                    s8   |du r| j ||d}| |}t j||||||dS )zT
        Persist a value to the cache, and set an optional expiration time.
        Nr2   )r.   r4   r6   r3   r5   nx)r7   r1   r   set)r   r.   r4   r6   r3   r5   rM   r   r   r   rN   R   s   
zShardClient.setc                 C   s(   |  D ]\}}| j||||d qdS )a"  
        Set a bunch of values in the cache at once from a dict of key/value
        pairs. This is much more efficient than calling set() multiple times.

        If timeout is given, that timeout will be used for the key; otherwise
        the default cache timeout will be used.
        r2   N)itemsrN   )r   datar6   r3   r.   r4   r   r   r   set_many`   s   zShardClient.set_manyc              
   C   sd   |du r| j ||d}| |}| j ||d}z||dkW S  ty1 } zt|d|d}~ww )z%
        Test if key exists.
        Nr2   r
   
connection)r7   r1   existsr   r   )r   r.   r3   r5   er   r   r   has_keyk   s   
zShardClient.has_keyc                    2   |d u r| j ||d}| |}t j|||dS Nr2   rE   )r7   r1   r   deleter   r.   r3   r5   r   r   r   rY   z      
zShardClient.deletec                    2   |du r| j ||d}| |}t j|||dS )z
        Executes TTL redis command and return the "time-to-live" of specified key.
        If key is a non volatile key, it returns None.
        Nr2   rE   )r7   r1   r   ttlrZ   r   r   r   r]         
zShardClient.ttlc                    r\   )z
        Executes PTTL redis command and return the "time-to-live" of specified key
        in milliseconds. If key is a non volatile key, it returns None.
        Nr2   rE   )r7   r1   r   pttlrZ   r   r   r   r_      r^   zShardClient.pttlc                    rW   rX   )r7   r1   r   persistrZ   r   r   r   r`      r[   zShardClient.persistc                    r9   Nr2   )r.   r6   r3   r5   )r7   r1   r   expirer   r.   r6   r3   r5   r   r   r   rb      r<   zShardClient.expirec                    r9   ra   )r7   r1   r   pexpirerc   r   r   r   rd      r<   zShardClient.pexpirewhenc                    4   |du r| j ||d}| |}t j||||dS z
        Set an expire flag on a ``key`` to ``when`` on a shard client.
        ``when`` which can be represented as an integer indicating unix
        time or a Python datetime object.
        Nr2   )r.   re   r3   r5   )r7   r1   r   
pexpire_atr   r.   re   r3   r5   r   r   r   rh         
zShardClient.pexpire_atc                    rf   rg   )r7   r1   r   	expire_atri   r   r   r   rk      rj   zShardClient.expire_at皙?Tc                    sF   |d u r| j ||d}| |}| j ||d}t j||||||dS )Nr2   )r6   sleepr5   blocking_timeoutthread_local)r7   r1   r   lock)r   r.   r3   r6   rm   rn   r5   ro   r   r   r   rp      s   

zShardClient.lockc                    s>   d} fdd|D D ]}  |}| j||d7 }q|S )z/
        Remove multiple keys at once.
        r   c                    r=   r>   r?   rA   krB   r   r   rC      rD   z+ShardClient.delete_many.<locals>.<listcomp>r5   )r1   rY   )r   rH   r3   resr.   r5   r   rB   r   delete_many   s
   
zShardClient.delete_manyr
   c           
   
   C   s   |d u r| j ||d}| |}|d u r| jj}|  ||}| j|||d}z
| j|||d}W n tyB } zt|d|d }~ww |d u rMtd| t	|t
r^| j | || d}	n	| j ||| d}	| j|	||| |	d | j||d || S )Nr2   )r3   r5   rR   zKey '%s' not found)r6   r5   rs   )r7   r1   _backendr3   r;   r]   r   r   
ValueErrorr   r	   original_keyrN   rY   )
r   r.   deltar3   r5   old_keyr4   r]   rU   new_keyr   r   r   incr_version   s*   

zShardClient.incr_versionc                    r9   Nr2   )r.   ry   r3   r5   )r7   r1   r   incrr   r.   ry   r3   r5   r   r   r   r~     r<   zShardClient.incrc                    r9   r}   )r7   r1   r   decrr   r   r   r   r     r<   zShardClient.decrc                 C   s   t d)Nz)iter_keys not supported on sharded clientr    )r   r.   r3   r   r   r   	iter_keys  s   zShardClient.iter_keysc           	   
      s|    j ||d}g }z j D ]\}}||| qW n ty4 } z |}t|d|d }~ww  fdd|D S )Nr2   rR   c                    s   g | ]	}  | qS r   )reverse_keydecoderq   r   r   r   rC      rD   z$ShardClient.keys.<locals>.<listcomp>)make_patternr   rO   extendrH   r   r1   r   )	r   searchr3   patternrH   serverrS   rU   r5   r   r   r   rH     s   
zShardClient.keysc                 C   s   | j |||d}d|i}|r||d< g }| j D ]\}}	|dd |	jdi |D  qd}
|rC| j D ]\}}	|
|	j| 7 }
q7|
S )	z3
        Remove all keys matching pattern.
        )r3   prefixr)   countc                 s   s    | ]}|V  qd S r   r   r@   r   r   r   	<genexpr>/  s    z-ShardClient.delete_pattern.<locals>.<genexpr>r   Nr   )r   r   rO   r   	scan_iterrY   )r   r   r3   r5   itersizer   r   rH   r   rS   rt   r   r   r   delete_pattern"  s   "zShardClient.delete_patternc                 C   s    | j  D ]}| j|d qd S )Nrs   )r   values
disconnect)r   r5   r   r   r   do_close_clients7  s   zShardClient.do_close_clientsc                    r9   ra   )r7   r1   r   touchrc   r   r   r   r   ;  r<   zShardClient.touchc                 C   s   | j  D ]}|  qd S r   )r   r   flushdb)r   r5   rS   r   r   r   clearB  s   
zShardClient.clear)r   )NNNr   )NN)NNrl   NNT)r
   NN)NNNN)*__name__
__module____qualname__recompileIr(   r   r"   r   r0   r1   r   r8   r;   rL   rN   rQ   rV   rY   r]   r_   r`   rb   rd   r   r   intrh   rk   rp   ru   r|   r~   r   r   rH   r   r   r   r   __classcell__r   r   r   r   r      sP    	
	







r   )r   collectionsr   r   typingr   redis.exceptionsr   
exceptionsr   	hash_ringr   utilr	   r:   r   r   r   r   r   r   r   <module>   s    