
    Ջg                     :    d dl Zd dlmZ d dlmZ  G d de      Zy)    N)	BaseCache)RedisSerializerc                      e Zd ZU dZdZej                  ed<   dZej                  ed<    e	       Z
	 	 	 	 	 	 d!d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ZdefdZdej                  e   defdZdedej                  fdZdedej(                  ej                     fdZd"dedej                  dej                  e   dej                  fdZd"dedej                  dej                  e   dej                  fdZ	 d"dej0                  eej                  f   dej                  e   dej(                  ej                     fdZdedefdZdedej(                  ej                     fdZdedefdZdefdZd#dededej                  fdZd#dededej                  fd Z y)$
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hostportpassworddbdefault_timeout
key_prefixkwargsc                 b   t        j                  | |       |t        d      t        |t              rF	 dd l}|j                  dd       rt        d       |j                  d||||d|x| _
        | _        n|x| _        | _
        |xs d| _        y # t        $ r}	t        d      |	d }	~	ww xY w)	Nz)RedisCache host parameter may not be Noner   zno redis module founddecode_responsesz0decode_responses is not supported by RedisCache.)r	   r
   r   r     )r   __init__
ValueError
isinstancestrredisImportErrorRuntimeErrorgetRedisr   r   r   )
selfr	   r
   r   r   r   r   r   r   errs
             A/var/www/html/venv/lib/python3.12/site-packages/cachelib/redis.pyr   zRedisCache.__init__!   s     	41<HIIdC E zz,d3 !STT5@U[[ 6xB6BH6 D!2 6:9D 2$*  E"#:;DEs   B 	B.B))B.returnc                 n    t        | j                  t              r| j                  S | j                         S N)r   r   r   )r   s    r   _get_prefixzRedisCache._get_prefix<   s*    )$//3?DOO	
EI__EV	
    timeoutc                 @    t        j                  | |      }|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%   s     r   r(   zRedisCache._normalize_timeoutA   s'     ..tW=a<Gr$   keyc                     | j                   j                  | j                  j                  | j	                          |             S r"   )
serializerloadsr   r   r#   r   r)   s     r   r   zRedisCache.getL   s@    $$!!T%5%5%7$8">?
 	
r$   keysc                    | j                   r"|D cg c]  }| j                          |  }}nt        |      }| j                  j	                  |      D cg c]  }| j
                  j                  |       c}S c c}w c c}w r"   )r   r#   listr   mgetr+   r,   )r   r.   r)   prefixed_keysxs        r   get_manyzRedisCache.get_manyQ   su    ??EIJc 0 0 23C59JMJ JM262C2C2H2H2WXQ%%a(XX K Ys   A<"Bvaluec                 *   | j                  |      }| j                  j                  |      }|dk(  r1| j                  j	                  | j                          | |      }|S | j                  j                  | j                          | ||      }|S )Nr'   namer5   r8   r5   time)r(   r+   dumpsr   setr#   setex)r   r)   r5   r%   dumpresults         r   r<   zRedisCache.setX   s    ))'2$$U+b=''++((*+C51 , F  ''--((*+C51G . F r$   c                 (   | j                  |      }| j                  j                  |      }| j                  j	                  | j                          | |      }|r4|dk7  r/| j                  j                  | j                          | |       |S )Nr7   r'   )r8   r:   )r(   r+   r;   r   setnxr#   expire)r   r)   r5   r%   r>   createds         r   addzRedisCache.adde   s    ))'2$$U+$$**$$&'u-T + 
 w"}%%d.>.>.@-A#+Gg%Vr$   mappingc                    | j                  |      }| j                  j                  d      }|j                         D ]q  \  }}| j                  j                  |      }|dk(  r&|j                  | j                          | |       L|j                  | j                          | ||       s |j                         }t        |j                         |      D 	cg c]
  \  }}	|	s	| c}	}S c c}	}w )NF)transactionr'   r7   r9   )r(   r   pipelineitemsr+   r;   r<   r#   r=   executezipr.   )
r   rE   r%   piper)   r5   r>   resultskwas_sets
             r   set_manyzRedisCache.set_manyp   s     ))'2 !!**u*=!--/ 	XJC??((/D"}!1!1!3 4SE:$G

4#3#3#5"6se <Dw
W	X ,,.$'$@LjaGLLLs   
C2*C2c                 n    t        | j                  j                  | j                          |             S r"   )boolr   deleter#   r-   s     r   rS   zRedisCache.delete   s1    D&&--1A1A1C0DSE.JKLLr$   c                    |sg S | j                   r"|D cg c]  }| j                          |  }}n|D cg c]  }| }} | j                  j                  |  |D cg c]  }| j	                  |      r| c}S c c}w c c}w c c}w r"   )r   r#   r   rS   has)r   r.   r)   r2   rN   s        r   delete_manyzRedisCache.delete_many   s    I??EIJc 0 0 23C59JMJ(,-1Q-M-!!!=1(<a<<	 K-<s   A=	BB6Bc                 n    t        | j                  j                  | j                          |             S r"   )rR   r   existsr#   r-   s     r   rU   zRedisCache.has   s1    D%%,,0@0@0B/CC5-IJKKr$   c                    d}| j                   rR| j                  j                  | j                         dz         }|r> | j                  j
                  | }t        |      S | j                  j                         }t        |      S )Nr   *)r   r   r.   r#   r   rS   flushdbrR   )r   statusr.   s      r   clearzRedisCache.clear   sy    ??$$))$*:*:*<s*BCD2++22D9 F| ''//1FF|r$   deltac                 `    | j                   j                  | j                          | |      S N)r8   amountr   incrr#   r   r)   r^   s      r   inczRedisCache.inc   s2    !!&&t/?/?/A.B3%,HQV&WWr$   c                 b    | j                   j                  | j                          | |       S r`   rb   rd   s      r   deczRedisCache.dec   s5    !!&&t/?/?/A.B3%,HRWQW&XXr$   )	localhosti  Nr   i,  Nr"   )   )!__name__
__module____qualname____doc__r   _tAny__annotations__r   r   r+   intOptionalr   UnionCallabler   r#   r(   r   Listr4   r<   rD   DictrP   rR   rS   rV   rU   r]   re   rg   r   r$   r   r   r      ss   *  L"&& M266  "J #%)"GK+ff+ + ++c"	+
 + + KKbkk"c'.B)B CD+ &&+6
S 

	"++c*: 	s 	
s 
rvv 

Yc Ybggbffo Ys 266 BKK4D PRPVPV 	s 	266 	BKK4D 	PRPVPV 	 JNMwwsBFF{+M68kk#6FM	M"M# M$ M= = =Ls Lt Lt Xs X3 Xrvv XYs Y3 Yrvv Yr$   r   )typingrn   cachelib.baser   cachelib.serializersr   r   r   r$   r   <module>rz      s     # 0XY XYr$   