o
    2'h—  ã                   @   s4   d dl Zd dlmZ d dlmZ G dd„ deƒZdS )é    N)Ú	BaseCache)ÚRedisSerializerc                   @   sà  e Zd ZU dZdZejed< dZejed< e	ƒ Z
						d4d	ejd
edeje dededejejeejg ef f  dejfdd„Zdefdd„Zdeje defdd„Zdedejfdd„Zdedejej fdd„Zd5dedejdeje dejfdd „Zd5dedejdeje dejfd!d"„Z	d5d#ejeejf deje dejej fd$d%„Zdedefd&d'„Zdedejej fd(d)„Zdedefd*d+„Zdefd,d-„Zd6ded/edejfd0d1„Zd6ded/edejfd2d3„Z dS )7Ú
RedisCachea  Uses the Redis key-value store as a cache backend.

    The first argument can be either a string denoting address of the Redis
    server or an object resembling an instance of a redis.Redis class.

    Note: Python Redis API already takes care of encoding unicode strings on
    the fly.

    :param host: address of the Redis server or an object which API is
                 compatible with the official Python Redis client (redis-py).
    :param port: port number on which Redis server listens for connections.
    :param password: password authentication for the Redis server.
    :param db: db (zero-based numeric index) on Redis Server to connect.
    :param default_timeout: the default timeout that is used if no timeout is
                            specified on :meth:`~BaseCache.set`. A timeout of
                            0 indicates that the cache never expires.
    :param key_prefix: A prefix that should be added to all keys.

    Any additional keyword arguments will be passed to ``redis.Redis``.
    NÚ_read_clientÚ_write_clientÚ	localhostéë  r   é,  ÚhostÚportÚpasswordÚdbÚdefault_timeoutÚ
key_prefixÚkwargsc           
   
   K   s¨   t  | |¡ |d u rtdƒ‚t|tƒrGzdd l}W n ty* }	 ztdƒ|	‚d }	~	ww | dd ¡r5tdƒ‚|j	d||||dœ|¤Ž | _
| _n| | _| _
|pPd| _d S )	Nz)RedisCache host parameter may not be Noner   zno redis module foundZdecode_responsesz0decode_responses is not supported by RedisCache.)r
   r   r   r   Ú © )r   Ú__init__Ú
ValueErrorÚ
isinstanceÚstrÚredisÚImportErrorÚRuntimeErrorÚgetZRedisr   r   r   )
Úselfr
   r   r   r   r   r   r   r   Úerrr   r   úDC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/redis.pyr   !   s&   


€ÿÿÿzRedisCache.__init__Úreturnc                 C   s   t | jtƒr	| jS |  ¡ S ©N)r   r   r   ©r   r   r   r   Ú_get_prefix<   s   ÿÿzRedisCache._get_prefixÚtimeoutc                 C   s   t  | |¡}|dkrd}|S )z¨Normalize timeout by setting it to default of 300 if
        not defined (None) or -1 if explicitly set to zero.

        :param timeout: timeout to normalize.
        r   éÿÿÿÿ)r   Ú_normalize_timeout)r   r"   r   r   r   r$   A   s   zRedisCache._normalize_timeoutÚkeyc                 C   s    | j  | j |  ¡ › |› �¡¡S r   )Ú
serializerÚloadsr   r   r!   ©r   r%   r   r   r   r   L   s   ÿzRedisCache.getÚkeysc                    s<   ˆ j r‡ fdd„|D ƒ}nt|ƒ}‡ fdd„ˆ j |¡D ƒS )Nc                    ó   g | ]
}ˆ   ¡ › |› �‘qS r   ©r!   ©Ú.0r%   r    r   r   Ú
<listcomp>S   ó    z'RedisCache.get_many.<locals>.<listcomp>c                    s   g | ]}ˆ j  |¡‘qS r   )r&   r'   )r-   Úxr    r   r   r.   V   ó    )r   Úlistr   Zmget©r   r)   Zprefixed_keysr   r    r   Úget_manyQ   s   zRedisCache.get_manyÚvaluec                 C   s`   |   |¡}| j |¡}|dkr| jj|  ¡ › |› �|d�}|S | jj|  ¡ › |› �||d�}|S )Nr#   ©Únamer5   ©r7   r5   Útime)r$   r&   Údumpsr   Úsetr!   Úsetex)r   r%   r5   r"   ÚdumpÚresultr   r   r   r;   X   s   
ÿýÿzRedisCache.setc                 C   s^   |   |¡}| j |¡}| jj|  ¡ › |› �|d�}|r-|dkr-| jj|  ¡ › |› �|d� |S )Nr6   r#   )r7   r9   )r$   r&   r:   r   Zsetnxr!   Zexpire)r   r%   r5   r"   r=   Úcreatedr   r   r   Úadde   s   
ÿzRedisCache.addÚmappingc                 C   s–   |   |¡}| jjdd�}| ¡ D ]*\}}| j |¡}|dkr,|j|  ¡ › |› �|d� q|j|  ¡ › |› �||d� q| 	¡ }dd„ t
| ¡ |ƒD ƒS )NF)Ztransactionr#   r6   r8   c                 S   s   g | ]\}}|r|‘qS r   r   )r-   ÚkZwas_setr   r   r   r.      r1   z'RedisCache.set_many.<locals>.<listcomp>)r$   r   ZpipelineÚitemsr&   r:   r;   r!   r<   ÚexecuteÚzipr)   )r   rA   r"   Úpiper%   r5   r=   Úresultsr   r   r   Úset_manyp   s   
zRedisCache.set_manyc                 C   ó   t | j |  ¡ › |› �¡ƒS r   )Úboolr   Údeleter!   r(   r   r   r   rK   �   ó   zRedisCache.deletec                    sN   |sg S ˆ j r‡ fdd„|D ƒ}ndd„ |D ƒ}ˆ jj|Ž  ‡ fdd„|D ƒS )Nc                    r*   r   r+   r,   r    r   r   r.   ˆ   r/   z*RedisCache.delete_many.<locals>.<listcomp>c                 S   s   g | ]}|‘qS r   r   ©r-   rB   r   r   r   r.   Š   s    c                    s   g | ]	}ˆ   |¡s|‘qS r   )ÚhasrM   r    r   r   r.   Œ   s    )r   r   rK   r3   r   r    r   Údelete_many„   s   zRedisCache.delete_manyc                 C   rI   r   )rJ   r   Úexistsr!   r(   r   r   r   rN   Ž   rL   zRedisCache.hasc                 C   sH   d}| j r| j |  ¡ d ¡}|r| jj|Ž }t|ƒS | j ¡ }t|ƒS )Nr   Ú*)r   r   r)   r!   r   rK   ZflushdbrJ   )r   Ústatusr)   r   r   r   Úclear‘   s   
ÿzRedisCache.clearé   Údeltac                 C   s   | j j|  ¡ › |› �|d�S ©N)r7   Úamount©r   Úincrr!   ©r   r%   rU   r   r   r   Úinc›   rL   zRedisCache.incc                 C   s   | j j|  ¡ › |› �| d�S rV   rX   rZ   r   r   r   Údecž   s   zRedisCache.dec)r   r   Nr   r	   Nr   )rT   )!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú_tÚAnyÚ__annotations__r   r   r&   ÚintÚOptionalr   ÚUnionÚCallabler   r!   r$   r   ÚListr4   r;   r@   ÚDictrH   rJ   rK   rO   rN   rS   r[   r\   r   r   r   r   r      s\   
 ùþýüûúù
ø&&ÿÿÿ

þ

r   )Útypingra   Zcachelib.baser   Zcachelib.serializersr   r   r   r   r   r   Ú<module>   s    