
oTc           @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z m Z d d d     YZ d d  Z d	   Z	 d
 e
 d  Z d S(   iN(   t   cptoolst   httpt   MemoryCachec           B   s_   e  Z d  Z d Z d Z d Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   i  i i iX  c         C   sg   |  j    t j d |  j d d  } | |  _ t t j d  rL t | _ n | j t  | j	   d  S(   Nt   targett   namet   expire_cachet   daemon(
   t   cleart	   threadingt   ThreadR   t   expiration_threadt   hasattrt   TrueR   t	   setDaemont   start(   t   selft   t(    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   __init__   s    
	c         C   sL   i  |  _  i  |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S(   Ni    (   t   cachet   expirationst   tot_putst   tot_getst   tot_histt   tot_expirest   tot_non_modifiedt   cursize(   R   (    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyR      s    							c         C   s   t  j d t  j j  S(   Nt   qs(   t   cherrypyt   urlt   requestt   query_string(   R   (    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   key&   s    c         C   s   x t  r t  j    } x |  j j   D]| \ } } | | k r% xT | D]L \ } } y, |  j | =|  j d 7_ |  j | 8_ WqD t k
 r qD XqD W|  j | =q% q% Wt  j d  q Wd  S(   Ni   g?(   t   timeR   t   itemsR   R   R   t   KeyErrort   sleep(   R   t   nowt   expiration_timet   objectst   obj_sizet   obj_key(    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyR   )   s    	
c         C   sK   |  j  d 7_  |  j j |  j   d   } | rC |  j d 7_ | Sd  Sd  S(   Ni   (   R   R   t   getR   t   NoneR   (   R   t
   cache_item(    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyR)   =   s    c         C   s   t  |  j  |  j k  r t  | d  } |  j | } | |  j k  r | |  j k  r t j j |  j	 } |  j
   } |  j j | g   } | j | | f  | |  j | <|  j d 7_ | |  _ q n  d  S(   Ni   i   (   t   lenR   t
   maxobjectsR   t   maxobj_sizet   maxsizeR   t   responseR    t   delayR   R   t
   setdefaultt   appendR   (   R   t   objR'   t
   total_sizeR%   R(   t   bucket(    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   putG   s    c         C   s   |  j  j |  j   d   d  S(   N(   R   t   popR   R*   (   R   (    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   deleteX   s    (   t   __name__t
   __module__R-   R.   R/   R1   R   R   R   R   R)   R7   R9   (    (    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyR   	   s   					
	t   POSTt   PUTt   DELETEc         K   s  t  j } | j |  k r; t  j j   t | _ t | _ t St  j j   } t	 |  | _ } | | _ | rt  j
 } | \ } } } }	 }
 xU | j d  D]D } | j } |
 | | j j | d  k r t | _ t | _ t Sq Wt j   | _ } x- | D]% } t j | | t j | |   q Wt t | j |	   | j d <y t j   Wn= t  j k
 r} | j d k rt  j j d 7_ n    n X| | _ | | _ n  | S(   Nt   Varyt   missingt   Agei0  i   (   R   R   t   methodt   _cacheR9   t   Falset   cachedt	   cacheableR)   t   boolR0   t   elementst   valuet   headersR   R   t	   HeaderMapt   dictt   __setitem__t   __getitem__t   strt   intR    R    t   validate_sincet   HTTPRedirectt   statusR   t   body(   t   invalid_methodst   kwargsR   t
   cache_datat   cR0   t   st   ht   bt   create_timet   original_req_headerst   header_elementR   t   rht   kt   x(    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyR)   \   s>    			
				# 	c             s.     f d   }  t  j   |    j    _ d  S(   Nc         3   s  g  } x  |  D] } | j  |  | Vq W  j j d d   d k rd j |  }  g  t j j j d  D] } | j ^ qi } | r t	 g  t j
 j j   D]$ \ } } | | k r | | f ^ q  } n i  } t j j   j   j p i  |    j | f  n  d  S(   Nt   Pragmas   no-cachet    R?   (   R3   RJ   R)   R*   t   joinR   R0   RH   RI   RL   R   t	   iteritemsRC   R7   RS   R    (   RT   t   outputt   chunkt   het   varyR`   t   vt   sel_headers(   R0   (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   tee   s    	($(   R   R0   RT   (   Rl   (    (   R0   s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt
   tee_output   s    	i    c         C   s$  t  j } | j } t } | sH x' d D] } | | k r% t } Pq% q% Wn  | s t |  t j  rw d |  j |  j	 }  n  |  d k r | s d | k r d | d <n  t  j
 j d k r | s d
 | k r d | d
 <q n  t j d  } n t j | j |   } | sd | k r | | d <q n  d  S(   Nt   Etags   Last-ModifiedRA   t   ExpiresiQ i    Rb   s   no-cachei   s   Cache-Controls   no-cache, must-revalidateg   nA(   Rn   s   Last-ModifiedRA   Ro   (   i   i   (   R   R0   RJ   RD   R   t
   isinstancet   datetimet	   timedeltat   dayst   secondsR   t   protocolR   t   HTTPDateR    (   t   secst   forceR0   RJ   RF   t	   indicatort   expiry(    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   expires   s*    		(    (   R<   R=   R>   (   Rq   R   R    R   t   cherrypy.libR    R   R   R)   Rm   RD   R{   (    (    (    s(   /var/www/SABnzbd/cherrypy/lib/caching.pyt   <module>   s   SS	