
    [e                        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ee	e
f                   Zer G d d	ej        e	e
f                   ZdS  G d
 d	ej                  ZdS )    )annotationsN)IteratorMutableMapping)TYPE_CHECKING)KTVTZictBaseclosediscardflushlockedc                       e Zd ZU dZded<   ded<   ded<   ded<   d	ed
<   	 dd fdZedd            Zed d            Zed!d            Z	d"dZ
d#dZd$dZd%dZd%dZ x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
    MutableMapping[KT, VT]datacacheboolupdate_on_setint_genzdict[KT, int]_last_updatedTc                    t                                                       || _        || _        || _        d| _        i | _        d S )Nr   )super__init__r   r   r   r   r   )selfr   r   r   	__class__s       *lib/python3.11/site-packages/zict/cache.pyr   zCache.__init__0   sF     		
*	    keyr   returnr   c                H   	 | j         |         S # t          $ r Y nw xY w| j        |         }|                                 5  | j        |         }d d d            n# 1 swxY w Y   || j                            |          k    r|| j         |<   | j        |xx         dz  cc<   |S N   )r   KeyErrorr   unlockr   get)r   r   genvalues       r   __getitem__zCache.__getitem__=   s    	:c?" 	 	 	D	 %[[]] 	# 	#IcNE	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#
 $$((----#DJsOs###q(###s    
 AA!Ar(   Nonec                   t          | j        |           | j        }|dz  }|x| j        |<   | _        |                                 5  || j        |<   d d d            n# 1 swxY w Y   || j        vrt          | j        |           d S || j        |         k    r,| j        |xx         dz  cc<   t          | j        |           d S | j        |xx         dz  cc<   | j        r|| j        |<   d S d S r"   )r   r   r   r   r%   r   r   )r   r   r(   r'   s       r   __setitem__zCache.__setitem__O   sc    	
C   iq.113$)[[]] 	# 	#"DIcN	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# d(((DIs#####D&s+++
 s###q(### DJ$$$$$ s###q(###! ("'
3( (s   AA"%A"c                P    | j         |= | j        |= t          | j        |           d S N)r   r   r   r   r   r   s     r   __delitem__zCache.__delitem__m   s.    IcNs#
C     r   c                *    t          | j                  S r.   )lenr   r   s    r   __len__zCache.__len__s   s    49~~r   Iterator[KT]c                *    t          | j                  S r.   )iterr   r3   s    r   __iter__zCache.__iter__v   s    DIr   objectc                    || j         v S r.   )r   r/   s     r   __contains__zCache.__contains__y   s    dir   c                :    t          | j        | j                   d S r.   )r   r   r   r3   s    r   r   zCache.flush}       dj$)$$$$$r   c                :    t          | j        | j                   d S r.   )r
   r   r   r3   s    r   r
   zCache.close   r=   r   )T)r   r   r   r   r   r   )r   r   r    r   r   r   r(   r   r    r*   )r   r   r    r*   )r    r   )r    r5   )r   r9   r    r   )r    r*   )__name__
__module____qualname____doc____annotations__r   r   r)   r,   r0   r4   r8   r;   r   r
   __classcell__r   s   @r   r   r   
   sR         > !   !!!!III     #	                 V" ( ( ( V(: ! ! ! V!
             % % % %% % % % % % % %r   r   c                      e Zd ZdS )WeakValueMappingN)r@   rA   rB    r   r   rH   rH      s        r   rH   c                  $     e Zd ZdZd	 fdZ xZS )
rH   zVariant of weakref.WeakValueDictionary which silently ignores objects that
        can't be referenced by a weakref.ref
        r   r   r(   r   r    r*   c                p    	 t                                          ||           d S # t          $ r Y d S w xY wr.   )r   r,   	TypeError)r   r   r(   r   s      r   r,   zWeakValueMapping.__setitem__   sJ    ##C/////   s   "' 
55r?   )r@   rA   rB   rC   r,   rE   rF   s   @r   rH   rH      sG        	 		 	 	 	 	 	 	 	 	 	r   )
__future__r   weakrefcollections.abcr   r   typingr   zict.commonr   r   r	   r
   r   r   r   r   WeakValueDictionaryrH   rI   r   r   <module>rS      s=   " " " " " "  4 4 4 4 4 4 4 4             G G G G G G G G G G G G G G G G G Gw% w% w% w% w%HRV w% w% w%t      76r2v>     
	 	 	 	 	76 	 	 	 	 	r   