o
    QDi                     @   s   d dl Z d dlZ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mZ d dlmZ e	ee
ejfZG d	d
 d
ZdedefddZG dd deZdS )    N)OrderedDict)settings)ConnectionError)ResponseError)TimeoutError)DEFAULT_TIMEOUTDefaultClient)ConnectionInterruptedc                   @   s   e Zd ZdZdS )Markerz<
    Dummy class for use as
    marker for herded keys.
    N)__name__
__module____qualname____doc__ r   r   Q/var/www/Datamplify/venv/lib/python3.10/site-packages/django_redis/client/herd.pyr
      s    r
   herd_timeoutreturnc                 C   s$   | |krdS | t d| }||kS )NT   )randomrandint)xr   valr   r   r   _is_expired   s   r   c                       s   e Zd Z fddZdd Zdd Zeddddf fd	d
	Zd fdd	ZdddZ	edddf fdd	Z
dd Zdd ZeddfddZ  ZS )
HerdClientc                    s,   t  | _ttdd| _t j|i | d S )NCACHE_HERD_TIMEOUT<   )r
   _markergetattrr   _herd_timeoutsuper__init__selfargskwargs	__class__r   r   r    (   s   zHerdClient.__init__c                 C   s$   |p| j jtt  }| j||fS N)_backenddefault_timeoutinttimer   )r"   valuetimeoutr   r   r   r   _pack-   s   zHerdClient._packc              	   C   st   z|\}}}W n t tfy   |df Y S w t|ts |dfS tt }||k r6|| }|t|| jfS |dfS )NF)
ValueError	TypeError
isinstancer
   r*   r+   r   r   )r"   r,   markerunpackedr   nowr   r   r   r   _unpack1   s   
zHerdClient._unpackNFc           
   	      sh   |t u r| jj}|d u s|dkrt j|||||||dS | ||}|| j }	t j|||	|||dS )Nr   )r-   versionclientnxxx)r-   r6   r7   r8   )r   r(   r)   r   setr.   r   )
r"   keyr,   r-   r6   r7   r8   r9   packedreal_timeoutr%   r   r   r:   A   s"   


zHerdClient.setc                    s.   t  j||||d}| |\}}|r|S |S )N)defaultr6   r7   )r   getr5   )r"   r;   r>   r6   r7   r<   r   refreshr%   r   r   r?   `   s
   zHerdClient.getc              
      s   |d u r
 j dd}|si S t } fdd|D }tt||}z|j| }W n ty; } zt|d|d }~ww t||D ]\}	}
|
d u rJqA  |
\}}|rXd n||||	 < qA|S )NFwritec                    s   g | ]	} j |d qS ))r6   )make_key).0r;   r"   r6   r   r   
<listcomp>r   s    z'HerdClient.get_many.<locals>.<listcomp>
connection)	
get_clientr   dictzipmget_main_exceptionsr	   r5   decode)r"   keysr6   r7   recovered_datanew_keysmap_keysresultser;   r,   r   r@   r   rE   r   get_manyi   s&   zHerdClient.get_manyTc              
      s   |du r
| j dd}|r| jnt j}z| }| D ]\}}	|||	|||d q|  W dS  tyB }
 zt|d|
d}
~
ww )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.
        NTrA   r6   r7   rG   )rI   r:   r   pipelineitemsexecuterM   r	   )r"   datar-   r6   r7   herdset_functionrW   r;   r,   rT   r%   r   r   set_many   s   
zHerdClient.set_manyc                 O      t r'   NotImplementedErrorr!   r   r   r   incr      zHerdClient.incrc                 O   r^   r'   r_   r!   r   r   r   decr   rb   zHerdClient.decrc                 C   sH   |d u r
| j dd}| j|||d}|d u rdS | j|||||d dS )NTrA   rV   F)r-   r6   r7   )rI   r?   r:   )r"   r;   r-   r6   r7   r,   r   r   r   touch   s   zHerdClient.touch)NNN)NN)r   r   r   r    r.   r5   r   r:   r?   rU   r]   ra   rc   rd   __classcell__r   r   r%   r   r   '   s"    
	r   )r   socketr+   collectionsr   django.confr   redis.exceptionsr   RedisConnectionErrorr   r   RedisTimeoutErrordjango_redis.client.defaultr   r   django_redis.exceptionsr	   r-   rM   r
   r*   boolr   r   r   r   r   r   <module>   s$    	