
    yjX                    &   d dl mZ d dlZd dlZd dlmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ d dlmZ  ej        e          Z ej                    ZddZddZddZej        ej         ej        efZ!ddZ"ddZ#ddZ$g dZ%dS )    )annotationsN)Anycast)cache)PicklerType)
exceptions)AsyncRetryingretry_if_exception_typestop_after_attemptstop_after_delaywait_exponential_jitter)settingsreturnboolc                 $    t           j        j        S N)r   CACHEENABLED     8/DATA/AppData/hermes/projects/honcho/src/cache/client.pyis_cache_enabledr      s    >!!r   strc                 J    t          t          t          j        j                  S r   )r   r   r   r   	NAMESPACEr   r   r   get_cache_namespacer      s    X^-...r   Nonec                   K   t           4 d{V  t          j                     d{V  t                      sNt                              d           t          j        dt          j                   	 ddd          d{V  dS 	 t          j        t          j
        j        t          j                   n# t          $ r} t                              dt          j
        j        |            t          j        j        rt!          j        |            t          j        dt          j                   Y d} ~ ddd          d{V  dS d} ~ ww xY wt          j                     	 t'          t)          dd          t+          d	          t-          t.          j        t.          j        t4          j        t0          f          d
          2 3 d{V }|5  t5          j        d          4 d{V  t          j                     d{V  t                              dt          j
        j                   ddd          d{V  n# 1 d{V swxY w Y   ddd           n# 1 swxY w Y   6 n]# t.          j        t.          j        t4          j        t0          f$ r}t                              dt          j
        j        |           t          j        j        rt!          j        |           t          j                     d{V  t          j        dt          j                   Y d}~nd}~wt          $ r}t                              dt          j
        j        |           t          j        j        rt!          j        |           t          j                     d{V  t          j        dt          j                   Y d}~nd}~ww xY wddd          d{V  dS # 1 d{V swxY w Y   dS )z2Initialize and verify cache connection if enabled.Nz%Cache disabled, using in-memory cachezmem://)pickle_typez>Cache setup failed for %s: %s. Falling back to in-memory cacheg?g       @initialmax   T)waitstopretryreraise   zConnected to cache at %szEFailed to connect to cache at %s: %s. Falling back to in-memory cachezAUnexpected cache error at %s: %s. Falling back to in-memory cache)_cache_lockr   closer   loggerinfosetupr   
SQLALCHEMYr   r   URL	ExceptionwarningSENTRYr   
sentry_sdkcapture_exceptionenabler	   r   r   r
   	redis_excTimeoutErrorConnectionErrorasynciotimeoutping)	setup_errattemptes      r   
init_cacher?   $   s      KF KF KF KF KF KF KF KFkmm!! 	KK?@@@Kk.DEEEEKF KF KF KF KF KF KF KF KF KF KF KF KF KF	K"'2    
  
	 
	 
	NNP"  
 & 8,Y777Kk.DEEEEFFF7KF KF KF KF KF KF KF KF KF KF KF KF KF KF"
	 	,	F!.,ScBBB%a((-!.!1,$	  " " " T T T T T T Tg  T T&q11 T T T T T T T T#jll*******$>@RSSST T T T T T T T T T T T T T T T T T T T T T T T T T TT T T T T T T T T T T T T T T" "$ "% 	
 	F 	F 	F NNW"  
 & 0,Q///+--Kk.DEEEEEEEEE 
	F 
	F 
	FNNS"  
 & 0,Q///+--Kk.DEEEEEEEEE
	FCKF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KF KFs   A#O/B76O7
EA0E1OEO$A#I-I*I-I+AI	/I
IIIII-I""I-%I"&I-+O-,OB	L'"O'O4B	O=OOO
O'*O'keyvaluer   expireint | floatc                j  K   	 t          t          d          t          dd          t          t                    d          2 3 d{V }|5  t          j        | ||           d{V  ddd           n# 1 swxY w Y   ?6 dS # t          $ r! t          	                    d	| d
           Y dS w xY w)z`Best-effort cache set with retries on transient errors. Failures are logged but never propagate.   皙?      ?r    Tr%   r$   r&   r'   N)rB   zCache set failed for key %sexc_info)
r	   r   r   r
   _TRANSIENT_CACHE_ERRORSr   setr0   r+   r1   )r@   rA   rB   r=   s       r   safe_cache_setrM   |   sF     
J*#A&&(#>>>)*ABB	
 
 
 	; 	; 	; 	; 	; 	; 	;'  ; ;iU6::::::::::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
 
 
  J J J4cDIIIIIIJsB   AB BB A8,B 8A<	<B ?A<	 B 'B21B2c                d  K   	 t          t          d          t          dd          t          t                    d          2 3 d{V }|5  t          j        |            d{V  ddd           n# 1 swxY w Y   <6 dS # t          $ r! t          	                    d| d	           Y dS w xY w)
zcBest-effort cache delete with retries on transient errors. Failures are logged but never propagate.rE   rF   rG   r    TrH   NzCache delete failed for key %srI   )
r	   r   r   r
   rK   r   deleter0   r+   r1   )r@   r=   s     r   safe_cache_deleterP      s?     
M*#A&&(#>>>)*ABB	
 
 
 	( 	( 	( 	( 	( 	( 	('  ( (l3'''''''''( ( ( ( ( ( ( ( ( ( ( ( ( ( (
 
 
  M M M7tLLLLLLMsB   AB BB A5)B 5A9	9B <A9	=B 'B/.B/c                 <   K   t          j                     d {V  d S r   )r   r*   r   r   r   close_cacherR      s*      
+--r   )r?   rR   r   rP   rM   )r   r   )r   r   )r   r   )r@   r   rA   r   rB   rC   r   r   )r@   r   r   r   )&
__future__r   r9   loggingtypingr   r   r3   cashewsr   cashews.picklersr   redisr   r6   tenacityr	   r
   r   r   r   
src.configr   	getLogger__name__r+   Lockr)   r   r   r?   r7   r8   rK   rM   rP   rR   __all__r   r   r   <module>r_      s   " " " " " "                     ( ( ( ( ( ( ) ) ) ) ) )                    		8	$	$ glnn" " " "/ / / /
MF MF MF MFb 	 J J J JM M M M     r   