o
    2'h^  ã                   @   s@   d dl Zd dlmZ d dlmZ d dlmZ G dd„ deƒZdS )é    N)Útime)Ú	BaseCache)ÚSimpleSerializerc                
   @   s  e Zd ZdZeƒ Z		d$dedefdd„Zdefd	d
„Z	de
ddfdd„Zd%dd„Zd%dd„Zdeje defdd„Zdedejfdd„Z	d&dedejdeje deje fdd„Zd&dedejdeje defdd„Zdedefdd„Zdedefd d!„Zdefd"d#„ZdS )'ÚSimpleCachea�  Simple memory cache for single process environments.  This class exists
    mainly for the development server and is not 100% thread safe.  It tries
    to use as many atomic operations as possible and no locks for simplicity
    but it could happen under heavy load that keys are added multiple times.

    :param threshold: the maximum number of items the cache stores before
                      it starts deleting some.
    :param default_timeout: the default timeout that is used if no timeout is
                            specified on :meth:`~BaseCache.set`. A timeout of
                            0 indicates that the cache never expires.
    éô  é,  Ú	thresholdÚdefault_timeoutc                 C   s    t  | |¡ i | _|pd| _d S )Nr   )r   Ú__init__Ú_cacheÚ
_threshold)Úselfr   r	   © r   úEC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/simple.pyr
      s   zSimpleCache.__init__Úreturnc                 C   s   t | jƒ| jkS ©N)Úlenr   r   ©r   r   r   r   Ú_over_threshold    s   zSimpleCache._over_thresholdÚnowNc                    s4   ‡ fdd„| j  ¡ D ƒ}|D ]	}| j  |d ¡ qd S )Nc                    s    g | ]\}\}}|ˆ k r|‘qS r   r   )Ú.0ÚkÚexpiresÚ_©r   r   r   Ú
<listcomp>$   s     z/SimpleCache._remove_expired.<locals>.<listcomp>)r   ÚitemsÚpop)r   r   Ztoremover   r   r   r   Ú_remove_expired#   s   ÿzSimpleCache._remove_expiredc                 C   sJ   dd„ t | j ¡ dd„ d�D ƒ}|D ]}| j |d ¡ |  ¡ s" d S qd S )Nc                 s   s   � | ]\}}|V  qd S r   r   )r   r   Úvr   r   r   Ú	<genexpr>)   s   € 
ÿz,SimpleCache._remove_older.<locals>.<genexpr>c                 S   s   | d d S )Né   r   r   )Úitemr   r   r   Ú<lambda>*   s    z+SimpleCache._remove_older.<locals>.<lambda>)Úkey)Úsortedr   r   r   r   )r   Z	k_orderedr   r   r   r   Ú_remove_older(   s   ÿÿþzSimpleCache._remove_olderc                 C   s0   |   ¡ rtƒ }|  |¡ |   ¡ r|  ¡  d S d S r   )r   r   r   r&   )r   r   r   r   r   Ú_prune1   s   
ÿzSimpleCache._pruneÚtimeoutc                 C   s&   t  | |¡}|dkrttƒ ƒ| }|S ©Nr   )r   Ú_normalize_timeoutÚintr   )r   r(   r   r   r   r*   9   s   zSimpleCache._normalize_timeoutr$   c                 C   sJ   z| j | \}}|dks|tƒ kr| j |¡W S W d S  ty$   Y d S w r)   )r   r   Ú
serializerÚloadsÚKeyError©r   r$   r   Úvaluer   r   r   Úget?   s   ÿÿzSimpleCache.getr0   c                 C   s,   |   |¡}|  ¡  || j |¡f| j|< dS )NT)r*   r'   r,   Údumpsr   )r   r$   r0   r(   r   r   r   r   ÚsetG   s   
zSimpleCache.setc                 C   sB   |   |¡}|  ¡  || j |¡f}|| jv rdS | j ||¡ dS )NFT)r*   r'   r,   r2   r   Ú
setdefault)r   r$   r0   r(   r   r"   r   r   r   ÚaddO   s   

zSimpleCache.addc                 C   s   | j  |d ¡d uS r   )r   r   )r   r$   r   r   r   ÚdeleteX   s   zSimpleCache.deletec                 C   s<   z| j | \}}t|dkp|tƒ kƒW S  ty   Y dS w )Nr   F)r   Úboolr   r.   r/   r   r   r   Úhas[   s   ÿzSimpleCache.hasc                 C   s   | j  ¡  t| j ƒ S r   )r   Úclearr7   r   r   r   r   r9   b   s   
zSimpleCache.clear)r   r   )r   Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   r+   r
   r7   r   Úfloatr   r&   r'   Ú_tÚOptionalr*   ÚstrÚAnyr1   r3   r5   r6   r8   r9   r   r   r   r   r      s<    ýþ
ý	

		ÿÿÿÿ
þ$	r   )Útypingr?   r   Zcachelib.baser   Zcachelib.serializersr   r   r   r   r   r   Ú<module>   s
    