
    R}bg*                         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e          ZdS )    N)OrderedDict)datetime)Union)ConnectionError   )ConnectionInterrupted)HashRing)CacheKey   )DEFAULT_TIMEOUTDefaultClientc                       e Zd Z ej        dej                  Z fdZd Zd'dZ	d Z
d Zeddf fd		Zd( fd
	Zd)dZedddf fd	ZedfdZd*dZd* fd	Zd* fd	Zd* fd	Zd* fd	Zd* fd	Zd* fd	Zd*deeef         f fdZd*deeef         f fdZ	 	 	 	 	 	 d+ fd	Zd)dZd,dZ d, fd	Z!d, fd 	Z"d)d!Z#d)d"Z$	 d-d#Z%d$ Z&eddf fd%	Z'd)d&Z( xZ)S ).ShardClientz.*\{(.*)\}.*c                      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__s      n/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/django_redis/client/sharded.pyr   zShardClient.__init__   si    $)&)))$,u66 	* L>DLdl++
<<>>    c                     t           r   NotImplementedError)r   r   r   s      r   
get_clientzShardClient.get_client   s    !!r    r   c                 X    i }| j         D ]}| j                            |          ||<    |S r   )r   connection_factoryr   )r   indexconnection_dictnames       r   r   zShardClient.connect   s>    L 	J 	JD$($;$C$CD$I$IOD!!r    c                    t          |          }| j                            |          }|?t          |                                          dk    r|                                d         }| j                            |          }|S )Nr   )str	_findhashmatchlengroupsr   get_node)r   _keykeygr)   s        r   get_server_namezShardClient.get_server_name#   sj    $iiN  %%=S__q00((**Q-Cz""3''r    c                 F    |                      |          }| j        |         S r   )r4   r   )r   r2   r)   s      r   
get_serverzShardClient.get_server+   s#    ##C((%%r    Nc                     |,|                      ||          }|                     |          }t                                          |||||          S )Nversion)r2   valuer9   clienttimeout)make_keyr6   r   add)r   r2   r:   r<   r9   r;   r   s         r   r>   zShardClient.add/   sX    >--W-55C__S))Fww{{5'&'  
 
 	
r    c                     |,|                      ||          }|                     |          }t                                          ||||          S )Nr8   )r2   defaultr9   r;   )r=   r6   r   get)r   r2   r@   r9   r;   r   s        r   rA   zShardClient.get8   sL    >--W-55C__S))Fww{{sGWV{TTTr    c                     |si S t                      } fd|D             }t          t          ||                    }|D ]=}                     |          }                     ||          }|2||||         <   >|S )Nc                 >    g | ]}                     |           S r8   r=   ).0r2   r   r9   s     r   
<listcomp>z(ShardClient.get_many.<locals>.<listcomp>E   s)    HHHCDMM#wM77HHHr    r2   r9   r;   )r   dictzipr6   rA   )	r   keysr9   recovered_datanew_keysmap_keysr2   r;   r:   s	   ` `      r   get_manyzShardClient.get_many?   s     	I$HHHHH4HHHHd++,, 	2 	2C__S))FHHgfHEEE},1N8C=))r    Fc                     |,|                      ||          }|                     |          }t                                          ||||||          S )zT
        Persist a value to the cache, and set an optional expiration time.
        Nr8   )r2   r:   r<   r9   r;   nx)r=   r6   r   set)r   r2   r:   r<   r9   r;   rQ   r   s          r   rR   zShardClient.setR   s]     >--W-55C__S))Fww{{5'76VX  
 
 	
r    c                 l    |                                 D ]\  }}|                     ||||           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.
        r8   N)itemsrR   )r   datar<   r9   r2   r:   s         r   set_manyzShardClient.set_many`   sF     **,, 	; 	;JCHHS%'H::::	; 	;r    c                    |,|                      ||          }|                     |          }|                      ||          }	 |                    |          dk    S # t          $ r}t	          |          |d}~ww xY w)z%
        Test if key exists.
        Nr8   r   
connection)r=   r6   existsr   r   )r   r2   r9   r;   es        r   has_keyzShardClient.has_keyk   s    
 >--W-55C__S))FmmCm11	B==%%** 	B 	B 	B'6:::A	Bs   A   
B *A;;B c                     |,|                      ||          }|                     |          }t                                          |||          S Nr8   rH   )r=   r6   r   deleter   r2   r9   r;   r   s       r   r_   zShardClient.deletez   sJ    >--W-55C__S))Fww~~#wv~FFFr    c                     |,|                      ||          }|                     |          }t                                          |||          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.
        Nr8   rH   )r=   r6   r   ttlr`   s       r   rb   zShardClient.ttl   sL     >--W-55C__S))Fww{{sGF{CCCr    c                     |,|                      ||          }|                     |          }t                                          |||          S )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.
        Nr8   rH   )r=   r6   r   pttlr`   s       r   rd   zShardClient.pttl   sL     >--W-55C__S))Fww||WV|DDDr    c                     |,|                      ||          }|                     |          }t                                          |||          S r^   )r=   r6   r   persistr`   s       r   rf   zShardClient.persist   sJ    >--W-55C__S))Fww3GGGr    c                     |,|                      ||          }|                     |          }t                                          ||||          S Nr8   )r2   r<   r9   r;   )r=   r6   r   expirer   r2   r<   r9   r;   r   s        r   ri   zShardClient.expire   sM    >--W-55C__S))Fww~~#wPV~WWWr    c                     |,|                      ||          }|                     |          }t                                          ||||          S rh   )r=   r6   r   pexpirerj   s        r   rl   zShardClient.pexpire   sM    >--W-55C__S))Fww3QWXXXr    whenc                     |,|                      ||          }|                     |          }t                                          ||||          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.
        Nr8   )r2   rm   r9   r;   )r=   r6   r   
pexpire_atr   r2   rm   r9   r;   r   s        r   rp   zShardClient.pexpire_at   sQ     >--W-55C__S))Fww!!cgf!UUUr    c                     |,|                      ||          }|                     |          }t                                          ||||          S ro   )r=   r6   r   	expire_atrq   s        r   rs   zShardClient.expire_at   sQ     >--W-55C__S))Fww  StWV TTTr    皙?Tc                     |,|                      ||          }|                     |          }|                      ||          }t                                          ||||||          S )Nr8   )r<   sleepr;   blocking_timeoutthread_local)r=   r6   r   lock)	r   r2   r9   r<   rv   rw   r;   rx   r   s	           r   ry   zShardClient.lock   sv     >--W-55C__S))FmmCm11ww||-%  
 
 	
r    c                      d} fd|D             D ]1}                      |          }|                     ||          z  }2|S )z/
        Remove multiple keys at once.
        r   c                 >    g | ]}                     |           S rD   rE   )rF   kr   r9   s     r   rG   z+ShardClient.delete_many.<locals>.<listcomp>   s)    DDD!DMM!WM55DDDr    r;   )r6   r_   )r   rK   r9   resr2   r;   s   ` `   r   delete_manyzShardClient.delete_many   sc     DDDDDtDDD 	3 	3C__S))F4;;s6;222CC
r    r   c                    |,|                      ||          }|                     |          }|| j        j        }|                      ||          }|                     |||          }	 |                     |||          }n## t          $ r}t          |          |d }~ww xY w|t          d|z            t          |t                    r-|                      |                                ||z             }	n|                      |||z             }	|                     |	|||                     |	                     |                     ||           ||z   S )Nr8   )r9   r;   rX   zKey '%s' not found)r<   r;   r}   )r=   r6   _backendr9   rA   rb   r   r   
ValueErrorr   r
   original_keyrR   r_   )
r   r2   deltar9   r;   old_keyr:   rb   r[   new_keys
             r   incr_versionzShardClient.incr_version   so   >--W-55C__S))F?m+G--W--'&AA	B((7GF(CCCC 	B 	B 	B'6:::A	B =1C7888c8$$ 	BmmC$4$4$6$6%mPPGGmmC5mAAG%T__W5M5MNNNGF+++s   ,B 
B%B  B%c                     |,|                      ||          }|                     |          }t                                          ||||          S Nr8   )r2   r   r9   r;   )r=   r6   r   incrr   r2   r   r9   r;   r   s        r   r   zShardClient.incr  L    >--W-55C__S))Fww||5'&|QQQr    c                     |,|                      ||          }|                     |          }t                                          ||||          S r   )r=   r6   r   decrr   s        r   r   zShardClient.decr  r   r    c                      t          d          )Nz)iter_keys not supported on sharded clientr"   )r   r2   r9   s      r   	iter_keyszShardClient.iter_keys  s    !"MNNNr    c                 T                          ||          }g }	  j                                        D ]-\  }}|                    |                    |                     .n8# t
          $ r+}                     |          }t          |          |d }~ww xY w fd|D             S )Nr8   rX   c                 ^    g | ])}                     |                                          *S  )reverse_keydecode)rF   r|   r   s     r   rG   z$ShardClient.keys.<locals>.<listcomp>   s/    ;;;  ,,;;;r    )make_patternr   rT   extendrK   r   r6   r   )	r   searchr9   patternrK   serverrY   r[   r;   s	   `        r   rK   zShardClient.keys  s    ##FG#<<	B&*&6&<&<&>&> 6 6"
JOOG4455556 	B 	B 	B__W--F'6:::A	B
 <;;;d;;;;s   AA$ $
B.&BBc           	      B   |                      |||          }d|i}|r||d<   g }| j                                        D ]/\  }}	|                    d  |	j        di |D                        0d}
|r,| j                                        D ]\  }}	|
 |	j        | z  }
|
S )z3
        Remove all keys matching pattern.
        )r9   prefixr-   countc              3      K   | ]}|V  d S r   r   )rF   r2   s     r   	<genexpr>z-ShardClient.delete_pattern.<locals>.<genexpr>/  s"      FFFFFFFFr    r   r   )r   r   rT   r   	scan_iterr_   )r   r   r9   r;   itersizer   r   rK   r   rY   r~   s              r   delete_patternzShardClient.delete_pattern"  s     ##GWV#LL7# 	'&F7O"&"2"8"8":": 	G 	GFJKKFF';z';'E'Ef'E'EFFFFFFF 	0&*&6&<&<&>&> 0 0"
(z($//
r    c                 j    | j                                         D ]}|                     |           d S )Nr}   )r   values
disconnect)r   r;   s     r   do_close_clientszShardClient.do_close_clients7  sA    &--// 	+ 	+FOO6O****	+ 	+r    c                     |,|                      ||          }|                     |          }t                                          ||||          S rh   )r=   r6   r   touchrj   s        r   r   zShardClient.touch;  sL    >--W-55C__S))Fww}}gwv}VVVr    c                 f    | j                                         D ]}|                                 d S r   )r   r   flushdb)r   r;   rY   s      r   clearzShardClient.clearB  s?    *1133 	! 	!J    	! 	!r    )r   )NNNr   )NN)NNrt   NNT)r   NN)NNNN)*__name__
__module____qualname__recompileIr,   r   r$   r   r4   r6   r   r>   rA   rO   rR   rV   r\   r_   rb   rd   rf   ri   rl   r   r   intrp   rs   ry   r   r   r   r   r   rK   r   r   r   r   __classcell__)r   s   @r   r   r      s       
?BD11I* * * * *" " "     & & & '6tD 
 
 
 
 
 
U U U U U U   ( #24QV
 
 
 
 
 
 &5d 	; 	; 	; 	;B B B BG G G G G G
D 
D 
D 
D 
D 
D
E 
E 
E 
E 
E 
EH H H H H HX X X X X XY Y Y Y Y Y
V 
VE(C-$8 
V 
V 
V 
V 
V 
V
U 
U53#7 
U 
U 
U 
U 
U 
U 
 
 
 
 
 
0      8R R R R R RR R R R R RO O O O< < < < IM   *+ + + "1$t W W W W W W! ! ! ! ! ! ! !r    r   )r   collectionsr   r   typingr   redis.exceptionsr   
exceptionsr   	hash_ringr	   utilr
   r@   r   r   r   r   r    r   <module>r      s    				 # # # # # #             , , , , , , . . . . . .                   3 3 3 3 3 3 3 3v! v! v! v! v!- v! v! v! v! v!r    