a
    Ad4                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ ej	dej
edZdae Zdadadd Zdd Zd	d
 ZdS )    N)	data_pathscript_pathZSD_WEBUI_CACHE_FILE
cache.jsonc                  C   sZ   dd } t : t d atdu r8tjd| dat  W d   n1 sL0    Y  dS )zr
    Marks cache for writing to disk. 5 seconds after no one else flags the cache for writing, it is written.
    c               	   S   s   t d ur t t k r td q tj td } t| ddd }tjt|dd W d    n1 sb0    Y  t	
| t d a d aW d    n1 s0    Y  d S )N   -wutf8encoding   )indent)dump_cache_aftertimesleep
cache_lockcache_filenameopenjsondump
cache_dataosreplacedump_cache_thread)Zcache_filename_tmpfile r   5/var/www/html/stable-diffusion-webui/modules/cache.pythread_func   s    .zdump_cache.<locals>.thread_func   Nzcache-writer)nametarget)r   r   r   r   	threadingThreadstart)r   r   r   r   
dump_cache   s    r#   c                 C   s   t du rt t du rtjts(i a ntz<ttddd}t|a W d   n1 sX0    Y  W n6 t	y   t
ttjtdd td i a Y n0 W d   n1 s0    Y  t | i }|t | < |S )z
    Retrieves or initializes a cache for a specific subsection.

    Parameters:
        subsection (str): The subsection identifier for the cache.

    Returns:
        dict: The cache data for the specified subsection.
    Nrr   r	   tmpr   zq[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache)r   r   r   pathisfiler   r   r   load	Exceptionr   joinr   printget)
subsectionr   sr   r   r   cache1   s    ,(r/   c           	      C   sx   t | }tj|}||}|r:|dd}||kr:d}|rFd|vrp| }|du rXdS ||d}|||< t  |d S )aP  
    Retrieves or generates data for a specific file, using a caching mechanism.

    Parameters:
        subsection (str): The subsection of the cache to use.
        title (str): The title of the data entry in the subsection of the cache.
        filename (str): The path to the file to be checked for modifications.
        func (callable): A function that generates the data if it is not available in the cache.

    Returns:
        dict or None: The cached or generated data, or None if data generation fails.

    The `cached_data_for_file` function implements a caching mechanism for data stored in files.
    It checks if the data associated with the given `title` is present in the cache and compares the
    modification time of the file with the cached modification time. If the file has been modified,
    the cache is considered invalid and the data is regenerated using the provided `func`.
    Otherwise, the cached data is returned.

    If the data generation fails, None is returned to indicate the failure. Otherwise, the generated
    or cached data is returned as a dictionary.
    mtimer   Nvalue)r0   r1   )r/   r   r&   getmtimer,   r#   )	r-   titlefilenamefuncZexisting_cacheZondisk_mtimeentryZcached_mtimer1   r   r   r   cached_data_for_fileR   s    

r7   )r   r   os.pathr    r   Zmodules.pathsr   r   environr,   r&   r*   r   r   Lockr   r   r   r#   r/   r7   r   r   r   r   <module>   s    !