o
    ưi                     @   sx   d Z ddlZddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ G d	d
 d
eZdS )z
S3 Cache implementation

Has 4 methods:
    - set_cache
    - get_cache
    - async_set_cache (uses run_in_executor)
    - async_get_cache (uses run_in_executor)
    N)partial)Optional)datetimetimezone	timedelta)print_verboseverbose_logger   )	BaseCachec                   @   s~   e Zd Z										ddee fddZdedefdd	Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )S3CacheNT
s3_use_sslc                 K   sR   dd l }|| _|r|dd nd| _|j	d||||||||	|
d	|| _d S )Nr   / s3)	Zregion_nameZendpoint_urlapi_versionZuse_sslverifyZaws_access_key_idZaws_secret_access_keyZaws_session_tokenconfig)r   )boto3bucket_namerstrip
key_prefixclient	s3_client)selfZs3_bucket_nameZs3_region_nameZs3_api_versionr   Z	s3_verifyZs3_endpoint_urlZs3_aws_access_key_idZs3_aws_secret_access_keyZs3_aws_session_tokenZ	s3_configZs3_pathkwargsr    r   O/home/app/Keep/.python/lib/python3.10/site-packages/litellm/caching/s3_cache.py__init__   s$   zS3Cache.__init__keyreturnc                 C   s   | j |dd S )zConvert cache key to S3 key:r   )r   replace)r   r   r   r   r   
_to_s3_key;   s   zS3Cache._to_s3_keyc           	      K   s   z^t d| d|  |dd }t|}| |}|d urHd| d| }ttjt	|d }| j
j| j||||ddd	| d
d W d S d}| j
j| j|||ddd	| d
d W d S  tyx } zt d|  W Y d }~d S d }~ww )NzLiteLLM SET Cache - S3. Key=. Value=ttlzimmutable, max-age=z, s-maxage=)secondszapplication/jsonenzinline; filename="z.json")BucketKeyBodyExpiresCacheControlContentTypeContentLanguageContentDispositionz.immutable, max-age=31536000, s-maxage=31536000)r'   r(   r)   r+   r,   r-   r.   z1S3 Caching: set_cache() - Got exception from S3: )r   getjsondumpsr"   r   nowr   utcr   r   Z
put_objectr   	Exception)	r   r   valuer   r$   Zserialized_valueZcache_controlZexpiration_timeer   r   r   	set_cache?   s@   



	zS3Cache.set_cachec              
      s   z&t d| d|  t }t| j||fi |}|d|I dH  W dS  tyB } zt d|  W Y d}~dS d}~ww )z
        Asynchronously set cache using run_in_executor to avoid blocking the event loop.
        Compatible with Python 3.8+.
        zSet ASYNC S3 Cache: Key=r#   Nz7S3 Caching: async_set_cache() - Got exception from S3: )	r   debugasyncioget_event_loopr   r7   run_in_executorr4   error)r   r   r5   r   loopfuncr6   r   r   r   async_set_cachef   s   zS3Cache.async_set_cachec              
   K   sl  dd l }zi| |}td|  | jj| j|d}|d urRd|v r3|d }t|j}||kr3W d S |d 	 
d}zt|}W n tyQ   t|}Y nw t|ts[t|}td| d| d	t|  |W S  |jjy } z |jd
 d dkrtd| d W Y d }~d S W Y d }~d S d }~w ty } ztd|  W Y d }~d S d }~ww )Nr   zGet S3 Cache: key: )r'   r(   r*   r)   zutf-8zGot S3 Cache: key: z, cached_response z. Type Response ErrorZCodeZ	NoSuchKeyzS3 Cache: The specified key 'z"' does not exist in the S3 bucket.z1S3 Caching: get_cache() - Got exception from S3: )botocorer"   r   r   Z
get_objectr   r   r2   tzinforeaddecoder0   loadsr4   astliteral_eval
isinstancedictr   r8   type
exceptionsZClientErrorresponser<   )r   r   r   rA   cached_responseexpires_timecurrent_timer6   r   r   r   	get_caches   sT   


zS3Cache.get_cachec              
      s~   z"t d|  t }t| j|fi |}|d|I dH }|W S  ty> } zt d|  W Y d}~dS d}~ww )z
        Asynchronously get cache using run_in_executor to avoid blocking the event loop.
        Compatible with Python 3.8+.
        zGet ASYNC S3 Cache: key: Nz7S3 Caching: async_get_cache() - Got exception from S3: )	r   r8   r9   r:   r   rP   r;   r4   r<   )r   r   r   r=   r>   resultr6   r   r   r   async_get_cache   s   zS3Cache.async_get_cachec                 C   s   d S Nr   r   r   r   r   flush_cache   s   zS3Cache.flush_cachec                    s   d S rS   r   rT   r   r   r   
disconnect   s   zS3Cache.disconnectc                    sF   g }|D ]}| | j|d |d fi | qtj| I d H  d S )Nr   r	   )appendr?   r9   gather)r   Z
cache_listr   tasksvalr   r   r   async_set_cache_pipeline   s
   $z S3Cache.async_set_cache_pipeline)
NNTNNNNNNN)__name__
__module____qualname__r   boolr   strr"   r7   r?   rP   rR   rU   rV   r[   r   r   r   r   r      s,    
#'1r   )__doc__rF   r9   r0   	functoolsr   typingr   r   r   r   Zlitellm._loggingr   r   Z
base_cacher
   r   r   r   r   r   <module>   s    
