
 XO&                 @   s   d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 Gd d   d e  Z
 Gd d	   d	 e  Z e   Z d
 Z d d   Z d d   Z Gd d   d e  Z d S)zBase Cache class.    )unicode_literalsN)DjangoRuntimeWarningImproperlyConfigured)import_stringc               @   s   e  Z d  Z d S)InvalidCacheBackendErrorN)__name__
__module____qualname__ r
   r
   M/home/ubuntu/projects/ifolica/build/django/django/core/cache/backends/base.pyr      s   r   c               @   s   e  Z d  Z d S)CacheKeyWarningN)r   r   r	   r
   r
   r
   r   r      s   r      c             C   s   d | | |  f S)z
    Default function to generate keys.

    Constructs the key used by all other methods. By default it prepends
    the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
    function with custom key making behavior.
    z%s:%s:%sr
   )key
key_prefixversionr
   r
   r   default_key_func   s    r   c             C   s-   |  d k	 r) t  |   r |  St |   Sn  t S)z^
    Function to decide which key function to use.

    Defaults to ``default_key_func``.
    N)callabler   r   )key_funcr
   r
   r   get_key_func&   s
    r   c               @   sK  e  Z d  Z d d   Z e d d  Z d d d  Z e d d d	  Z d d d
 d  Z e d d d  Z	 d d d  Z
 d d d  Z d e d d d  Z d d d  Z d d d d  Z d d d d  Z d d   Z e d d d  Z d d d   Z d! d"   Z d# d$   Z d d d% d&  Z d d d' d(  Z d) d*   Z d S)+	BaseCachec             C   si  | j  d | j  d d   } | d  k	 r^ y t |  } Wq^ t t f k
 rZ d } Yq^ Xn  | |  _ | j  d i   } | j  d | j  d d   } y t |  |  _ Wn! t t f k
 r d |  _ Yn X| j  d | j  d d	   } y t |  |  _ Wn! t t f k
 r"d	 |  _ Yn X| j  d
 d  |  _ | j  d d  |  _ t	 | j  d   |  _
 d  S)NtimeoutTIMEOUTi,  OPTIONSmax_entriesZMAX_ENTRIEScull_frequencyZCULL_FREQUENCY   Z
KEY_PREFIX VERSION   ZKEY_FUNCTION)getint
ValueError	TypeErrordefault_timeoutZ_max_entriesZ_cull_frequencyr   r   r   r   )selfparamsr   optionsr   r   r
   r
   r   __init__5   s*    	zBaseCache.__init__c             C   sK   | t  k r |  j } n | d k r- d } n  | d k r= d St j   | S)zk
        Returns the timeout value usable by this backend based upon the provided
        timeout.
        r   r   N)DEFAULT_TIMEOUTr#   time)r$   r   r
   r
   r   get_backend_timeoutO   s
    	zBaseCache.get_backend_timeoutNc             C   s4   | d k r |  j  } n  |  j | |  j |  } | S)as  Constructs the key used by all other methods. By default it
        uses 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.
        N)r   r   r   )r$   r   r   new_keyr
   r
   r   make_key[   s    zBaseCache.make_keyc             C   s   t  d   d S)a  
        Set a value in the cache if the key does not already exist. If
        timeout is given, that timeout will be used for the key; otherwise
        the default cache timeout will be used.

        Returns True if the value was stored, False otherwise.
        z4subclasses of BaseCache must provide an add() methodN)NotImplementedError)r$   r   valuer   r   r
   r
   r   addi   s    zBaseCache.addc             C   s   t  d   d S)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() methodN)r.   )r$   r   defaultr   r
   r
   r   r   s   s    zBaseCache.getc             C   s   t  d   d S)z
        Set a value in the cache. If timeout is given, that timeout will be
        used for the key; otherwise the default cache timeout will be used.
        z3subclasses of BaseCache must provide a set() methodN)r.   )r$   r   r/   r   r   r
   r
   r   setz   s    zBaseCache.setc             C   s   t  d   d S)z@
        Delete a key from the cache, failing silently.
        z6subclasses of BaseCache must provide a delete() methodN)r.   )r$   r   r   r
   r
   r   delete   s    zBaseCache.deletec             C   sI   i  } x< | D]4 } |  j  | d | } | d k	 r | | | <q q W| S)a/  
        Fetch a bunch of keys from the cache. For certain backends (memcached,
        pgsql) this can be *much* faster when fetching multiple values.

        Returns a dict mapping each key in keys to its value. If the given
        key is missing, it will be missing from the response dict.
        r   N)r   )r$   keysr   dkvalr
   r
   r   get_many   s    zBaseCache.get_manyc             C   s   | d k r t  d   n  |  j | d | } | d k r t |  rT |   } n  |  j | | d | d | |  j | | d | S| S)ai  
        Fetch a given key from the cache. If the key does not exist,
        the key is added and set to the default value. The default value can
        also be any callable. If timeout is given, that timeout will be used
        for the key; otherwise the default cache timeout will be used.

        Return the value of the key stored or retrieved.
        NzYou need to specify a value.r   r   )r!   r   r   r0   )r$   r   r1   r   r   r7   r
   r
   r   
get_or_set   s    	zBaseCache.get_or_setc             C   s   |  j  | d | d k	 S)zN
        Returns True if the key is in the cache and has not expired.
        r   N)r   )r$   r   r   r
   r
   r   has_key   s    zBaseCache.has_keyr   c             C   sX   |  j  | d | } | d k r4 t d |   n  | | } |  j | | d | | S)zs
        Add delta to value in the cache. If the key does not exist, raise a
        ValueError exception.
        r   NzKey '%s' not found)r   r!   r2   )r$   r   deltar   r/   Z	new_valuer
   r
   r   incr   s    
zBaseCache.incrc             C   s   |  j  | | d | S)zz
        Subtract delta from value in the cache. If the key does not exist, raise
        a ValueError exception.
        r   )r<   )r$   r   r;   r   r
   r
   r   decr   s    zBaseCache.decrc             C   s   |  j  |  S)zN
        Returns True if the key is in the cache and has not expired.
        )r:   )r$   r   r
   r
   r   __contains__   s    zBaseCache.__contains__c             C   s=   x6 | j    D]( \ } } |  j | | d | d | q Wd S)aM  
        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, that timeout will be used for the key; otherwise
        the default cache timeout will be used.
        r   r   N)itemsr2   )r$   datar   r   r   r/   r
   r
   r   set_many   s    	zBaseCache.set_manyc             C   s(   x! | D] } |  j  | d | q Wd S)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.
        r   N)r3   )r$   r4   r   r   r
   r
   r   delete_many   s    zBaseCache.delete_manyc             C   s   t  d   d S)z+Remove *all* values from the cache at once.z5subclasses of BaseCache must provide a clear() methodN)r.   )r$   r
   r
   r   clear   s    zBaseCache.clearc             C   s   t  |  t k r/ t j d | t f t  n  xJ | D]B } t |  d k  s` t |  d k r6 t j d | t  Pq6 q6 Wd S)z
        Warn about keys that would not be portable to the memcached
        backend. This encourages (but does not force) writing backend-portable
        cache code.
        zGCache key will cause errors if used with memcached: %r (longer than %s)!      zOCache key contains characters that will cause errors if used with memcached: %rN)lenMEMCACHE_MAX_KEY_LENGTHwarningswarnr   ord)r$   r   charr
   r
   r   validate_key   s    $zBaseCache.validate_keyc             C   s   | d k r |  j  } n  |  j | d | } | d k rL t d |   n  |  j | | d | | |  j | d | | | S)z_Adds delta to the cache version for the supplied key. Returns the
        new version.
        Nr   zKey '%s' not found)r   r   r!   r2   r3   )r$   r   r;   r   r/   r
   r
   r   incr_version   s    zBaseCache.incr_versionc             C   s   |  j  | | |  S)zfSubtracts delta from the cache version for the supplied key. Returns
        the new version.
        )rM   )r$   r   r;   r   r
   r
   r   decr_version  s    zBaseCache.decr_versionc             K   s   d S)zClose the cache connectionNr
   )r$   kwargsr
   r
   r   close  s    zBaseCache.close)r   r   r	   r'   r)   r+   r-   r0   r   r2   r3   r8   r9   r:   r<   r=   r>   rA   rB   rC   rL   rM   rN   rP   r
   r
   r
   r   r   4   s(   
		r   )__doc__
__future__r   r*   rH   Zdjango.core.exceptionsr   r   Zdjango.utils.module_loadingr   r   r   objectr)   rG   r   r   r   r
   r
   r
   r   <module>   s   	