
    Be                        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y G d
 d	ej"                        Zy)    )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_updatedc                h    t         |           || _        || _        || _        d| _        i | _        y )Nr   )super__init__r   r   r   r   r   )selfr   r   r   	__class__s       *lib/python3.12/site-packages/zict/cache.pyr   zCache.__init__0   s5     		
*	    c                T   	 | j                   |   S # t        $ r Y nw xY w| j                  |   }| j                         5  | j                  |   }d d d        n# 1 sw Y   nxY w|| j                  j                  |      k(  r&| j                   |<   | j                  |xx   dz  cc<   S N   )r   KeyErrorr   unlockr   get)r   keygenvalues       r   __getitem__zCache.__getitem__=   s    	::c?" 		  %[[] 	#IIcNE	# 	# 	#
 $$$((--#DJJsOs#q(#s    	AA!c                   t        | j                  |       | j                  }|dz  }|x| j                  |<   | _        | j	                         5  || j
                  |<   d d d        || j                  vrt        | j
                  |       y || j                  |   k7  r.| j                  |xx   dz  cc<   t        | j                  |       y | j                  |xx   dz  cc<   | j                  r|| j                  |<   y y # 1 sw Y   xY wr    )r   r   r   r   r#   r   r   )r   r%   r'   r&   s       r   __setitem__zCache.__setitem__O   s     	

C iiq.113$)[[] 	#"DIIcN	# d(((DIIs#D&&s++
 s#q(# DJJ$ s#q(#!!"'

3 "%	# 	#s   C??Dc                d    | j                   |= | j                  |= t        | j                  |       y N)r   r   r   r   r   r%   s     r   __delitem__zCache.__delitem__m   s)    IIcNs#

C r   c                ,    t        | j                        S r,   )lenr   r   s    r   __len__zCache.__len__s   s    499~r   c                ,    t        | j                        S r,   )iterr   r1   s    r   __iter__zCache.__iter__v   s    DIIr   c                    || j                   v S r,   )r   r-   s     r   __contains__zCache.__contains__y   s    diir   c                D    t        | j                  | j                         y r,   )r   r   r   r1   s    r   r   zCache.flush}       djj$))$r   c                D    t        | j                  | j                         y r,   )r
   r   r   r1   s    r   r
   zCache.close   r9   r   )T)r   r   r   r   r   r   )r%   r   returnr   r%   r   r'   r   r;   None)r%   r   r;   r=   )r;   r   )r;   zIterator[KT])r%   objectr;   r   )r;   r=   )__name__
__module____qualname____doc____annotations__r   r   r(   r*   r.   r2   r5   r7   r   r
   __classcell__r   s   @r   r   r   
   s    > ! !!
I   #	 $  &  	   " ( (: ! !
 %%r   r   c                      e Zd Zy)WeakValueMappingN)r?   r@   rA    r   r   rG   rG      s    r   rG   c                  $     e Zd ZdZd fdZ xZS )rG   zVariant of weakref.WeakValueDictionary which silently ignores objects that
        can't be referenced by a weakref.ref
        c                F    	 t         |   ||       y # t        $ r Y y w xY wr,   )r   r*   	TypeError)r   r%   r'   r   s      r   r*   zWeakValueMapping.__setitem__   s)    #C/ s    	  r<   )r?   r@   rA   rB   r*   rD   rE   s   @r   rG   rG      s    		 	r   )
__future__r   weakrefcollections.abcr   r   typingr   zict.commonr   r   r	   r
   r   r   r   r   WeakValueDictionaryrG   rH   r   r   <module>rR      sd    "  4   G G Gw%HRV w%t 766r2v> 
	766 	r   