o
    ]f$                     @   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                   @   sB   e Zd ZdZ						dddZe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   Z/home/ubuntu/webapp/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 |}|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intstrencodepickledumps)r   valuetr   r   r   dump_object]   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keyr   r   r   
<listcomp>k       z%RedisCache.unlink.<locals>.<listcomp>unlink)r   getattr_write_clientcallabler;   delete)r   keysr;   r   r8   r   r;   f   s   zRedisCache.unlink)r   r   Nr   r   N)	__name__
__module____qualname____doc__r   classmethodr*   r5   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r6   r7   r3   r   r   r   
<dictcomp>   s    z/RedisSentinelCache.__init__.<locals>.<dictcomp>c                 S   s    i | ]\}}| d s||qS )rL   rN   rP   r   r   r   rQ      s    )	sentinelsr
   r   sentinel_kwargsr   )superr   redis.sentinelr   r   r    
ValueErroritemssentinelSentinel
master_forr=   	slave_for_read_client)r   rR   masterr
   r   r   r   r   r   r'   rS   rX   	__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_SENTINELSrK   CACHE_REDIS_SENTINEL_MASTERmymasterr   CACHE_REDIS_SENTINEL_PASSWORDr   r   r   )rR   r]   r
   sentinel_passwordr   r   r   r   r    r"   r#   r$   r%   r   r   r   r   r*      s   




zRedisSentinelCache.factory)NNNr   r   rG   rA   rB   rC   rD   r   rE   r*   __classcell__r   r   r^   r   rF   s   s    +rF   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``.
    rG   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 )NrH   rI   rJ   r   )RedisClusterClusterNodezno redis.cluster module foundc                 S   s   g | ]}| d qS ):)splitr6   noder   r   r   r9      r:   z.RedisClusterCache.__init__.<locals>.<listcomp>,c                    s&   g | ]} |d    |d   qS )r      )stripro   rk   r   r   r9      s    zQPlease give the correct cluster argument e.g. host1:port1,host2:port2,host3:port3skip_full_coverage_checkT)startup_nodesr
   rt   r   )rT   r   r    rV   r   rj   redis.clusterrl   r   r   rn   
IndexErrorr!   r=   r\   )r   clusterr
   r   r   r   rj   r'   nodesru   rt   r^   rk   r   r      sB   



zRedisClusterCache.__init__c              
   C   sD   | t|dd|dd|dd|ddd | |i |S )NCACHE_REDIS_CLUSTERrG   r   CACHE_DEFAULT_TIMEOUTr   r   )rx   r
   r   r   re   rf   r   r   r   r*     s   



zRedisClusterCache.factory)rG   rG   r   rG   rg   r   r   r^   r   ri      s    'ri   )	rD   r1   cachelibr   r   flask_caching.backends.baser   rF   ri   r   r   r   r   <module>   s    `R