
    Vh7                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
  G d de      Z G d	 d
e      Z G d de      Z e       ZdZd Zd Z G d d      Z e
d      Zd Zy)zBase Cache class.    N)sync_to_async)ImproperlyConfigured)import_string)_lazy_re_compilec                       e Zd Zy)InvalidCacheBackendErrorN__name__
__module____qualname__     O/home/dcms/DCMS/lib/python3.12/site-packages/django/core/cache/backends/base.pyr   r          r   r   c                       e Zd Zy)CacheKeyWarningNr	   r   r   r   r   r      r   r   r   c                       e Zd Zy)InvalidCacheKeyNr	   r   r   r   r   r      r   r   r      c                     |d|d| S )z
    Default function to generate keys.

    Construct the key used by all other methods. By default, prepend
    the `key_prefix`. KEY_FUNCTION can be used to specify an alternate
    function with custom key making behavior.
    :r   )key
key_prefixversions      r   default_key_funcr   !   s     $Wc22r   c                 B    | t        |       r| S t        |       S t        S )z]
    Function to decide which key function to use.

    Default to ``default_key_func``.
    )callabler   r   )key_funcs    r   get_key_funcr   ,   s(     HO **r   c                   f   e Zd Z e       Zd ZefdZd(dZd Z	d(dZ
edfdZedfdZd)d	Zd)d
ZedfdZedfdZedfdZedfdZd(dZd(dZd(dZd(dZedfdZedfdZd(dZd(dZd*dZd*dZd*dZd*dZd ZedfdZ edfdZ!d(dZ"d(dZ#d  Z$d! Z%d*d"Z&d*d#Z'd*d$Z(d*d%Z)d& Z*d' Z+y)+	BaseCachec                    |j                  d|j                  dd            }|	 t        |      }|| _        |j                  di       }|j                  d|j                  dd            }	 t        |      | _        |j                  d|j                  dd	            }	 t        |      | _        |j                  d
d      | _        |j                  dd      | _        t        |j                  d            | _
        y # t        t        f$ r d}Y w xY w# t        t        f$ r
 d| _        Y w xY w# t        t        f$ r
 d	| _        Y w xY w)NtimeoutTIMEOUTi,  OPTIONSmax_entriesMAX_ENTRIEScull_frequencyCULL_FREQUENCY   
KEY_PREFIX VERSION   KEY_FUNCTION)getint
ValueError	TypeErrordefault_timeout_max_entries_cull_frequencyr   r   r   r   )selfparamsr#   optionsr&   r(   s         r   __init__zBaseCache.__init__=   s:   **Y

9c(BCg,  '**Y+jjM30OP	$ #K 0D  $4gkkBRTU6VW	%#&~#6D  !**\26zz)Q/$VZZ%?@' 	*  I& 	$ #D	$ I& 	%#$D 	%s5   C> -D  D1 >DDD.-D.1E
	E
c                 r    |t         k(  r| j                  }n|dk(  rd}|dS t        j                         |z   S )zj
        Return the timeout value usable by this backend based upon the provided
        timeout.
        r   N)DEFAULT_TIMEOUTr4   time)r7   r#   s     r   get_backend_timeoutzBaseCache.get_backend_timeoutW   s=    
 o%**G\GtADIIK',AAr   Nc                 X    || j                   }| j                  || j                  |      S )ap  
        Construct the key used by all other methods. By default, use the
        key_func to generate a key (which, by default, prepends the
        `key_prefix' and 'version'). A different key function can be provided
        at the time of cache construction; alternatively, you can subclass the
        cache backend to provide custom key making behavior.
        )r   r   r   r7   r   r   s      r   make_keyzBaseCache.make_keyc   s)     ?llG}}S$//7;;r   c                 X    t        |      D ]  }t        j                  |t                y)z
        Warn about keys that would not be portable to the memcached
        backend. This encourages (but does not force) writing backend-portable
        cache code.
        N)memcache_key_warningswarningswarnr   )r7   r   warnings      r   validate_keyzBaseCache.validate_keyp   s&     -S1 	4GMM'?3	4r   c                 N    | j                  ||      }| j                  |       |S )z!Helper to make and validate keys.r   )rB   rH   rA   s      r   make_and_validate_keyzBaseCache.make_and_validate_keyy   s'    mmCm1#
r   c                     t        d      )z
        Set a value in the cache if the key does not already exist. If
        timeout is given, use that timeout for the key; otherwise use the
        default cache timeout.

        Return True if the value was stored, False otherwise.
        z4subclasses of BaseCache must provide an add() methodNotImplementedErrorr7   r   valuer#   r   s        r   addzBaseCache.add   s     "B
 	
r   c                 ^   K    t        | j                  d      ||||       d {   S 7 wNT)thread_sensitive)r   rQ   rO   s        r   aaddzBaseCache.aadd   3     C]488dC
 
 	
 
   $-+-c                     t        d      )z
        Fetch a given key from the cache. If the key does not exist, return
        default, which itself defaults to None.
        z3subclasses of BaseCache must provide a get() methodrM   r7   r   defaultr   s       r   r0   zBaseCache.get       
 ""WXXr   c                 \   K    t        | j                  d      |||       d {   S 7 wrS   )r   r0   rY   s       r   agetzBaseCache.aget   s1     C]488dC'
 
 	
 
   #,*,c                     t        d      )z
        Set a value in the cache. If timeout is given, use that timeout for the
        key; otherwise use the default cache timeout.
        z3subclasses of BaseCache must provide a set() methodrM   rO   s        r   setzBaseCache.set   r[   r   c                 ^   K    t        | j                  d      ||||       d {   S 7 wrS   )r   r`   rO   s        r   asetzBaseCache.aset   rV   rW   c                     t        d      )z
        Update the key's expiry time using timeout. Return True if successful
        or False if the key does not exist.
        z5subclasses of BaseCache must provide a touch() methodrM   r7   r   r#   r   s       r   touchzBaseCache.touch   s    
 "C
 	
r   c                 \   K    t        | j                  d      |||       d {   S 7 wrS   )r   re   rd   s       r   atouchzBaseCache.atouch   s1     E]4::E'
 
 	
 
r^   c                     t        d      )zh
        Delete a key from the cache and return whether it succeeded, failing
        silently.
        z6subclasses of BaseCache must provide a delete() methodrM   rA   s      r   deletezBaseCache.delete   s    
 "D
 	
r   c                 Z   K    t        | j                  d      ||       d {   S 7 wrS   )r   ri   rA   s      r   adeletezBaseCache.adelete   s&     F]4;;FsGTTTTs   "+)+c                 |    i }|D ]4  }| j                  || j                  |      }|| j                  us0|||<   6 |S )a.  
        Fetch a bunch of keys from the cache. For certain backends (memcached,
        pgsql) this can be *much* faster when fetching multiple values.

        Return a dict mapping each key in keys to its value. If the given
        key is missing, it will be missing from the response dict.
        rJ   r0   _missing_keyr7   keysr   dkvals         r   get_manyzBaseCache.get_many   sP      	A((1d//(AC$+++!	 r   c                    K   i }|D ]<  }| j                  || j                  |       d{   }|| j                  us8|||<   > |S 7 w)zSee get_many().rJ   Nr]   rn   ro   s         r   	aget_manyzBaseCache.aget_many   s[      	A		!T%6%6	HHC$+++!	  Is   )A
AA

A
c                     | j                  || j                  |      }|| j                  u r;t        |      r |       }| j                  ||||       | j                  |||      S |S )aU  
        Fetch a given key from the cache. If the key does not exist,
        add the key and set it to the default value. The default value can
        also be any callable. If timeout is given, use that timeout for the
        key; otherwise use the default cache timeout.

        Return the value of the key stored or retrieved.
        rJ   r#   r   )r0   rn   r   rQ   r7   r   rZ   r#   r   rs   s         r   
get_or_setzBaseCache.get_or_set   sk     hhsD--wh?$### !)HHS'7GHD 88C'8::
r   c                   K   | j                  || j                  |       d{   }|| j                  u rKt        |      r |       }| j                  ||||       d{    | j                  |||       d{   S |S 7 _7 $7 
w)zSee get_or_set().rJ   Nry   )r]   rn   r   rU   rz   s         r   aget_or_setzBaseCache.aget_or_set   s     IIc4#4#4gIFF$### !)))C'7)KKK 3AAA
 G L Bs3   "B
B<B
!B"B
=B>B
B
B
c                 V    | j                  || j                  |      | j                  uS )M
        Return True if the key is in the cache and has not expired.
        rJ   rm   rA   s      r   has_keyzBaseCache.has_key   s,    
 HHS$++WH=TEVEVV	
r   c                 r   K   | j                  || j                  |       d {   | j                  uS 7 wNrJ   rv   rA   s      r   ahas_keyzBaseCache.ahas_key  s7     ))C!2!2G)DD$$%	
Ds   "757c                     | j                  || j                  |      }|| j                  u rt        d|z        ||z   }| j                  |||       |S )zs
        Add delta to value in the cache. If the key does not exist, raise a
        ValueError exception.
        rJ   Key '%s' not found)r0   rn   r2   r`   r7   r   deltar   rP   	new_values         r   incrzBaseCache.incr  s^    
 d//AD%%%1C788EM	i1r   c                    K   | j                  || j                  |       d{   }|| j                  u rt        d|z        ||z   }| j                  |||       d{    |S 7 C7 w)zSee incr().rJ   Nr   )r]   rn   r2   rb   r   s         r   aincrzBaseCache.aincr  su     iiT%6%6iHHD%%%1C788EM	iiYi888 I 	9s!   "A,A(<A,!A*"A,*A,c                 ,    | j                  || |      S )zz
        Subtract delta from value in the cache. If the key does not exist, raise
        a ValueError exception.
        rJ   )r   r7   r   r   r   s       r   decrzBaseCache.decr  s    
 yyufgy66r   c                 H   K   | j                  || |       d {   S 7 wr   )r   r   s       r   adecrzBaseCache.adecr#  s#     ZZeVWZ====s   " "c                 $    | j                  |      S )r   )r   )r7   r   s     r   __contains__zBaseCache.__contains__&  s     ||C  r   c                 `    |j                         D ]  \  }}| j                  ||||        g S )a  
        Set a bunch of values in the cache at once from a dict of key/value
        pairs.  For certain backends (memcached), this is much more efficient
        than calling set() multiple times.

        If timeout is given, use that timeout for the key; otherwise use the
        default cache timeout.

        On backends that support it, return a list of keys that failed
        insertion, or an empty list if all keys were inserted successfully.
        ry   )itemsr`   r7   datar#   r   r   rP   s         r   set_manyzBaseCache.set_many/  s8     **, 	CJCHHS%'HB	C	r   c                 |   K   |j                         D ]"  \  }}| j                  ||||       d {    $ g S 7 w)Nry   )r   rb   r   s         r   	aset_manyzBaseCache.aset_many?  sF     **, 	JJC))C)III	J	 Js   /<:	<c                 8    |D ]  }| j                  ||        y)z
        Delete a bunch of values in the cache at once. For certain backends
        (memcached), this is much more efficient than calling delete() multiple
        times.
        rJ   N)ri   r7   rp   r   r   s       r   delete_manyzBaseCache.delete_manyD  s#      	.CKKWK-	.r   c                 T   K   |D ]  }| j                  ||       d {     y 7 wr   )rk   r   s       r   adelete_manyzBaseCache.adelete_manyM  s.      	5C,,sG,444	54s   (&(c                     t        d      )z+Remove *all* values from the cache at once.z5subclasses of BaseCache must provide a clear() methodrM   r7   s    r   clearzBaseCache.clearQ  s    !C
 	
r   c                 V   K    t        | j                  d              d {   S 7 wrS   )r   r   r   s    r   aclearzBaseCache.aclearW  s"     E]4::EGGGGs    )')c                     || j                   }| j                  || j                  |      }|| j                  u rt        d|z        | j	                  ||||z          | j                  ||       ||z   S )zf
        Add delta to the cache version for the supplied key. Return the new
        version.
        rJ   r   )r   r0   rn   r2   r`   ri   r7   r   r   r   rP   s        r   incr_versionzBaseCache.incr_versionZ  s{    
 ?llGd//AD%%%1C788eWu_5C)r   c                 4  K   || j                   }| j                  || j                  |       d{   }|| j                  u rt        d|z        | j	                  ||||z          d{    | j                  ||       d{    ||z   S 7 _7 &7 w)zSee incr_version().NrJ   r   )r   r]   rn   r2   rb   rk   r   s        r   aincr_versionzBaseCache.aincr_versionj  s     ?llGiiT%6%6iHHD%%%1C788iiUGeOi<<<ll3l000 I 	=0s3   0BB:B-B.BB	
BBBc                 *    | j                  || |      S )zm
        Subtract delta from the cache version for the supplied key. Return the
        new version.
        )r   r   s       r   decr_versionzBaseCache.decr_versionw  s    
   ufg66r   c                 F   K   | j                  || |       d {   S 7 wN)r   r   s       r   adecr_versionzBaseCache.adecr_version~  s#     ''eVW====s   !!c                      y)zClose the cache connectionNr   r7   kwargss     r   closezBaseCache.close  s    r   c                    K   y wr   r   r   s     r   aclosezBaseCache.aclose  s	     s   r   )NN)r.   N),r
   r   r   objectrn   r:   r=   r?   rB   rH   rK   rQ   rU   r0   r]   r`   rb   re   rg   ri   rk   rt   rw   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   :   s   8LA4 +: 
B<4 '6t 

 .=d 

Y

 '6t Y .=d 

 "1$ 
 )8 


U 0? & 7Ft 



7>! &5d   -<T 
.5
H 7>r   r!   z[\x00-\x20\x7f]c              #      K   t        |       t        kD  rd| dt        d t        j                  |       rd|  y y w)Nz4Cache key will cause errors if used with memcached: z (longer than )zMCache key contains characters that will cause errors if used with memcached: )lenMEMCACHE_MAX_KEY_LENGTHmemcached_error_chars_research)r   s    r   rD   rD     sO     
3x)) #&'>@	
  &&s+"	
 ,s   A A)__doc__r>   rE   asgiref.syncr   django.core.exceptionsr   django.utils.module_loadingr   django.utils.regex_helperr   r   RuntimeWarningr   r2   r   r   r=   r   r   r   r!   r   rD   r   r   r   <module>r      sz       & 7 5 6	3 		n 		j 	 (  3L L^
 ,,>? 

r   