3
(h                 @   sH   d Z ddlmZ ddlmZ ddlmZ eddddd	gZdddZdS )z
LRU cache implementation for Python 2.7

Ported from http://code.activestate.com/recipes/578078/ and simplified for our
use (only support maxsize > 0 and positional arguments).
    )
namedtuple)update_wrapper)RLock	CacheInfohitsmissesmaxsizecurrsized   c                s    fdd}|S )zLeast-recently-used cache decorator.

    Arguments to the cached function must be hashable.

    See:  http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

    c                s   t  ddgd\ jtt 	g }||d d g|d d < |g
d\r\dkshtd  	
fdd} 	fdd	}	
fd
d}|_||_||_t|S )Nr            zmaxsize %s not supportedc        
         sB  | }	t |}|d k	rx\}|\}}}}||< ||< | }| |< |< ||< ||<    d7  < |S W d Q R X |  }	 \}|krn
kr|}||< ||< |  }d< | }	d  |< |< |	= ||< n,| }||||g}| |<  |< |<   d7  < W d Q R X |S )Nr   r    )
argskeylinkrootZ	link_prevZ	link_nextresultlastZoldrootZoldkey)HITSKEYMISSESNEXTPREVRESULT_lencache	cache_getlockr   nonlocal_rootstatsuser_functionr   @/tmp/pip-install-q3hcpn_q/psycopg2-binary/psycopg2/_lru_cache.pywrapper&   sB    
z7lru_cache.<locals>.decorating_function.<locals>.wrapperc                  s*    t    tS Q R X dS )zReport cache statisticsN)
_CacheInfolenr   )r   r   r   r   r   r    r   r"   
cache_infoV   s    z:lru_cache.<locals>.decorating_function.<locals>.cache_infoc                 sH   :  j   d } | | ddg| dd< ddgdd< W dQ R X dS )z$Clear the cache and cache statisticsr   N)clear)r   )r   r   r   r    r   r"   cache_clear[   s
    z;lru_cache.<locals>.decorating_function.<locals>.cache_clear)r   r   )r   r   r   r   )	dictgetr%   r   AssertionError__wrapped__r&   r(   r   )r!   r   r#   r&   r(   )r   )r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   decorating_function   s$    &0z&lru_cache.<locals>.decorating_functionr   )r   r-   r   )r   r"   	lru_cache   s    Qr.   N)r
   )	__doc__collectionsr   	functoolsr   	threadingr   r$   r.   r   r   r   r"   <module>   s
   