3
ãde™  ã               @   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#eedœdd„Zedœdd	„Z	e
d
dœdd„Zd
dœdd„Zd
dœdd„Zeje edœdd„Zeejdœdd„Zd$eejeje eje dœdd„Zd%eejeje edœdd„Zeedœdd„Zeedœdd „Zedœ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 j| |ƒ i | _|pd| _d S )Niô  )r   Ú__init__Ú_cacheÚ
_threshold)Úselfr   r	   © r   úF/var/www/agendate/envp3/lib/python3.6/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_thresholdN)Únowr   c                s8   ‡ fdd„| j jƒ D ƒ}x|D ]}| j j|d ƒ qW 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jƒ dd„ d�D ƒ}x$|D ]}| jj|d ƒ | jƒ s&P q&W d S )Nc             s   s   | ]\}}|V  qd S )Nr   )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   s,   | j ƒ rtƒ }| j|ƒ | j ƒ r(| jƒ  d S )N)r   r   r   r#   )r   r   r   r   r   Ú_prune5   s
    
zSimpleCache._prune)Útimeoutr   c             C   s&   t j| |ƒ}|dkr"ttƒ ƒ| }|S )Nr   )r   Ú_normalize_timeoutÚintr   )r   r%   r   r   r   r&   =   s    zSimpleCache._normalize_timeout)r!   r   c             C   sJ   y0| j | \}}|dks"|tƒ kr.| jj|ƒS W n tk
rD   d S X d S )Nr   )r   r   Ú
serializerÚloadsÚKeyError)r   r!   r   Úvaluer   r   r   ÚgetC   s    zSimpleCache.get)r!   r+   r%   r   c             C   s,   | j |ƒ}| jƒ  || jj|ƒf| j|< dS )NT)r&   r$   r(   Údumpsr   )r   r!   r+   r%   r   r   r   r   ÚsetK   s    
zSimpleCache.setc             C   sB   | j |ƒ}| jƒ  || jj|ƒf}|| jkr0dS | jj||ƒ dS )NFT)r&   r$   r(   r-   r   Ú
setdefault)r   r!   r+   r%   r   r   r   r   r   ÚaddS   s    

zSimpleCache.addc             C   s   | j j|d ƒd k	S )N)r   r   )r   r!   r   r   r   Údelete\   s    zSimpleCache.deletec             C   s>   y$| j | \}}t|dkp"|tƒ kƒS  tk
r8   dS X d S )Nr   F)r   Úboolr   r*   )r   r!   r   r+   r   r   r   Úhas_   s
    zSimpleCache.hasc             C   s   | j jƒ  t| j ƒ S )N)r   Úclearr2   )r   r   r   r   r4   f   s    
zSimpleCache.clear)r   r   )N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r'   r
   r2   r   Úfloatr   r#   r$   Ú_tÚOptionalr&   ÚstrÚAnyr,   r.   r0   r1   r3   r4   r   r   r   r   r      s     		r   )Útypingr:   r   Zcachelib.baser   Zcachelib.serializersr   r   r   r   r   r   Ú<module>   s   