
P'^c           @   s  d  d l  Z  d  d l Z d  d l m Z m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 d  d l m Z d d l m Z d d l m Z d d	 l m Z m Z m Z d d
 l m Z d d l m Z d Z e d    Z d   Z e e e d    Z e d  Z e e e d    Z e d    Z d e j  f d     YZ! d e f d     YZ" e"   Z# e d    Z$ e e%  d    Z& d S(   iN(   t   memoizet   post_processingt   ContextDecorator(   t   DEFAULT_DB_ALIAS(   t   Ft
   Expression(   t   StrictVersioni   (   t   settings(   t
   get_prefix(   t   redis_clientt   handle_connection_failuret   load_script(   t   cache_invalidated(   t   queue_when_in_transactiont   invalidate_objt   invalidate_modelt   invalidate_allt   no_invalidationc          C   s&   t  j   d }  t |   t d  k S(   Nt   redis_versions   4.0(   R	   t   infoR   (   R   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   redis_can_unlink   s    c          G   s-   t    r t j d |   n t j |    d  S(   Nt   UNLINK(   R   R	   t   execute_commandt   delete(   t   keys(    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   invalidate_keys   s    	c      	   C   s   t  j s t j r d  S|  j j }  t d |  j j t | j	    f g d | g  } t
 d d t   d | g d |  j j t j | d t g  t j d |  d	 |  d  S(
   Nt
   _cond_dnfst   dbst
   invalidatet   stripR   t   argst   defaultt   sendert   obj_dict(   R   t   activeR   t   CACHEOPS_ENABLEDt   _metat   concrete_modelR   t   db_tablet   listt   itemsR   R   t   jsont   dumpst   strR   t   send(   t   modelR!   t   usingt   prefix(    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   invalidate_dict    s    3	c         C   s/   |  j  j j } t | t | |   d | d S(   sF   
    Invalidates caches that can possibly be influenced by object
    R.   N(   t	   __class__R$   R%   R0   t   get_obj_dict(   t   objR.   R-   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR   .   s    c         C   s   t  j s t j r d S|  j j }  t d |  j j g d | g  } t j	 d | |  j j f  } | r t j
 |  } t |  | } t |   n  t j d |  d d  d S(   s   
    Invalidates all caches for given model.
    NOTE: This is a heavy artillery which uses redis KEYS request,
          which could be relatively slow on large datasets.
    Nt   tablesR   s   %sconj:%s:*R    R!   (   R   R"   R   R#   R$   R%   R   R&   R	   R   t   sunionR'   R   R   R,   t   None(   R-   R.   R/   t
   conjs_keyst
   cache_keysR   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR   6   s    !c           C   s;   t  j s t j r d  St j   t j d d  d d   d  S(   NR    R!   (	   R   R"   R   R#   R	   t   flushdbR   R,   R6   (    (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR   L   s    
t   InvalidationStatec           B   s   e  Z d    Z RS(   c         C   s   d |  _  d  S(   Ni    (   t   depth(   t   self(    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   __init__U   s    (   t   __name__t
   __module__R=   (    (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR:   T   s   t   _no_invalidationc           B   s2   e  Z e   Z d    Z d   Z e d    Z RS(   c         C   s   |  j  j d 7_ d  S(   Ni   (   t   stateR;   (   R<   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt	   __enter__[   s    c         C   s   |  j  j d 8_ d  S(   Ni   (   RA   R;   (   R<   t   typet   valuet	   traceback(    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   __exit__^   s    c         C   s
   |  j  j S(   N(   RA   R;   (   R<   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR"   a   s    (   R>   R?   R:   RA   RB   RF   t   propertyR"   (    (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR@   X   s   			c         C   s   t  d   |  j j D  S(   Nc         s   s'   |  ] } t  | t j  s | Vq d  S(   N(   t
   isinstanceR   t   CACHEOPS_SKIP_FIELDS(   t   .0t   f(    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pys	   <genexpr>l   s    (   t   tupleR$   t   fields(   R-   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   serializable_fieldsj   s    c         c   s|   xu t  |   D]g } t | | j  } | d  k rB | j d  f Vq t | t t f  r] q q | j | j |  f Vq Wd  S(   N(   RN   t   getattrt   attnameR6   RH   R   R   t   get_prep_value(   R-   R3   t   fieldRD   (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyR2   o   s    (   R   R   R   R   ('   R)   t	   threadingt   funcyR    R   R   t	   django.dbR   t   django.db.models.expressionsR   R   t   distutils.versionR   t   confR   t   shardingR   t   redisR	   R
   R   t   signalsR   t   transactionR   t   __all__R   R   R0   R   R   R   t   localR:   R@   R   RN   t   dictR2   (    (    (    s7   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/invalidation.pyt   <module>   s4   		