ó
†¾^Yc           @   sˆ   d  Z  d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   sÄ   
This module contains multithread-safe cache implementations.

All Caches have

    getorbuild(key, builder)
    delentry(key)

methods and allow configuration when instantiating the cache class.
iÿÿÿÿ(   t   timet
   BasicCachec           B   sM   e  Z d  d „ Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z	 RS(   i€   c         C   s-   | |  _  t | | d ƒ |  _ i  |  _ d  S(   Ni   (   t
   maxentriest   intt   prunenumt   _dict(   t   selfR   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyt   __init__   s    	c         C   s   |  j  j ƒ  d  S(   N(   R   t   clear(   R   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR      s    c         C   s   |  j  | S(   N(   R   (   R   t   key(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyt	   _getentry   s    c         C   s   |  j  ƒ  | |  j | <d  S(   N(   t   _prunelowestweightR   (   R   R	   t   entry(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyt	   _putentry   s    
c         C   s2   y |  j  | =Wn t k
 r- | r. ‚  q. n Xd  S(   N(   R   t   KeyError(   R   R	   t   raising(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyt   delentry   s
    c         C   sP   y |  j  | ƒ } Wn3 t k
 rH |  j | | ƒ } |  j | | ƒ n X| j S(   N(   R
   R   t   _buildR   t   value(   R   R	   t   builderR   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyt
   getorbuild$   s    c         C   sª   t  |  j ƒ } | |  j k r¦ g  |  j j ƒ  D] \ } } | j | f ^ q. } | j ƒ  | |  j } | d k r¦ x. | |  D] \ } } |  j | d t ƒq} Wq¦ n  d S(   s'    prune out entries with lowest weight. i    R   N(	   t   lenR   R   t   itemst   weightt   sortR   R   t   False(   R   t
   numentriesR	   R   R   t   indexR   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   ,   s    .
(
   t   __name__t
   __module__R   R   R
   R   R   R   R   R   (    (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR      s   				t   BuildcostAccessCachec           B   s   e  Z d  Z d „  Z RS(   s‡   A BuildTime/Access-counting cache implementation.
        the weight of a value is computed as the product of

            num-accesses-of-a-value * time-to-build-the-value

        The values with the least such weights are evicted
        if the cache maxentries threshold is superceded.
        For implementation flexibility more than one object
        might be evicted at a time.
    c         C   s,   t  ƒ  } | ƒ  } t  ƒ  } t | | | ƒ S(   N(   t   gettimet   WeightedCountingEntry(   R   R	   R   t   startt   valt   end(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   G   s    			(   R   R   t   __doc__R   (    (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   :   s   
R    c           B   s&   e  Z d  „  Z d „  Z e e ƒ Z RS(   c         C   s   | |  _  | |  _ |  _ d  S(   N(   t   _valueR   t
   _oneweight(   R   R   t	   oneweight(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   O   s    	c         C   s   |  j  |  j 7_  |  j S(   N(   R   R&   R%   (   R   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   S   s    (   R   R   R   R   t   property(    (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR    N   s   		t
   AgingCachec           B   s/   e  Z d  Z d d d „ Z d „  Z d „  Z RS(   s;    This cache prunes out cache entries that are too old.
    i€   g      $@c         C   s#   t  t |  ƒ j | ƒ | |  _ d  S(   N(   t   superR)   R   t
   maxseconds(   R   R   R+   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   [   s    c         C   s9   |  j  | } | j ƒ  r5 |  j | ƒ t | ƒ ‚ n  | S(   N(   R   t	   isexpiredR   R   (   R   R	   R   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR
   _   s
    c         C   s&   | ƒ  } t  | t ƒ  |  j ƒ } | S(   N(   t
   AgingEntryR   R+   (   R   R	   R   R"   R   (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   f   s    	(   R   R   R$   R   R
   R   (    (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR)   X   s   	R-   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   t   expirationtime(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR   l   s    	c         C   s   t  ƒ  } | |  j k S(   N(   R   R   (   R   t   t(    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR,   p   s    	(   R   R   R   R,   (    (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyR-   k   s   	N(	   R$   R    R   t   objectR   R   R    R)   R-   (    (    (    s.   /tmp/pip-build-hU8Cw8/py/py/_path/cacheutil.pyt   <module>
   s   -
