U
    祡cF                     @   s  d Z dZddlZddlZddlZddlZddlZzddlmZ W n  e	k
r`   ddl
mZ Y nX ddlmZmZmZmZmZmZ ddlmZ edd	d
ddgZG dd deZG dd deZdd Zd"ddZd#ddZd$ddZd%ddZdejdfddZddejdfd d!Z dS )&z?`functools.lru_cache` compatible memoizing function decorators.)
fifo_cache	lfu_cache	lru_cache	mru_cacherr_cache	ttl_cache    N)RLock   )	FIFOCacheLFUCacheLRUCacheMRUCacheRRCacheTTLCache)keys	CacheInfohitsmissesmaxsizecurrsizec                   @   s$   e Zd Zedd Zedd ZdS )_UnboundCachec                 C   s   d S N selfr   r   3/tmp/pip-unpacked-wheel-f60fxks3/cachetools/func.pyr      s    z_UnboundCache.maxsizec                 C   s   t | S r   )lenr   r   r   r   r      s    z_UnboundCache.currsizeN)__name__
__module____qualname__propertyr   r   r   r   r   r   r      s   
r   c                   @   s    e Zd Zdd Zedd ZdS )_UnboundTTLCachec                 C   s   t | tj|| d S r   )r   __init__mathinf)r   ttltimerr   r   r   r"   $   s    z_UnboundTTLCache.__init__c                 C   s   d S r   r   r   r   r   r   r   '   s    z_UnboundTTLCache.maxsizeN)r   r   r   r"   r    r   r   r   r   r   r!   #   s   r!   c                    s    j  fdd}|S )Nc                    s   r
t jnt jd t  fdd}fdd}fdd}||_||_fdd	|_t|  |S )
Nr   c               
      s   | |}F z" | }d7 |W W  5 Q R  S  t k
rN   d7 Y nX W 5 Q R X | |}z*  ||W  5 Q R  W S Q R X W n tk
r   | Y S X d S )Nr	   )KeyError
setdefault
ValueError)argskwargskv)cachefuncr   keylockr   r   r   wrapper4   s    

$z*_cache.<locals>.decorator.<locals>.wrapperc               	      s*     j }  j}W 5 Q R X t| |S r   )r   r   
_CacheInfo)r   r   r.   r   r1   r   r   r   
cache_infoF   s    z-_cache.<locals>.decorator.<locals>.cache_infoc                	      s,    z    W 5 d X W 5 Q R X d S )Nr   )clearr   r4   r   r   cache_clearL   s    z._cache.<locals>.decorator.<locals>.cache_clearc                      s
    dS )Nr   typedr   r   r8   r   r   <lambda>V       z+_cache.<locals>.decorator.<locals>.<lambda>)	r   ZtypedkeyZhashkeyr   r5   r7   Zcache_parameters	functoolsupdate_wrapper)r/   r2   r5   r7   r.   r   r9   )r/   r   r0   r1   r   r   	decorator/   s    z_cache.<locals>.decorator)r   )r.   r9   r?   r   r>   r   _cache,   s    +r@      Fc                 C   s@   | dkrt t |S t| r.t td|| S t t| |S dS )zDecorator to wrap a function with a memoizing callable that saves
    up to `maxsize` results based on a First In First Out (FIFO)
    algorithm.

    NrA   )r@   r   callabler
   r8   r   r   r   r   ]   s
    r   c                 C   s@   | dkrt t |S t| r.t td|| S t t| |S dS )zDecorator to wrap a function with a memoizing callable that saves
    up to `maxsize` results based on a Least Frequently Used (LFU)
    algorithm.

    NrA   )r@   r   rB   r   r8   r   r   r   r   k   s
    r   c                 C   s@   | dkrt t |S t| r.t td|| S t t| |S dS )zDecorator to wrap a function with a memoizing callable that saves
    up to `maxsize` results based on a Least Recently Used (LRU)
    algorithm.

    NrA   )r@   r   rB   r   r8   r   r   r   r   y   s
    r   c                 C   s@   | dkrt t |S t| r.t td|| S t t| |S dS )zDecorator to wrap a function with a memoizing callable that saves
    up to `maxsize` results based on a Most Recently Used (MRU)
    algorithm.
    NrA   )r@   r   rB   r   r8   r   r   r   r      s
    r   c                 C   sD   | dkrt t |S t| r0t td||| S t t| ||S dS )zDecorator to wrap a function with a memoizing callable that saves
    up to `maxsize` results based on a Random Replacement (RR)
    algorithm.

    NrA   )r@   r   rB   r   )r   choicer9   r   r   r   r      s
    r   iX  c                 C   sL   | dkrt t|||S t| r6t td|||| S t t| |||S dS )zDecorator to wrap a function with a memoizing callable that saves
    up to `maxsize` results based on a Least Recently Used (LRU)
    algorithm with a per-item time-to-live (TTL) value.
    NrA   )r@   r!   rB   r   )r   r%   r&   r9   r   r   r   r      s
    r   )rA   F)rA   F)rA   F)rA   F)!__doc____all__collectionsr<   r#   randomtime	threadingr   ImportErrorZdummy_threading r
   r   r   r   r   r   r   
namedtupler3   dictr   r!   r@   r   r   r   r   rC   r   	monotonicr   r   r   r   r   <module>   s2     

	1



