
    h                     V    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e	      Z
y)	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                   H    e Zd Zd
dZd
dZd
dZd Zd Zd
dZd
dZ	d
d	Z
y)AzureBlobCacheNc                 @   ddl m} ddlm} ddlm} ddlm} ddlm}  || |             j                  |      | _	         || |             j                  |      | _
        t        |      5  | j                  j                          d d d        y # 1 sw Y   y xY w)Nr   )BlobServiceClient)ResourceExistsError)DefaultAzureCredential)account_url
credential)azure.storage.blobr   azure.core.exceptionsr   azure.identityr   azure.identity.aioazure.storage.blob.aioget_container_clientcontainer_clientasync_container_clientr   create_container)selfr   	containerr   r   r   AsyncDefaultAzureCredentialAsyncBlobServiceClients           \/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/caching/azure_blob_cache.py__init__zAzureBlobCache.__init__   s    8=9\V 1#-/!
 
y
) 	 '=#24'
 
y
) 	#
 )* 	5!!224	5 	5 	5s   0BBc                     t        d| d|        t        j                  |      }	 | j                  j	                  ||       y # t
        $ r}t        d|        Y d }~y d }~ww xY 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kwargsserialized_valuees         r   	set_cachezAzureBlobCache.set_cache(   sk    <SE%QR::e,	W!!--c3CD 	WQRSQTUVV	Ws   A 	A&A!!A&c                    K   t        d| d|        t        j                  |      }	 | j                  j	                  ||d       d {    y 7 # t
        $ r}t        d|        Y d }~y d }~ww xY ww)Nr    r!   T)	overwriter"   )r   r#   r$   r   r%   r&   r'   s         r   async_set_cachezAzureBlobCache.async_set_cache1   sz     <SE%QR::e,	W--99#?O[_9``` 	WQRSQTUVV	Ws@   'A7!A AA A7A 	A4A/*A7/A44A7c           
      2   ddl m} 	 t        d|        | j                  j	                  |      j                         }|j                  d      }t        j                  |      }t        j                  d| d| dt        |              |S # |$ r Y y w xY wNr   )ResourceNotFoundErrorzGet Azure Blob Cache: key: zutf-8zGot Azure Blob Cache: key: z, cached_response z. Type Response )r   r3   r   r   download_blobreadalldecoder#   loadsr   debugtype)r   r(   r*   r3   as_bytesas_strcached_responses          r   	get_cachezAzureBlobCache.get_cache:   s    ?	7u=>,,::3?GGIH__W-F"jj0O  -cU2D_DUUefjkzf{e|} #"$ 		s   BB BBc           
      f  K   ddl m} 	 t        d|        | j                  j	                  |       d {   }|j                          d {   }|j                  d      }t        j                  |      }t        j                  d| d| dt        |              |S 7 k7 U# |$ r Y y w xY wwr2   )r   r3   r   r   r4   r5   r6   r#   r7   r   r8   r9   )r   r(   r*   r3   blobr:   r;   r<   s           r   async_get_cachezAzureBlobCache.async_get_cacheK   s     ?	7u=>44BB3GGD!\\^+H__W-F"jj0O  -cU2D_DUUefjkzf{e|} #" H+ % 		sK   B1,B& B"B& B$AB& !B1"B& $B& &B.+B1-B..B1c                     | j                   j                         D ]'  }| j                   j                  |j                         ) y N)r   
walk_blobsdelete_blobname)r   r?   s     r   flush_cachezAzureBlobCache.flush_cache[   s3    ))446D!!--dii8 7    c                    K   | j                   j                          | j                  j                          d {    y 7 wrB   )r   closer   )r   s    r   
disconnectzAzureBlobCache.disconnect_   s0     ##%))//111s   8AA Ac                    K   g }|D ]+  }|j                   | j                  |d   |d   fi |       - t        j                  |  d {    y 7 w)Nr   r   )appendr0   asynciogather)r   
cache_listr*   tasksvals        r   async_set_cache_pipelinez'AzureBlobCache.async_set_cache_pipelinec   sP     CLL---c!fc!fGGH nne$$$s   AA
AA)returnN)__name__
__module____qualname__r   r-   r0   r=   r@   rF   rJ   rR    rG   r   r	   r	      s,    5&WW" 92%rG   r	   )__doc__rM   r#   
contextlibr   litellm._loggingr   r   
base_cacher   r	   rW   rG   r   <module>r\      s)       : !S%Y S%rG   