
    Vh                         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	 ddl
mZmZ ddlmZ ddlmZ  G d de      Zy)	zFile-based cache backend    N)md5)DEFAULT_TIMEOUT	BaseCache)locks)file_move_safec                        e Zd ZdZej
                  Z fdZedfdZ	ddZ
d ZedfdZedfdZdd	Zd
 ZddZd Zd ZddZd Zd Zd Z xZS )FileBasedCachez.djcachec                     t         |   |       t        j                  j	                  |      | _        | j                          y N)super__init__ospathabspath_dir
_createdir)selfdirparams	__class__s      T/home/dcms/DCMS/lib/python3.12/site-packages/django/core/cache/backends/filebased.pyr   zFileBasedCache.__init__   s.     GGOOC(	    Nc                 R    | j                  ||      ry| j                  ||||       yNFT)has_keyset)r   keyvaluetimeoutversions        r   addzFileBasedCache.add   s(    <<W%eWg.r   c                 2   | j                  ||      }	 t        |d      5 }| j                  |      s?t        j                  t        j                  |j                                     cd d d        S 	 d d d        |S # 1 sw Y   |S xY w# t        $ r Y |S w xY w)Nrb)	_key_to_fileopen_is_expiredpickleloadszlib
decompressreadFileNotFoundError)r   r   defaultr    fnamefs         r   getzFileBasedCache.get    s    !!#w/	eT" Ca''*!<<(ABC C*C
 C
  ! 		s5   B	 AA<'	B	 2B	 <BB	 B	 		BBc                    | j                  |      }|j                  t        j                  || j                               |j                  t        j                  t        j                  || j                                     y r   )get_backend_timeoutwriter'   dumpspickle_protocolr)   compress)r   filer   r   expirys        r   _write_contentzFileBasedCache._write_content*   sU    ))'2

6<<(<(<=>

4==eT5I5I!JKLr   c                    | j                          | j                  ||      }| j                          t        j                  | j
                        \  }}d}	 t        |d      5 }	| j                  |	||       d d d        t        ||d       d}|st        j                  |       y y # 1 sw Y   2xY w# |st        j                  |       w w xY w)N)r   FwbT)allow_overwrite)r   r$   _culltempfilemkstempr   r%   r9   r   r   remove)
r   r   r   r   r    r.   fdtmp_pathrenamedr/   s
             r   r   zFileBasedCache.set/   s    !!#w/

''DII6H	$b$ 71##Aw678UDAG		(# 7 7
 		(# s$   B6 %B*9B6 *B3/B6 6Cc                 D   	 t        | j                  ||      d      5 }	 t        j                  |t        j                         | j                  |      r	 t        j                  |       d d d        yt        j                  t        j                  |j                                     }|j                  d       | j                  |||       	 t        j                  |       d d d        y# t        j                  |       w xY w# 1 sw Y   y xY w# t        $ r Y yw xY w)Nzr+bFr   T)r%   r$   r   lockLOCK_EXr&   unlockr'   r(   r)   r*   r+   seekr9   r,   )r   r   r   r    r/   previous_values         r   touchzFileBasedCache.touch>   s    	d''W5u= $
$JJq%--0''*$ LLO$ $ *0dooaffh6O)Pq	++AwG#LLO$ $ LLO$ $ ! 		sX   D D5C-D+D 4AC-D$D -DDDD D 	DDc                 D    | j                  | j                  ||            S r   )_deleter$   r   r   r    s      r   deletezFileBasedCache.deleteO   s    ||D--c7;<<r   c                     |j                  | j                        rt        j                  j	                  |      sy	 t        j
                  |       y# t        $ r Y yw xY wr   )
startswithr   r   r   existsr@   r,   r   r.   s     r   rL   zFileBasedCache._deleteR   sP    		*"''..2G	IIe  ! 		s   A 	AAc                     | j                  ||      }	 t        |d      5 }| j                  |       cd d d        S # 1 sw Y   y xY w# t        $ r Y yw xY w)Nr#   F)r$   r%   r&   r,   )r   r   r    r.   r/   s        r   r   zFileBasedCache.has_key\   s\    !!#w/	eT" /a++A../ / /  		s+   A <	A AA A 	AAc                     | j                         }t        |      }|| j                  k  ry| j                  dk(  r| j	                         S t        j                  |t        || j                  z              }|D ]  }| j                  |        y)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len_max_entries_cull_frequencyclearrandomsampleintrL   )r   filelistnum_entriesr.   s       r   r=   zFileBasedCache._culld   s     ))+(m***1$::<==3{T=Q=Q/Q+RS 	 ELL	 r   c                     t        j                  d      }	 t        j                  | j                  dd       t        j                  |       y # t        j                  |       w xY w)N?   i  T)exist_ok)r   umaskmakedirsr   )r   	old_umasks     r   r   zFileBasedCache._creatediru   sC     HHUO		 KK		548HHYBHHYs   "A A&c           	          | j                  ||      }t        j                  j                  | j                  dj                  t        |j                         d      j                         | 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)	make_and_validate_keyr   r   joinr   r   encode	hexdigestcache_suffixrM   s      r   r$   zFileBasedCache._key_to_file~   sh    
 ((g(>ww||IIGG

e<FFH%%
 	
r   c                 P    | j                         D ]  }| j                  |        y)z-
        Remove all the cache files.
        N)rU   rL   rR   s     r   rY   zFileBasedCache.clear   s(     ++- 	 ELL	 r   c                     	 t        j                  |      }|C|t        j                         k  r,|j	                          | j                  |j                         yy# t        $ r d}Y Sw xY w)zL
        Take an open cache file `f` and delete it if it's expired.
        r   TF)r'   loadEOFErrortimecloserL   name)r   r/   exps      r   r&   zFileBasedCache._is_expired   s]    	++a.C ?sTYY[0GGILL   	C	s   A A+*A+c                     t        j                   d| j                   | j                        D cg c],  }t        j                  j                  | j                  |      . c}S c c}w )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.
        *)root_dir)globrl   r   r   r   ri   rR   s     r   rU   z FileBasedCache._list_cache_files   sT     Qt'8'8&9#:TYYO
 GGLLE*
 	
 
s   1A%)NNr   )__name__
__module____qualname__rl   r'   HIGHEST_PROTOCOLr5   r   r   r!   r0   r9   r   rJ   rN   rL   r   r=   r   r$   rY   r&   rU   __classcell__)r   s   @r   r	   r	      st    L--O
 '6t M
 '6t $ "1$ "= " 
  
r   r	   )__doc__rx   r   r'   rZ   r>   rq   r)   hashlibr   django.core.cache.backends.baser   r   django.core.filesr   django.core.files.mover   r	    r   r   <module>r      s7      	       F # 1Z
Y Z
r   