U
    /e                     @  s   d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZ G dd dee	e
f ZerG dd	 d	eje	e
f ZnG d
d	 d	ejZdS )    )annotationsN)IteratorMutableMapping)TYPE_CHECKING)KTVTZictBaseclosediscardflushlockedc                      s   e Zd ZU dZded< ded< ded< ded< d	ed
< d'dddd fddZedddddZeddddddZedddddZ	ddddZ
ddddZd ddd!d"Zddd#d$Zddd%d&Z  ZS )(Cachea  Transparent write-through cache around a MutableMapping with an expensive
    __getitem__ method.

    Parameters
    ----------
    data: MutableMapping
        Persistent, slow to read mapping to be cached
    cache: MutableMapping
        Fast cache for reads from data. This mapping may lose keys on its own; e.g. it
        could be a LRU.
    update_on_set: bool, optional
        If True (default), the cache will be updated both when writing and reading.
        If False, update the cache when reading, but just invalidate it when writing.

    Notes
    -----
    If you call methods of this class from multiple threads, access will be fast as long
    as all methods of ``cache``, plus ``data.__delitem__``, are fast. Other methods of
    ``data`` are not protected by locks.

    Examples
    --------
    Keep the latest 100 accessed values in memory
    >>> from zict import Cache, File, LRU, WeakValueMapping
    >>> d = Cache(File('myfile'), LRU(100, {}))  # doctest: +SKIP

    Read data from disk every time, unless it was previously accessed and it's still in
    use somewhere else in the application
    >>> d = Cache(File('myfile'), WeakValueMapping())  # doctest: +SKIP
    zMutableMapping[KT, VT]datacacheboolupdate_on_setint_genzdict[KT, int]_last_updatedT)r   r   r   c                   s,   t    || _|| _|| _d| _i | _d S )Nr   )super__init__r   r   r   r   r   )selfr   r   r   	__class__ ./tmp/pip-unpacked-wheel-z3s6s24u/zict/cache.pyr   0   s    
zCache.__init__r   r   )keyreturnc              	   C  sz   z| j | W S  tk
r    Y nX | j| }|   | j| }W 5 Q R X || j|krv|| j |< | j|  d7  < |S N   )r   KeyErrorr   unlockr   get)r   r   genvaluer   r   r   __getitem__=   s    


zCache.__getitem__Noner   r$   r   c              	   C  s   t | j| | j}|d7 }| | j|< | _|   || j|< W 5 Q R X || jkr`t | j| nP|| j| kr| j|  d7  < t | j| n"| j|  d7  < | jr|| j|< d S r   )r
   r   r   r   r!   r   r   )r   r   r$   r#   r   r   r   __setitem__O   s    

zCache.__setitem__c                 C  s    | j |= | j|= t| j| d S N)r   r   r
   r   r   r   r   r   r   __delitem__m   s    zCache.__delitem__)r   c                 C  s
   t | jS r)   )lenr   r   r   r   r   __len__s   s    zCache.__len__zIterator[KT]c                 C  s
   t | jS r)   )iterr   r-   r   r   r   __iter__v   s    zCache.__iter__objectc                 C  s
   || j kS r)   )r   r*   r   r   r   __contains__y   s    zCache.__contains__c                 C  s   t | j| j d S r)   )r   r   r   r-   r   r   r   r   }   s    zCache.flushc                 C  s   t | j| j d S r)   )r	   r   r   r-   r   r   r   r	      s    zCache.close)T)__name__
__module____qualname____doc____annotations__r   r   r%   r(   r+   r.   r0   r2   r   r	   __classcell__r   r   r   r   r   
   s&   
 r   c                   @  s   e Zd ZdS )WeakValueMappingN)r3   r4   r5   r   r   r   r   r9      s   r9   c                      s*   e Zd ZdZdddd fddZ  ZS )r9   zVariant of weakref.WeakValueDictionary which silently ignores objects that
        can't be referenced by a weakref.ref
        r   r   r&   r'   c                   s,   zt  || W n tk
r&   Y nX d S r)   )r   r(   	TypeError)r   r   r$   r   r   r   r(      s    zWeakValueMapping.__setitem__)r3   r4   r5   r6   r(   r8   r   r   r   r   r9      s   )
__future__r   weakrefcollections.abcr   r   typingr   Zzict.commonr   r   r   r	   r
   r   r   r   WeakValueDictionaryr9   r   r   r   r   <module>   s   $z