o
    Wli%                     @   sZ   d Z ddlZddlmZ ddlmZ G dd deeZG dd deZG d	d
 d
eZdS )z
    flask_caching.backends.rediscache
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The redis caching backend.

    :copyright: (c) 2018 by Peter Justin.
    :copyright: (c) 2010 by Thadeus Burgess.
    :license: BSD, see LICENSE for more details.
    N)
RedisCache)	BaseCachec                   @   sJ   e Zd ZdZ						dddZed	d
 Zdd Zdd Zdd Z	dS )r   a  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``.
    	localhost  Nr   ,  c              	   K   s2   t j| |d tj| f||||||d| d S )N)default_timeout)hostportpassworddbr   
key_prefix)r   __init__CachelibRedisCache)selfr   r	   r
   r   r   r   kwargs r   q/var/www/html/tatsat2dev/dashboard-backend/venv/lib/python3.10/site-packages/flask_caching/backends/rediscache.pyr   (   s   

zRedisCache.__init__c              
   C   s   zddl m} W n ty } ztd|d }~ww |t|dd|dd|ddd	 |d
}|r;||d< |d}|rF||d< |d}	|	rY||	|dd d|d< | |i |}
|
S )Nr   )from_urlno redis module foundCACHE_REDIS_HOSTr   CACHE_REDIS_PORTr   CACHE_REDIS_DB)r   r	   r   CACHE_REDIS_PASSWORDr
   CACHE_KEY_PREFIXr   CACHE_REDIS_URLr   )r   r   )redisr   ImportErrorRuntimeErrorupdatedictgetpop)clsappconfigargsr   redis_from_urler
   r   	redis_url	new_classr   r   r   factory>   s0   






zRedisCache.factoryc                 C   s   t | jtr	| jS |  S )N)
isinstancer   strr   r   r   r   _get_prefix\   s   zRedisCache._get_prefixc                 C   s,   t |}|tkrt|dS dt| S )zDumps an object into a string for redis.  By default it serializes
        integers as regular string and pickle dumps everything else.
        ascii   !)typeintr,   encodepickledumps)r   valuetr   r   r   dump_objecta   s   zRedisCache.dump_objectc                    sV   |sdS  j r fdd|D }t jdd}|dur%t|r% jj| S  jj| S )z<when redis-py >= 3.0.0 and redis > 4, support this operationNc                    s   g | ]} j | qS r   )r   ).0keyr-   r   r   
<listcomp>o       z%RedisCache.unlink.<locals>.<listcomp>unlink)r   getattr_write_clientcallabler=   delete)r   keysr=   r   r-   r   r=   j   s   zRedisCache.unlink)r   r   Nr   r   N)
__name__
__module____qualname____doc__r   classmethodr*   r.   r8   r=   r   r   r   r   r      s    

	r   c                       s:   e Zd ZdZ						d
 fdd	Zedd	 Z  ZS )RedisSentinelCachea  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 sentinels: A list or a tuple of Redis sentinel addresses.
    :param master: The name of the master server in a sentinel configuration.
    :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.sentinel.Sentinel``.
    Nr   r    c              
      s   t  j||d zdd l}W n ty }	 ztd|	d }	~	ww |dd r*td|p.dg}dd | D }
d	d | D }|jj	d||||
d
|}|
|| _||| _d S )Nr   r   r   r   decode_responses0decode_responses is not supported by RedisCache.z	127.0.0.1ig  c                 S   s(   i | ]\}}| d r|dd |qS )	sentinel_	   N
startswithr9   r:   r6   r   r   r   
<dictcomp>   s    z/RedisSentinelCache.__init__.<locals>.<dictcomp>c                 S   s    i | ]\}}| d s||qS )rN   rP   rR   r   r   r   rS      s    )	sentinelsr
   r   sentinel_kwargsr   )superr   redis.sentinelr   r   r    
ValueErroritemssentinelSentinel
master_forr?   	slave_for_read_client)r   rT   masterr
   r   r   r   r   r   r'   rU   rZ   	__class__r   r   r      s4   


zRedisSentinelCache.__init__c                 C   sZ   | t|ddg|dd|dd |dd |dd |dd	d
 | |i |S )NCACHE_REDIS_SENTINELSrM   CACHE_REDIS_SENTINEL_MASTERmymasterr   CACHE_REDIS_SENTINEL_PASSWORDr   r   r   )rT   r_   r
   sentinel_passwordr   r   r   r   r    r"   r#   r$   r%   r   r   r   r   r*      s   




zRedisSentinelCache.factory)NNNr   r   rI   rC   rD   rE   rF   r   rG   r*   __classcell__r   r   r`   r   rH   w   s    +rH   c                       s0   e Zd ZdZ	d fdd	Zedd Z  ZS )	RedisClusterCachea  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 rediscluster.RedisCluster
    class.

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


    :param cluster: The redis cluster nodes address separated by comma.
                    e.g. host1:port1,host2:port2,host3:port3 .
    :param password: password authentication for the Redis server.
    :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
    ``rediscluster.RedisCluster``.
    rI   r   c              
      s   t  j||d |dd rtdzddlm} ddlm  W n ty1 } zt	d|d }~ww zdd	 |
d
D } fdd	|D }	W n tyX } ztd|d }~ww |dd}
|d|	||
d|}|| _|| _d S )NrJ   rK   rL   r   )RedisClusterClusterNodezno redis.cluster module foundc                 S   s   g | ]}| d qS ):)splitr9   noder   r   r   r;      r<   z.RedisClusterCache.__init__.<locals>.<listcomp>,c                    s&   g | ]} |d    |d   qS )r      )striprq   rm   r   r   r;      s    zQPlease give the correct cluster argument e.g. host1:port1,host2:port2,host3:port3skip_full_coverage_checkT)startup_nodesr
   rv   r   )rV   r   r    rX   r   rl   redis.clusterrn   r   r   rp   
IndexErrorr!   r?   r^   )r   clusterr
   r   r   r   rl   r'   nodesrw   rv   r`   rm   r   r      sB   



zRedisClusterCache.__init__c              
   C   sD   | t|dd|dd|dd|ddd | |i |S )NCACHE_REDIS_CLUSTERrI   r   CACHE_DEFAULT_TIMEOUTr   r   )rz   r
   r   r   rg   rh   r   r   r   r*     s   



zRedisClusterCache.factory)rI   rI   r   rI   ri   r   r   r`   r   rk      s    'rk   )	rF   r4   cachelibr   r   flask_caching.backends.baser   rH   rk   r   r   r   r   <module>   s    
eR