ó
P'—^c           @@  s>  d  d l  m Z d  d l Z d  d l m Z d  d l Z d  d l m Z d  d l m	 Z	 d  d l
 m Z m Z m Z m Z m Z d  d l Z d  d l m Z d d	 l m Z e j rÇ e d
 „  ƒ Z n e Z d Z d e j f d „  ƒ  YZ e d „  ƒ Z d  d l Z d  d l Z e j d e j ƒ Z  e e! d „ ƒ Z" d S(   i    (   t   absolute_importN(   t   contextmanager(   t   ImproperlyConfigured(   t   import_string(   t	   decoratort   identityt   memoizet   omitt
   LazyObject(   t   Sentineli   (   t   settingsc         C@  se   y |  ƒ  SWnS t  j k
 r7 } t j d | t ƒ n* t  j k
 r` } t j d | t ƒ n Xd  S(   Ns,   The cacheops cache is unreachable! Error: %ss'   The cacheops cache timed out! Error: %s(   t   redist   ConnectionErrort   warningst   warnt   RuntimeWarningt   TimeoutError(   t   callt   e(    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyt   handle_connection_failure   s    i<   t   CacheopsRedisc           B@  sJ   e  Z e e j j ƒ Z e e d  „ ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 RS(   c         c@  s_   | s |  j  | ƒ VnD t } z$ |  j | ƒ } | d  k } | VWd  | rZ |  j | ƒ n  Xd  S(   N(   t   gett   Falset   _get_or_lockt   Nonet   _release_lock(   t   selft   keyt   lockt   lockedt   data(    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyt   getting"   s    	c         C@  s¢   t  |  d |  j d ƒ ƒ |  _ | d } xs t r |  j | ƒ } | d  k rt |  j d | | g d t g ƒ r„ d  Sn | d k r„ | S|  j | | d t ƒq+ Wd  S(   Nt   _locksÌ   
            local locked = redis.call('set', KEYS[1], 'LOCK', 'nx', 'ex', ARGV[1])
            if locked then
                redis.call('del', KEYS[2])
            end
            return locked
        s   :signalt   keyst   argst   LOCKt   timeout(   t   getattrt   register_scriptR    t   TrueR   R   t   LOCK_TIMEOUTt
   brpoplpush(   R   R   t
   signal_keyR   (    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyR   0   s    
	!c         C@  sB   t  |  d |  j d ƒ ƒ |  _ | d } |  j d | | g ƒ d  S(   Nt   _unlocksÖ   
            if redis.call('get', KEYS[1]) == 'LOCK' then
                redis.call('del', KEYS[1])
            end
            redis.call('lpush', KEYS[2], 1)
            redis.call('expire', KEYS[2], 1)
        s   :signalR!   (   R%   R&   R+   (   R   R   R*   (    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyR   F   s    
(   t   __name__t
   __module__R   R   t   StrictRedisR   R   R   R   R   R   (    (    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyR      s
   c          C@  sü   t  j r! t  j r! t d ƒ ‚ n  t }  t  j rB t t  j ƒ }  n  t  j rÆ d d h t t  j ƒ k su t d ƒ ‚ n  t t  j d t	 t  j d ƒ  } | j
 t  j d d |  d t  j j d d ƒ ƒSt t  j t j ƒ rë |  j t  j ƒ S|  t  j   Sd  S(	   Ns;   CACHEOPS_REDIS and CACHEOPS_SENTINEL are mutually exclusivet	   locationst   service_names8   Specify locations and service_name for CACHEOPS_SENTINELt   dbt   redis_classi    (   R/   R0   R1   (   R
   t   CACHEOPS_REDISt   CACHEOPS_SENTINELR   R   t   CACHEOPS_CLIENT_CLASSR   t   setR	   R   t
   master_forR   t
   isinstancet   sixt   string_typest   from_url(   t   client_classt   sentinel(    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyt   redis_clientS   s$    		
s   TOSTRIP.*/TOSTRIPc         C@  sn   t  j j t  j j t ƒ d |  ƒ } t | ƒ  } | j ƒ  } Wd  QX| ra t j d | ƒ } n  t	 j
 | ƒ S(   Ns
   lua/%s.luat    (   t   ost   patht   joint   dirnamet   __file__t   opent   readt   STRIP_REt   subR>   R&   (   t   namet   stript   filenamet   ft   code(    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyt   load_scriptw   s    %(#   t
   __future__R    R   t
   contextlibR   R9   t   django.core.exceptionsR   t   django.utils.module_loadingR   t   funcyR   R   R   R   R   R   t   redis.sentinelR	   t   confR
   t   CACHEOPS_DEGRADE_ON_FAILURER   R(   R.   R   R>   t   ret   os.pathR@   t   compilet   SRG   R   RN   (    (    (    s0   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/redis.pyt   <module>   s(   (		4