U
    9a                     @   s4   d dl Z d dlmZ d dlmZ G dd deZdS )    N)time)	BaseCachec                   @   sn   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dddZdddZdd Z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.
      ,  c                 C   s*   t | | i | _| jj| _|p"d| _d S )Nr   )r   __init___cacheclear
_threshold)self	thresholdZdefault_timeout r   3/tmp/pip-unpacked-wheel-bx08cqbu/cachelib/simple.pyr      s    
zSimpleCache.__init__c                 C   s   t | j| jkS N)lenr   r
   )r   r   r   r   _over_threshold   s    zSimpleCache._over_thresholdc                    s4    fdd| j  D }|D ]}| j |d  qd S )Nc                    s    g | ]\}\}}| k r|qS r   r   ).0kexpires_nowr   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   _prune,   s
    
zSimpleCache._prunec                 C   s"   t | |}|dkrt | }|S Nr   )r   _normalize_timeoutr   )r   timeoutr   r   r   r'   4   s    
zSimpleCache._normalize_timeoutc              	   C   sR   z0| j | \}}|dks"|t kr.t|W S W n ttjfk
rL   Y d S X d S r&   )r   r   pickleloadsKeyErrorPickleErrorr   r"   r   valuer   r   r   get:   s    zSimpleCache.getNc                 C   s.   |  |}|   |t|tjf| j|< dS )NT)r'   r%   r)   dumpsHIGHEST_PROTOCOLr   )r   r"   r.   r(   r   r   r   r   setB   s    
zSimpleCache.setc                 C   sD   |  |}|   |t|tjf}|| jkr2dS | j|| dS )NFT)r'   r%   r)   r0   r1   r   
setdefault)r   r"   r.   r(   r   r   r   r   r   addH   s    

zSimpleCache.addc                 C   s   | j |d d k	S r   )r   r   )r   r"   r   r   r   deleteQ   s    zSimpleCache.deletec                 C   s>   z"| j | \}}|dkp |t kW S  tk
r8   Y dS X d S )Nr   F)r   r   r+   r-   r   r   r   hasT   s
    zSimpleCache.has)r   r   )N)N)__name__
__module____qualname____doc__r   r   r   r$   r%   r'   r/   r2   r4   r5   r6   r   r   r   r   r      s   
	

	r   )r)   r   Zcachelib.baser   r   r   r   r   r   <module>   s   