o
    ;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   A/var/www/html/venv/lib/python3.10/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 )zNormalize 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    