o
    ưi                     @   sP   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 G dd de	Z
dS )	z}
Azure Blob Cache implementation

Has 4 methods:
    - set_cache
    - get_cache
    - async_set_cache
    - async_get_cache
    N)suppress)print_verboseverbose_logger   )	BaseCachec                   @   sX   e Zd ZdddZdddZdddZd	d
 Zdd ZdddZdddZ	dddZ
dS )AzureBlobCachereturnNc                 C   s   ddl m} ddlm} ddlm} ddlm} ddlm} ||| d|| _	||| d|| _
t| | j	  W d    d S 1 sIw   Y  d S )Nr   )BlobServiceClient)ResourceExistsError)DefaultAzureCredential)account_urlZ
credential)Zazure.storage.blobr	   azure.core.exceptionsr
   Zazure.identityr   Zazure.identity.aioZazure.storage.blob.aioZget_container_clientcontainer_clientasync_container_clientr   Zcreate_container)selfr   	containerr	   r
   r   ZAsyncDefaultAzureCredentialZAsyncBlobServiceClient r   W/home/app/Keep/.python/lib/python3.10/site-packages/litellm/caching/azure_blob_cache.py__init__   s(   
"zAzureBlobCache.__init__c              
   K   sh   t d| d|  t|}z
| j|| W d S  ty3 } zt d|  W Y d }~d S d }~ww )N$LiteLLM SET Cache - Azure Blob. Key=. Value=5LiteLLM set_cache() - Got exception from Azure Blob: )r   jsondumpsr   upload_blob	Exceptionr   keyvaluekwargsZserialized_valueer   r   r   	set_cache(   s   
zAzureBlobCache.set_cachec              
      st   t d| d|  t|}z| jj||ddI d H  W d S  ty9 } zt d|  W Y d }~d S d }~ww )Nr   r   T)	overwriter   )r   r   r   r   r   r   r   r   r   r   async_set_cache1   s   
zAzureBlobCache.async_set_cachec              	   K   sz   ddl m} z,td|  | j| }|d}t|}t	
d| d| dt|  |W S  |y<   Y d S w Nr   )ResourceNotFoundErrorzGet Azure Blob Cache: key: zutf-8zGot Azure Blob Cache: key: z, cached_response z. Type Response )r   r%   r   r   download_blobreadalldecoder   loadsr   debugtype)r   r   r   r%   as_bytesas_strcached_responser   r   r   	get_cache:   s   

zAzureBlobCache.get_cachec              	      s   ddl m} z4td|  | j|I d H }| I d H }|d}t|}t	
d| d| dt|  |W S  |yE   Y d S w r$   )r   r%   r   r   r&   r'   r(   r   r)   r   r*   r+   )r   r   r   r%   blobr,   r-   r.   r   r   r   async_get_cacheK   s   

zAzureBlobCache.async_get_cachec                 C   s"   | j  D ]	}| j |j qd S N)r   Z
walk_blobsZdelete_blobname)r   r0   r   r   r   flush_cache[   s   zAzureBlobCache.flush_cachec                    s    | j   | j I d H  d S r2   )r   closer   )r   r   r   r   
disconnect_   s   
zAzureBlobCache.disconnectc                    sF   g }|D ]}| | j|d |d fi | qtj| I d H  d S )Nr   r   )appendr#   asynciogather)r   Z
cache_listr   tasksvalr   r   r   async_set_cache_pipelinec   s
   $z'AzureBlobCache.async_set_cache_pipeline)r   N)__name__
__module____qualname__r   r!   r#   r/   r1   r4   r6   r<   r   r   r   r   r      s    


		

r   )__doc__r8   r   
contextlibr   Zlitellm._loggingr   r   Z
base_cacher   r   r   r   r   r   <module>   s    
