
    ^hR                     J    S r SSKrSSKrSSKJrJr  SSKJr   " S S\5      rg)z|
In-Memory Cache implementation

Has 4 methods:
    - set_cache
    - get_cache
    - async_set_cache
    - async_get_cache
    N)ListOptional   )	BaseCachec                       \ rS rSr  SS\\   S\\   4S jjrS rS rS r	SS	 jr
S
\S\\   4S jrS rS\4S jrS
\S\4S jrS rS\4S jrS
\S\4S jrS rS rS rS\S\\   4S jrSrg)InMemoryCache   max_size_in_memorydefault_ttlc                 `    U=(       d    SU l         U=(       d    SU l        0 U l        0 U l        g)zv
max_size_in_memory [int]: Maximum number of items in cache. done to prevent memory leaks. Use 200 items as a default
   X  N)r
   r   
cache_dictttl_dict)selfr
   r   s      Y/home/james-whalen/.local/lib/python3.13/site-packages/litellm/caching/in_memory_cache.py__init__InMemoryCache.__init__   s3     %# 	 '-# !#     c                    [        U R                  R                  5       5       Hc  n[        R                  " 5       U R                  U   :  d  M+  U R                  R                  US5        U R                  R                  US5        Me     g)ae  
Eviction policy:
- check if any items in ttl_dict are expired -> remove them from ttl_dict and cache_dict


This guarantees the following:
- 1. When item ttl not set: At minimumm each item will remain in memory for 5 minutes
- 2. When ttl is set: the item will remain in memory for at least that amount of time
- 3. the size of in-memory cache is bounded

N)listr   keystimer   popr   keys     r   evict_cacheInMemoryCache.evict_cache&   s_     **,-Cyy{T]]3//##C.!!#t, .r   c                 N   [        U R                  5      U R                  :  a  U R                  5         X R                  U'   SU;   a/  US   b)  [        R                  " 5       US   -   U R
                  U'   g [        R                  " 5       U R                  -   U R
                  U'   g )Nttl)lenr   r
   r   r   r   r   r   r   valuekwargss       r   	set_cacheInMemoryCache.set_cache<   s}    t4#:#::$F?ve}8!%ve}!<DMM#!%t/?/?!?DMM#r   c                 4   #    U R                   " SXS.UD6  g 7f)Nr   r#    r%   r"   s       r   async_set_cacheInMemoryCache.async_set_cacheG   s     636v6s   Nc                 j   #    U H)  u  pEUb  U R                  XEUS9  M  U R                  XES9  M+     g 7f)N)r   r#   r    r(   r*   )r   
cache_listr    r$   	cache_keycache_values         r   async_set_cache_pipeline&InMemoryCache.async_set_cache_pipelineJ   s5     &0"I9SI9@	 '1s   13r#   r    c                    #    U R                  US9=(       d
    [        5       nU H  nUR                  U5        M     U R                  XUS9  U$ 7f)z
Add value to set
r   )r    )	get_cachesetaddr%   )r   r   r#   r    
init_valuevals         r   async_set_cache_sadd"InMemoryCache.async_set_cache_saddQ   sI     
 ^^^,5
CNN3 sC0s   AAc                 <   XR                   ;   az  XR                  ;   aC  [        R                  " 5       U R                  U   :  a  U R                   R                  US 5        g U R                   U   n [        R
                  " U5      nU$ g ! [         a    Un U$ f = fN)r   r   r   r   jsonloads	Exception)r   r   r$   original_cached_responsecached_responses        r   r5   InMemoryCache.get_cache\   s    //!mm#99;s!33OO''T2'+s';$;"&**-E"F #"  ;":"";s   2B BBr   c                 f    / nU H(  nU R                   " SSU0UD6nUR                  U5        M*     U$ Nr   r)   r5   appendr   r   r$   
return_valkr9   s         r   batch_get_cacheInMemoryCache.batch_get_cachej   s=    
A..1Q1&1Cc"  r   returnc                 d    U R                  US9=(       d    SnXB-   nU R                  " X40 UD6  U$ Nr4   r   )r5   r%   r   r   r#   r$   r8   s        r   increment_cacheInMemoryCache.increment_cacheq   s5    ^^^,1
"s,V,r   c                 2   #    U R                   " SSU0UD6$ 7frE   )r5   )r   r   r$   s      r   async_get_cacheInMemoryCache.async_get_cachex   s     ~~0#000s   c                 n   #    / nU H(  nU R                   " SSU0UD6nUR                  U5        M*     U$ 7frE   rF   rH   s         r   async_batch_get_cache#InMemoryCache.async_batch_get_cache{   s@     
A..1Q1&1Cc"  s   35c                    #    U R                  US9I S h  vN =(       d    SnXB-   nU R                  " X40 UD6I S h  vN   U$  N. N7frO   )rT   r+   rP   s        r   async_incrementInMemoryCache.async_increment   sN     //C/88=A
"""38888	 98s   AA'AAAAc                 l    U R                   R                  5         U R                  R                  5         g r=   )r   clearr   r   s    r   flush_cacheInMemoryCache.flush_cache   s"    r   c                    #    g 7fr=   r)   r^   s    r   
disconnectInMemoryCache.disconnect   s     s   c                 t    U R                   R                  US 5        U R                  R                  US 5        g r=   )r   r   r   r   s     r   delete_cacheInMemoryCache.delete_cache   s*    C&#t$r   r   c                 B   #    U R                   R                  US5      $ 7f)z3
Get the remaining TTL of a key in in-memory cache
N)r   getr   s     r   async_get_ttlInMemoryCache.async_get_ttl   s      }}  d++s   )r   r   r
   r   )r   r   r=   )__name__
__module____qualname____firstlineno__r   intr   r   r%   r+   r1   r   floatr:   r5   r   rK   rQ   rT   rW   rZ   r_   rb   re   strri   __static_attributes__r)   r   r   r   r      s     -0 !$SM! 
!&-,	@7A	T 	 	D # C 1  E %,s ,x} ,r   r   )	__doc__r>   r   typingr   r   
base_cacher   r   r)   r   r   <module>rv      s&      ! !G,I G,r   