a
    Ê[Ùf¨  ã                   @   s„   d Z ddlZddlZ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
 ddlmZ ddlmZ ddlmZ G dd„ de
ƒZdS )	zFile-based cache backendé    N)ÚDEFAULT_TIMEOUTÚ	BaseCache)Úlocks)Úfile_move_safe)Úmd5c                       s°   e Zd ZdZejZ‡ fdd„Zedfdd„Z	d!dd„Z
d	d
„ Zedfdd„Zedfdd„Zd"dd„Zdd„ Zd#dd„Zdd„ Zdd„ Zd$dd„Zdd„ Zdd„ Zdd „ Z‡  ZS )%ÚFileBasedCachez.djcachec                    s&   t ƒ  |¡ tj |¡| _|  ¡  d S ©N)ÚsuperÚ__init__ÚosÚpathÚabspathÚ_dirÚ
_createdir)ÚselfÚdirÚparams©Ú	__class__© úvC:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\site-packages\django/core/cache/backends/filebased.pyr
      s    zFileBasedCache.__init__Nc                 C   s$   |   ||¡rdS |  ||||¡ dS ©NFT)Úhas_keyÚset)r   ÚkeyÚvalueÚtimeoutÚversionr   r   r   Úadd   s    zFileBasedCache.addc                 C   s€   |   ||¡}z\t|dƒ>}|  |¡sHt t | ¡ ¡¡W  d   ƒ W S W d   ƒ n1 s\0    Y  W n tyz   Y n0 |S )NÚrb)	Ú_key_to_fileÚopenÚ_is_expiredÚpickleÚloadsÚzlibÚ
decompressÚreadÚFileNotFoundError)r   r   Údefaultr   ÚfnameÚfr   r   r   Úget   s    
FzFileBasedCache.getc                 C   s<   |   |¡}| t || j¡¡ | t t || j¡¡¡ d S r   )Zget_backend_timeoutÚwriter#   ÚdumpsÚpickle_protocolr%   Úcompress)r   Úfiler   r   Zexpiryr   r   r   Ú_write_content)   s    
zFileBasedCache._write_contentc           
   	   C   s¤   |   ¡  |  ||¡}|  ¡  tj| jd\}}d}z\t|dƒ}	|  |	||¡ W d   ƒ n1 sb0    Y  t||dd d}W |s t	 
|¡ n|sžt	 
|¡ 0 d S )N)r   FÚwbT)Zallow_overwrite)r   r    Ú_cullÚtempfileÚmkstempr   r!   r2   r   r   Úremove)
r   r   r   r   r   r*   ÚfdZtmp_pathZrenamedr+   r   r   r   r   .   s    ,ÿzFileBasedCache.setc              
   C   sì   zÒt |  ||¡dƒ¬}zŽt |tj¡ |  |¡rNW t |¡ W d   ƒ W dS t t	 
| ¡ ¡¡}| d¡ |  |||¡ W t |¡ W d   ƒ W dS W t |¡ nt |¡ 0 W d   ƒ n1 sÆ0    Y  W n tyæ   Y dS 0 d S )Nzr+bFr   T)r!   r    r   ÚlockZLOCK_EXr"   Úunlockr#   r$   r%   r&   r'   Úseekr2   r(   )r   r   r   r   r+   Úprevious_valuer   r   r   Útouch=   s"    

ù

þ:zFileBasedCache.touchc                 C   s   |   |  ||¡¡S r   )Ú_deleter    ©r   r   r   r   r   r   ÚdeleteN   s    zFileBasedCache.deletec                 C   sD   |  | j¡rtj |¡sdS zt |¡ W n ty>   Y dS 0 dS r   )Ú
startswithr   r   r   Úexistsr7   r(   ©r   r*   r   r   r   r>   Q   s    zFileBasedCache._deletec                 C   sb   |   ||¡}z<t|dƒ}|  |¡ W  d   ƒ W S 1 s<0    Y  W n ty\   Y dS 0 d S )Nr   F)r    r!   r"   r(   )r   r   r   r*   r+   r   r   r   r   [   s    0zFileBasedCache.has_keyc                 C   s^   |   ¡ }t|ƒ}|| jk rdS | jdkr0|  ¡ S t |t|| j ƒ¡}|D ]}|  |¡ qJdS )zÏ
        Remove random cache entries if max_entries is reached at a ratio
        of num_entries / cull_frequency. A value of 0 for CULL_FREQUENCY means
        that the entire cache will be purged.
        Nr   )	Ú_list_cache_filesÚlenZ_max_entriesZ_cull_frequencyÚclearÚrandomÚsampleÚintr>   )r   ÚfilelistZnum_entriesr*   r   r   r   r4   c   s    

zFileBasedCache._cullc              	   C   s<   t  d¡}z t j| jddd W t  |¡ nt  |¡ 0 d S )Né?   iÀ  T)Úexist_ok)r   ÚumaskÚmakedirsr   )r   Z	old_umaskr   r   r   r   t   s    
zFileBasedCache._createdirc              	   C   s:   | j ||d}tj | jd t| ¡ dd ¡ | jg¡¡S )z•
        Convert a key into a cache file path. Basically this is the
        root cache path joined with the md5sum of the key and a suffix.
        )r   Ú F)Úusedforsecurity)	Zmake_and_validate_keyr   r   Újoinr   r   ÚencodeÚ	hexdigestÚcache_suffixr?   r   r   r   r    }   s    þÿþzFileBasedCache._key_to_filec                 C   s   |   ¡ D ]}|  |¡ qdS )z-
        Remove all the cache files.
        N)rD   r>   rC   r   r   r   rF      s    zFileBasedCache.clearc                 C   sV   zt  |¡}W n ty$   d}Y n0 |durR|t ¡ k rR| ¡  |  |j¡ dS dS )zL
        Take an open cache file `f` and delete it if it's expired.
        r   NTF)r#   ÚloadÚEOFErrorÚtimeÚcloser>   Úname)r   r+   Úexpr   r   r   r"   ”   s    
zFileBasedCache._is_expiredc                    s"   ‡ fdd„t  ˆ jdˆ j ¡D ƒS )z‘
        Get a list of paths to all the cache files. These are all the files
        in the root cache dir that end on the cache_suffix.
        c                    s   g | ]}t j ˆ j|¡‘qS r   )r   r   rQ   r   )Ú.0r*   ©r   r   r   Ú
<listcomp>§   s   ÿz4FileBasedCache._list_cache_files.<locals>.<listcomp>z*%s)ÚglobÚglob1r   rT   r\   r   r\   r   rD   ¢   s    
þz FileBasedCache._list_cache_files)NN)N)N)N)Ú__name__Ú
__module__Ú__qualname__rT   r#   ÚHIGHEST_PROTOCOLr/   r
   r   r   r,   r2   r   r=   r@   r>   r   r4   r   r    rF   r"   rD   Ú__classcell__r   r   r   r   r      s"   




	
r   )Ú__doc__r^   r   r#   rG   r5   rW   r%   Zdjango.core.cache.backends.baser   r   Zdjango.core.filesr   Zdjango.core.files.mover   Zdjango.utils.cryptor   r   r   r   r   r   Ú<module>   s   