o
    ]f                     @   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   H/home/ubuntu/webapp/venv/lib/python3.10/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   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   	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   _prune5   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,   =   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   getC   s   zSimpleCache.getr2   c                 C   s,   |  |}|   || j|f| j|< dS )NT)r,   r)   r.   dumpsr   )r   r%   r2   r*   r   r   r   r   setK   s   
zSimpleCache.setc                 C   sB   |  |}|   || j|f}|| jv rdS | j|| dS )NFT)r,   r)   r.   r4   r   
setdefault)r   r%   r2   r*   r   r#   r   r   r   addS   s   

zSimpleCache.addc                 C   s   | j |d d uS r   )r   r   )r   r%   r   r   r   delete\   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   r0   r1   r   r   r   has_   s   zSimpleCache.hasc                 C   s   | j   t| j  S r   )r   clearr9   r   r   r   r   r;   f   s   
zSimpleCache.clear)r   r   )r   Nr   )__name__
__module____qualname____doc__r   r.   r-   r
   r9   r   floatr   r(   r)   _tOptionalr,   strAnyr3   r5   r7   r8   r:   r;   r   r   r   r   r      s<    
	

	
$	r   )typingrA   r   cachelib.baser   cachelib.serializersr   r   r   r   r   r   <module>   s
    