Format: application/x-python-code
⬇ Download File (4.3 KB)
   1  o
   2  
   3  2'h^
   4  �@s@ddlZddlmZddlmZddlmZGdd�de�ZdS)�N)�time)�	BaseCache)�SimpleSerializerc
   5  @seZdZdZe�Z		d$dedefdd�Zdefd	d
   6  �Z	de
   7  ddfd
   8  d�Zd%dd�Zd%dd�Z
   9  dejedefdd�Zdedejfdd�Z	d&dedejdejedejefdd�Zd&dedejdejedefdd�Zdedefdd�Zdedefd d!�Zdefd"d#�ZdS)'�SimpleCachea�Simple memory cache for single process environments.  This class exists
  10      mainly for the development server and is not 100% thread safe.  It tries
  11      to use as many atomic operations as possible and no locks for simplicity
  12      but it could happen under heavy load that keys are added multiple times.
  13  
  14      :param threshold: the maximum number of items the cache stores before
  15                        it starts deleting some.
  16      :param default_timeout: the default timeout that is used if no timeout is
  17                              specified on :meth:`~BaseCache.set`. A timeout of
  18                              0 indicates that the cache never expires.
  19      ���,�	threshold�default_timeoutcCs t�||�i|_|pd|_dS)Nr)r�__init__�_cache�
  20  _threshold)�selfrr	�r�EC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/simple.pyr
  21  szSimpleCache.__init__�returncCst|j�|jkS�N)�lenrr�r
  22  rrr�_over_threshold szSimpleCache._over_threshold�nowNcs4�fdd�|j��D�}|D]	}|j�|d�qdS)Ncs g|]\}\}}|�kr|�qSrr)�.0�k�expires�_�rrr�
  23  <listcomp>$s z/SimpleCache._remove_expired.<locals>.<listcomp>)r�items�pop)r
  24  rZtoremoverrrr�_remove_expired#s�zSimpleCache._remove_expiredcCsJdd�t|j��dd�d�D�}|D]}|j�|d�|��s"dSqdS)Ncss�|]\}}|VqdSrr)rr�vrrr�	<genexpr>)s�
  25  �z,SimpleCache._remove_older.<locals>.<genexpr>cSs|ddS)N�rr)�itemrrr�<lambda>*sz+SimpleCache._remove_older.<locals>.<lambda>)�key)�sortedrrrr)r
  26  Z	k_orderedrrrr�
  27  _remove_older(s���zSimpleCache._remove_oldercCs0|��rt�}|�|�|��r|��dSdSr)rrrr&)r
  28  rrrr�_prune1s
  29  �zSimpleCache._prune�timeoutcCs&t�||�}|dkrtt��|}|S�Nr)r�_normalize_timeout�intr)r
  30  r(rrrr*9szSimpleCache._normalize_timeoutr$cCsJz|j|\}}|dks|t�kr|j�|�WSWdSty$YdSwr))rr�
  31  serializer�loads�KeyError�r
  32  r$r�valuerrr�get?s��zSimpleCache.getr0cCs,|�|�}|��||j�|�f|j|<dS)NT)r*r'r,�dumpsr)r
  33  r$r0r(rrrr�setGs
  34  zSimpleCache.setcCsB|�|�}|��||j�|�f}||jvrdS|j�||�dS)NFT)r*r'r,r2r�
  35  setdefault)r
  36  r$r0r(rr"rrr�addOs
  37  
  38  zSimpleCache.addcCs|j�|d�duSr)rr)r
  39  r$rrr�deleteXszSimpleCache.deletecCs<z|j|\}}t|dkp|t�k�WStyYdSw)NrF)r�boolrr.r/rrr�has[s�zSimpleCache.hascCs|j��t|j�Sr)r�clearr7rrrrr9bs
  40  zSimpleCache.clear)rr)rNr)�__name__�
  41  __module__�__qualname__�__doc__rr,r+r
  42  r7r�floatrr&r'�_t�Optionalr*�str�Anyr1r3r5r6r8r9rrrrrs<��
  43  �	
  44  
  45  		����
  46  �$	r)�typingr?rZ
  47  cachelib.baserZcachelib.serializersrrrrrr�<module>s
  48