o
    ;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   B/var/www/html/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   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
    