
    ,[g:                         d Z ddlmZ ddlmZ ddlZddlZddlZ e       ZdZ	 G d de      Z
 G d d	e
      Z G d
 de
      Z G d de
      Z G d de      Z G d de      Zy)z! LRU caching class and decorator     )abstractmethod)ABCMetaNl            c                   R    e Zd ZeZed        Zedd       Zed        Zed        Z	y)Cachec                      y)!Remove all entries from the cacheN selfs    N/var/www/html/bid-api/venv/lib/python3.12/site-packages/repoze/lru/__init__.pyclearzCache.clear           Nc                      y)5Return value for key. If not in cache, return defaultNr	   r   keydefaults      r   getz	Cache.get   r   r   c                      y)#Add key to the cache with value valNr	   r   r   vals      r   putz	Cache.put   r   r   c                      y)Remove key from the cacheNr	   r   r   s     r   
invalidatezCache.invalidate   r   r   N)
__name__
__module____qualname__r   __metaclass__r   r   r   r   r   r	   r   r   r   r      sW    M0 0 D D 2 2 ( (r   r   c                   0    e Zd ZdZd ZddZd Zd Zd Zy)	UnboundedCachez9
    a simple unbounded cache backed by a dictionary
    c                 "    t               | _        y r   )dict_datar
   s    r   __init__zUnboundedCache.__init__)   s    V
r   Nc                 :    | j                   j                  ||      S r   )r(   r   r   s      r   r   zUnboundedCache.get,   s    zz~~c7++r   c                 8    | j                   j                          y r   )r(   r   r
   s    r   r   zUnboundedCache.clear/   s    

r   c                 >    	 | j                   |= y # t        $ r Y y w xY wr   )r(   KeyErrorr   s     r   r   zUnboundedCache.invalidate2   s$    	

3 		s    	c                 "    || j                   |<   y r   )r(   r   s      r   r   zUnboundedCache.put8   s    

3r   r   )	r    r!   r"   __doc__r)   r   r   r   r   r	   r   r   r%   r%   $   s     ,r   r%   c                   0    e Zd ZdZd Zd ZddZd Zd Zy)	LRUCachez Implements a pseudo-LRU algorithm (CLOCK)

    The Clock algorithm is not kept strictly to improve performance, e.g. to
    allow get() and invalidate() to work without acquiring the lock.
    c                    t        |      }|dk  rt        d      || _        t        j                         | _        d| _        |dz
  | _        d | _        d | _	        d | _
        d| _        d| _        d| _        d| _        | j                          y N   zsize must be >0r   )int
ValueErrorsize	threadingLocklockhandmaxpos
clock_keys
clock_refsdata	evictionshitsmisseslookupsr   r   r7   s     r   r)   zLRUCache.__init__B   s    4y!8.//	NN$		Qh		

r   c                     | j                   5  i | _        | j                  }t        g|z  | _        dg|z  | _        d| _        d| _        d| _        d| _	        d| _
        ddd       y# 1 sw Y   yxY wr   Fr   Nr:   r?   r7   _MARKERr=   r>   r;   r@   rA   rB   rC   rD   s     r   r   zLRUCache.clearS   sc    YY DI99D&i$.DO$gnDODIDNDIDKDL YY   AA''A0Nc                     | xj                   dz  c_         	 | j                  |   \  }}| xj                  dz  c_        d| j
                  |<   |S # t        $ r | xj                  dz  c_        |cY S w xY w)r   r4   T)rC   r?   rA   r-   rB   r>   )r   r   r   posr   s        r   r   zLRUCache.getf   sl    	yy~HCIINI  $
	  	KK1KN	s   'A  A21A2c                 T   | j                   }| j                  }| j                  }| j                  }| j                  5  |j                  |      }|)|\  }}	|	|ur||f||<   d| j                  |<   	 ddd       y| j                  }
d}d}	 ||
   }|dk(  r!d||
<   |
dz  }
|
|kD  rd}
|dz  }||k\  rcd||
<   n]||
   }|j                  |t              }|t        ur| xj                  dz  c_	        |||
<   d||
<   |
|f||<   |
dz  }
|
|kD  rd}
|
| _        n	 ddd       y# 1 sw Y   yxY w)r   NTr   k   r4   F)
r<   r>   r=   r?   r:   r   r;   poprH   r@   )r   r   r   r<   r>   r=   r?   entryrK   old_valr;   count	max_countrefoldkeyoldentrys                   r   r   zLRUCache.putr   sf    __
__
yyYYHHSME   %W#%!$c
DI'+$ Y 99DEI &$;',Jt$AIDf} QJE	) ,1
4('-F  $xx8Hw.!+'*Jt$'+Jt$!%sDIAIDf}  $DI9 8 W YYs   4D:BDD'c                 z    | j                   j                  |t              }|t        urd| j                  |d   <   yyr   Fr   Nr?   rN   rH   r>   r   r   rO   s      r   r   zLRUCache.invalidate   9     		c7+ ).DOOE!H%  r   r   )	r    r!   r"   r/   r)   r   r   r   r   r	   r   r   r1   r1   <   s!    
"&
3j.r   r1   c                   6    e Zd ZdZefdZd ZddZddZd Z	y)	ExpiringLRUCachez Implements a pseudo-LRU algorithm (CLOCK) with expiration times

    The Clock algorithm is not kept strictly to improve performance, e.g. to
    allow get() and invalidate() to work without acquiring the lock.
    c                 ,   || _         t        |      }|dk  rt        d      || _        t	        j
                         | _        d| _        |dz
  | _        d | _	        d | _
        d | _        d| _        d| _        d| _        d| _        | j!                          y r3   )default_timeoutr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   r   )r   r7   r^   s      r   r)   zExpiringLRUCache.__init__   s    .4y!8.//	NN$		Qh		

r   c                     | j                   5  i | _        | j                  }t        g|z  | _        dg|z  | _        d| _        d| _        d| _        d| _	        d| _
        ddd       y# 1 sw Y   yxY wrF   rG   rD   s     r   r   zExpiringLRUCache.clear   sc    YY DI99D&i$.DO$gnDODIDNDIDKDL! YYrI   Nc                 f   | xj                   dz  c_         	 | j                  |   \  }}}|t	        j                         kD  r&| xj
                  dz  c_        d| j                  |<   |S | xj                  dz  c_        d| j                  |<   |S # t        $ r | xj                  dz  c_        |cY S w xY w)z@Return value for key. If not in cache or expired, return defaultr4   TF)rC   r?   r-   rB   timerA   r>   )r   r   r   rK   r   expiress         r   r   zExpiringLRUCache.get   s    	 $		#Cg TYY[ IINI#'DOOC J KK1K#(DOOC N  	KK1KN	s   B  B0/B0c                    | j                   }| j                  }| j                  }| j                  }| j                  }|| j
                  }| j                  5  |j                  |      }	|	1|	d   }
|
|t        j                         |z   f||<   d||
<   	 ddd       y| j                  }d}d}	 ||   }|dk(  r!d||<   |dz  }||kD  rd}|dz  }||k\  ryd||<   ns||   }|j                  |t              }|t        ur| xj                  dz  c_        |||<   d||<   ||t        j                         |z   f||<   |dz  }||kD  rd}|| _        n	 ddd       y# 1 sw Y   yxY w)zAdd key to the cache with value val

        key will expire in $timeout seconds. If key is already in cache, val
        and timeout will be updated.
        Nr   TrM   r4   F)r<   r>   r=   r?   r:   r^   r   ra   r;   rN   rH   r@   )r   r   r   timeoutr<   r>   r=   r?   r:   rO   rK   r;   rQ   rR   rS   rT   rU   s                    r   r   zExpiringLRUCache.put   s    __
__
yyyy?**GYYHHSME  Ah #tyy{W'<=S	"&
3 Y 99DEI &$;',Jt$AIDf} QJE	) ,1
4('-F  $xx8Hw.!+'*Jt$'+Jt$!%sDIIK',A BDIAIDf}  $DI9 8 U YYs   <EB0EEc                 z    | j                   j                  |t              }|t        urd| j                  |d   <   yyrW   rX   rY   s      r   r   zExpiringLRUCache.invalidate-  rZ   r   r   )
r    r!   r"   r/   _DEFAULT_TIMEOUTr)   r   r   r   r   r	   r   r   r\   r\      s&    
 .> $((9v.r   r\   c                   $    e Zd ZdZ	 	 	 ddZd Zy)	lru_cachez Decorator for LRU-cached function

    timeout parameter specifies after how many seconds a cached entry should
    be considered invalid.
    Nc                 t    |(|t               }n|t        |      }nt        ||      }|| _        || _        y )N)r^   )r%   r1   r\   cache_ignore_unhashable_args)r   maxsizerj   rd   ignore_unhashable_argss        r   r)   zlru_cache.__init__>  s@    
 =&( )('J
'=$r   c                       j                   t         fd}d } ||d        ||d        ||d       |_        |S )Nc                      	 |r| t        |j                               fn| }j                  |      }|u r | i |}j                  ||       |S # t        $ r }j
                  r | i |cY d }~S |d }~ww xY wr   )	frozensetitemsr   r   	TypeErrorrk   )	argskwargsr   r   erj   funcmarkerr   s	        r   cached_wrapperz*lru_cache.__call__.<locals>.cached_wrapperQ  s    ;AtYv||~67t iiV,&=//CIIc3'
  //000G	s#   A 	A=A80A=6A88A=c                 B    t        | ||       }|| urt        |||       y y r   )getattrsetattr)sourcetargetattrvalues       r   _maybe_copyz'lru_cache.__call__.<locals>._maybe_copy`  s)    FD&1EF"e, #r   r!   r    r/   )rj   rH   _cache)r   rv   rx   r   rj   rw   s   ``  @@r   __call__zlru_cache.__call__M  sO    

		-
 	D.,7D.*5D.)4 %r   )NNF)r    r!   r"   r/   r)   r   r	   r   r   rh   rh   8  s     (-	>r   rh   c                   B    e Zd ZdZdefdZd	dZd
dZddZd	dZ	d Z
y)
CacheMakerz3Generates decorators that can be cleared later
    Nc                 .    || _         || _        i | _        y)zCreate cache decorator factory.

        - maxsize : the default size for created caches.

        - timeout : the defaut expiraiton time for created caches.
        N)_maxsize_timeoutr   )r   rl   rd   s      r   r)   zCacheMaker.__init__o  s      r   c                     |.	 t        t        j                               }|| j                  vrn-|| j                  v rt	        d|z        || j
                  }|t        d      || j                  }|||fS )Nzcache %s already in usezCache must have a maxsize set)struuiduuid4r   r-   r   r6   r   )r   namerl   rd   s       r   _resolve_settingzCacheMaker._resolve_settingz  s    <4::<(t{{*	  4;;4t;<<?mmG?<==?mmGWg%%r   c                 x    | j                  |d      \  }}}t               x}| j                  |<   t        d |      S )Nr   )r   r%   r   rh   r   r   rl   _rj   s        r   memoizedzCacheMaker.memoized  s>    00q9gq$2$44D!u%%r   c                 z    | j                  ||      \  }}}t        |      x}| j                  |<   t        ||      S )zNamed arguments:
        
        - name (optional) is a string, and should be unique amongst all caches

        - maxsize (optional) is an int, overriding any default value set by
          the constructor
        )r   r1   r   rh   r   s        r   lrucachezCacheMaker.lrucache  sB      00w?gq$,W$55D!%((r   c                     dt         z   | j                  |||      \  }}}t        ||      x}| j                  |<   t	        |||      S )NaY  Named arguments:

        - name (optional) is a string, and should be unique amongst all caches

        - maxsize (optional) is an int, overriding any default value set by
          the constructor

        - timeout (optional) is an int, overriding any default value set by
          the constructor or the default value (%d seconds)
        )rf   r   r\   r   rh   )r   r   rl   rd   rj   s        r   expiring_lrucachezCacheMaker.expiring_lrucache  sW    		 		 "&!6!6tWg!Ngw$4Wg$FFD!%11r   c                     t        |      dk(  r| j                  j                         }|D ]  }| j                  |   j                          ! y)z_Clear the given cache(s).
        
        If no 'names' are passed, clear all caches.
        r   N)lenr   keysr   )r   namesr   s      r   r   zCacheMaker.clear  s@    
 u:?KK$$&EDKK##% r   )NNNr   )NN)r    r!   r"   r/   rf   r)   r   r   r   r   r   r	   r   r   r   r   l  s+    #-= 	&,&

)2	&r   r   )r/   abcr   r   r8   ra   r   objectrH   rf   r   r%   r1   r\   rh   r   r	   r   r   <module>r      sy    '      (  (F ((U 0r.u r.lB.u B.L1 1hM& M&r   