U
    Cvf2                     @  sh   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
 e
dZe
dZG dd de	eef ZdS )	    )annotationsN)OrderedDict)AnyCallableIteratorMutableMappingTypeVarKVc                   @  s   e Zd ZU dZded< ded< ded< ded	< d
Zd'dddddZdddddZdddddZddddddZ	dddddZ
dddd Zddd!d"Zeddd#d$Zejddd%d&d$ZdS )(LRUCacheal  Thread-safe LRUCache based on an OrderedDict.

    All dict operations (__getitem__, __setitem__, __contains__) update the
    priority of the relevant key and take O(1) time. The dict is iterated over
    in order from the oldest to newest key, which means that a complete pass
    over the dict should not affect the order of any entries.

    When a new item is set and the maximum size of the cache is exceeded, the
    oldest item is dropped and called with ``on_evict(key, value)``.

    The ``maxsize`` property can be used to view or adjust the capacity of
    the cache, e.g., ``cache.maxsize = new_size``.
    zOrderedDict[K, V]_cacheint_maxsizezthreading.RLock_lockzCallable[[K, V], Any] | None	_on_evict)r   r   r   r   N)maxsizeon_evictc                 C  sD   t |tstd|dk r"td|| _t | _t | _	|| _
dS )a  
        Parameters
        ----------
        maxsize : int
            Integer maximum number of items to hold in the cache.
        on_evict : callable, optional
            Function to call like ``on_evict(key, value)`` when items are
            evicted.
        zmaxsize must be an integerr   maxsize must be non-negativeN)
isinstancer   	TypeError
ValueErrorr   r   r   	threadingRLockr   r   )selfr   r    r   =/tmp/pip-unpacked-wheel-h316xyqg/xarray/backends/lru_cache.py__init__!   s    


zLRUCache.__init__r	   r
   )keyreturnc              
   C  s8   | j ( | j| }| j| |W  5 Q R  S Q R X d S N)r   r   move_to_endr   r   valuer   r   r   __getitem__4   s    
zLRUCache.__getitem__None)capacityr   c                 C  s<   t | j|kr8| jjdd\}}| jdk	r | || q dS )z9Shrink the cache if necessary, evicting the oldest items.F)lastN)lenr   popitemr   )r   r%   r   r"   r   r   r   _enforce_size_limit;   s    
zLRUCache._enforce_size_limit)r   r"   r   c              	   C  sl   | j \ || jkr&| j|= || j|< n8| jrH| | jd  || j|< n| jd k	r^| || W 5 Q R X d S )N   )r   r   r   r)   r   r!   r   r   r   __setitem__B   s    

zLRUCache.__setitem__c                 C  s   | j |= d S r   )r   )r   r   r   r   r   __delitem__P   s    zLRUCache.__delitem__zIterator[K])r   c                 C  s   t t| jS r   )iterlistr   r   r   r   r   __iter__S   s    zLRUCache.__iter__c                 C  s
   t | jS r   )r'   r   r/   r   r   r   __len__X   s    zLRUCache.__len__c                 C  s   | j S )z1Maximum number of items can be held in the cache.)r   r/   r   r   r   r   [   s    zLRUCache.maxsize)sizer   c              	   C  s6   |dk rt d| j | | || _W 5 Q R X dS )z9Resize the cache, evicting the oldest items if necessary.r   r   N)r   r   r)   r   )r   r2   r   r   r   r   `   s
    
)N)__name__
__module____qualname____doc____annotations__	__slots__r   r#   r)   r+   r,   r0   r1   propertyr   setterr   r   r   r   r      s"   
r   )
__future__r   r   collectionsr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   <module>   s   