U
    Zf                     @   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 | | i | _|pd| _d S )Nr   )r   __init___cache
_threshold)selfr   r	    r   3/tmp/pip-unpacked-wheel-7m7iy3r0/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                    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   sH   dd t | j dd dD }|D ]}| j|d  |  s$ qDq$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>-       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,   |   rt }| | |   r(|   d S r   )r   r   r   r'   )r   r   r   r   r   _prune5   s
    
zSimpleCache._prune)timeoutr   c                 C   s&   t | |}|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   sN   z2| j | \}}|dks"|t kr0| j|W S W n tk
rH   Y d S X d S r*   )r   r   
serializerloadsKeyErrorr   r%   r   valuer   r   r   getC   s    zSimpleCache.get)r%   r1   r)   r   c                 C   s,   |  |}|   || j|f| j|< dS )NT)r+   r(   r-   dumpsr   )r   r%   r1   r)   r   r   r   r   setK   s    
zSimpleCache.setc                 C   sB   |  |}|   || j|f}|| jkr0dS | j|| dS )NFT)r+   r(   r-   r3   r   
setdefault)r   r%   r1   r)   r   r"   r   r   r   addS   s    

zSimpleCache.addc                 C   s   | j |d d k	S r   )r   r   )r   r%   r   r   r   delete\   s    zSimpleCache.deletec                 C   sB   z&| j | \}}t|dkp"|t kW S  tk
r<   Y dS X d S )Nr   F)r   boolr   r/   r0   r   r   r   has_   s
    zSimpleCache.hasc                 C   s   | j   t| j  S r   )r   clearr8   r   r   r   r   r:   f   s    
zSimpleCache.clear)r   r   )N)N)__name__
__module____qualname____doc__r   r-   r,   r
   r8   r   floatr   r'   r(   _tOptionalr+   strAnyr2   r4   r6   r7   r9   r:   r   r   r   r   r      s2     		   	r   )typingr@   r   Zcachelib.baser   Zcachelib.serializersr   r   r   r   r   r   <module>   s   