
    Ջg$                     l    d Z ddlZddlmZ ddlmZ  G d dee      Z G d de      Z G d	 d
e      Zy)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                   @    e Zd ZdZ	 	 	 	 	 	 ddZed        Zd Zd Zy)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``.
    Nc           
      l    t        j                  | |       t        j                  | f||||||d| y )N)default_timeout)hostportpassworddbr   
key_prefix)r   __init__CachelibRedisCache)selfr   r	   r
   r   r   r   kwargss           T/var/www/html/venv/lib/python3.12/site-packages/flask_caching/backends/rediscache.pyr   zRedisCache.__init__)   sF     	4A##		
+!		
 		
    c           
         	 ddl m} |j	                  t        |j                  dd      |j                  dd      |j                  dd      	             |j                  d
      }|r||d<   |j                  d      }|r||d<   |j                  d      }	|	r ||	|j                  dd             |d<    | |i |}
|
S # t        $ r}t        d      |d }~ww xY w)Nr   )from_urlno redis module foundCACHE_REDIS_HOST	localhostCACHE_REDIS_PORT  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_classs              r   factoryzRedisCache.factory?   s    	?8 	ZZ 2K@ZZ 2D9::.2	
 ::45!)F:ZZ 23
#-F< JJ01	+I&**T4:PQF6N((	1  	?67Q>	?s   B? ?	CCCc                     t        |      }|t        k(  rt        |      j                  d      S dt	        j
                  |      z   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ts      r   dump_objectzRedisCache.dump_object]   s=     K8u:$$W--fll5)))r   c                    |sy| j                   r|D cg c]  }| j                   |z    }}t        | j                  dd      }|$t        |      r | j                  j                  | S  | j                  j
                  | S c c}w )z<when redis-py >= 3.0.0 and redis > 4, support this operationNunlink)r   getattr_write_clientcallabler;   delete)r   keyskeyr;   s       r   r;   zRedisCache.unlinkf   s    ??59:cDOOc):D:++Xt<(6"2,4%%,,d33(t!!(($// ;s   B)r   r   Nr   ,  N)	__name__
__module____qualname____doc__r   classmethodr-   r9   r;    r   r   r   r      s>    . 
,  :*
0r   r   c                   @     e Zd ZdZ	 	 	 	 	 	 d fd	Zed        Z x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``.
    c                 <   t         |   ||       	 dd l}|j                  dd       rt        d      |xs dg}|j                         D 
ci c]  \  }
}|
j                  d      r|
dd  | }}
}|j                         D 
ci c]  \  }
}|
j                  d      s|
| }}
} |j                  j                  d
||||d	|}|j                  |      | _        |j                  |      | _        y # t        $ r}	t	        d      |	d }	~	ww xY wc c}}
w c c}}
w )Nr   r   r   r   decode_responses0decode_responses is not supported by RedisCache.z	127.0.0.1ig  	sentinel_	   )	sentinelsr
   r   sentinel_kwargsrH   )superr   redis.sentinelr   r    r#   
ValueErroritems
startswithsentinelSentinel
master_forr=   	slave_for_read_client)r   rR   masterr
   r   r   r   r   r   r*   rA   r7   rS   rY   	__class__s                 r   r   zRedisSentinelCache.__init__   sF    	JP	?! ::($/OPP7"6!7	 %lln
U~~k* GUN
 
 %lln
U>>+. J
 
 +5>>** 
+	

 
 &008$..v67  	?67Q>	?


s#   C5 !DD5	D>D

Dc                    |j                  t        |j                  ddg      |j                  dd      |j                  dd       |j                  dd       |j                  dd       |j                  dd	      
              | |i |S )NCACHE_REDIS_SENTINELSrO   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   s        r   r-   zRedisSentinelCache.factory   s     **%<?S>TUzz"?L$:DA"(**-Ld"S!::&8$?::.2		
 D#F##r   )NNNr   rB    rC   rD   rE   rF   r   rG   r-   __classcell__r_   s   @r   rJ   rJ   s   s6    0 )7V $ $r   rJ   c                   6     e Zd ZdZ	 d fd	Zed        Z x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``.
    c                 (   t         |   ||       |j                  dd       rt        d      	 ddlm} ddlm} 	 |j                  d      D 	cg c]  }	|	j                  d	       }
}	|
D 	cg c]-  }	 ||	d   j                         |	d
   j                               / }}	|j                  dd      } |d|||d|}|| _        || _        y # t        $ r}t        d      |d }~ww xY wc c}	w c c}	w # t        $ r}t        d      |d }~ww xY w)NrL   rM   rN   r   )RedisCluster)ClusterNodezno redis.cluster module found,:   zQPlease give the correct cluster argument e.g. host1:port1,host2:port2,host3:port3skip_full_coverage_checkT)startup_nodesr
   rt   rH   )rT   r   r#   rV   r   ro   redis.clusterrp   r   r    splitstrip
IndexErrorr$   r=   r]   )r   clusterr
   r   r   r   ro   rp   r*   nodenodesru   rt   r_   s                r   r   zRedisClusterCache.__init__   s9    	JP::($/OPP	G*1		3:==3EF4djjoFEFJOBFDGMMOT!W]]_=M  $*::.H$#O  
'%=
 	
 %#5  	G>?QF	G G  	; 	sL   C C7 C-)C7 /2C2!C7 	C*C%%C*-
C7 7	D DDc                     |j                  t        |j                  dd      |j                  dd      |j                  dd      |j                  dd                    | |i |S )NCACHE_REDIS_CLUSTERrh   r   CACHE_DEFAULT_TIMEOUTrB   r   )rz   r
   r   r   rf   rg   s        r   r-   zRedisClusterCache.factory  sd    

#8"=$:B? &

+BC H!::&8"=		
 D#F##r   )rh   rh   rB   rh   ri   rk   s   @r   rm   rm      s)    . HJ%$N 	$ 	$r   rm   )	rF   r5   cachelibr   r   flask_caching.backends.baser   rJ   rm   rH   r   r   <module>r      sD   	  5 1]0. ]0@O$ O$dH$
 H$r   