o
    iT                  	   @   s   d dl Z d dlZd dlmZmZ d dlmZmZ d dlmZ G dd dZ	e	 Z
dedee fd	d
Zddededee ddfddZdeddfddZdddZdddZdS )    N)AnyOptional)datetime	timedelta)loggerc                	   @   sp   e Zd Zdd Zdedee fddZddeded	ee ddfd
dZ	deddfddZ
dddZdddZdS )Cachec                 C   s   i | _ t | _d S N)_cache	threadingLock_lockself r   :/var/www/html/status_management/src/utils/cache_manager.py__init__   s   zCache.__init__keyreturnc                 C   s   | j 8 || jvr	 W d   dS | j| }|d r0|d t k r0| j|= 	 W d   dS |d W  d   S 1 s>w   Y  dS )z
        Get a value from cache if it exists and hasn't expired
        
        Args:
            key (str): Cache key
            
        Returns:
            Optional[Any]: Cached value if exists and valid, None otherwise
        N
expires_atvalue)r   r	   time)r   r   itemr   r   r   get   s   


	$z	Cache.getNr   ttlc                 C   s`   | j # |rt | nd}||d| j|< td|  W d   dS 1 s)w   Y  dS )z
        Set a value in cache with optional TTL
        
        Args:
            key (str): Cache key
            value (Any): Value to cache
            ttl (Optional[int]): Time to live in seconds. If None, item won't expire
        N)r   r   zCached value for key: )r   r   r	   r   debug)r   r   r   r   r   r   r   r   set!   s   	"z	Cache.setc                 C   s^   | j " || jv r| j|= td|  W d   dS W d   dS 1 s(w   Y  dS )zt
        Remove a specific key from cache
        
        Args:
            key (str): Cache key to remove
        zCleared cache for key: N)r   r	   r   r   )r   r   r   r   r   clear2   s   
"zCache.clearc                 C   s@   | j  | j  td W d   dS 1 sw   Y  dS )zClear all cached itemszCleared all cacheN)r   r	   r   r   r   r   r   r   r   	clear_all>   s   
"zCache.clear_allc                    s   | j 7 t   fdd| j D }|D ]}| j|= q|r2tdt| d W d   dS W d   dS 1 s=w   Y  dS )z#Remove all expired items from cachec                    s(   g | ]\}}|d  r|d   k r|qS )r   r   ).0r   r   current_timer   r   
<listcomp>H   s    z)Cache.cleanup_expired.<locals>.<listcomp>zCleaned up z expired cache entriesN)r   r   r	   itemsr   r   len)r   expired_keysr   r   r   r   cleanup_expiredD   s   

"zCache.cleanup_expiredr   r   N)__name__
__module____qualname__r   strr   r   r   intr   r   r   r%   r   r   r   r   r      s     
r   r   r   c                 C   s
   t | S )z!Get a value from the global cache)r	   r   r   r   r   r   	get_cacheU   s   
r-   r   r   c                 C   s   t | || dS )zSet a value in the global cacheN)r	   r   )r   r   r   r   r   r   	set_cacheY   s   r.   c                 C   s   t |  dS )z*Clear a specific key from the global cacheN)r	   r   r,   r   r   r   clear_cache]   s   r/   c                   C      t   dS )z%Clear all items from the global cacheN)r	   r   r   r   r   r   clear_all_cachea      r1   c                   C   r0   )z,Clean up expired items from the global cacheN)r	   r%   r   r   r   r   cleanup_expired_cachee   r2   r3   r   r&   )r   r
   typingr   r   r   r   utils.loggerr   r   r	   r*   r-   r+   r.   r/   r1   r3   r   r   r   r   <module>   s    K 
