
 X5                 @   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 Z d d l	 Z	 d d l
 m Z m Z d d l m Z d d l m Z y d d l m Z Wn e k
 r d d l Z Yn XGd d   d e  Z d S)	zFile-based cache backend    N)DEFAULT_TIMEOUT	BaseCache)file_move_safe)force_bytes)cPicklec                   s   e  Z d  Z d Z   f d d   Z e d d d  Z d d d d  Z e d 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   S)FileBasedCachez.djcachec                s9   t  t |   j |  t j j |  |  _ |  j   d  S)N)superr   __init__ospathabspath_dir
_createdir)selfdirparams)	__class__ R/home/ubuntu/projects/ifolica/build/django/django/core/cache/backends/filebased.pyr	      s    zFileBasedCache.__init__Nc             C   s0   |  j  | |  r d S|  j | | | |  d S)NFT)has_keyset)r   keyvaluetimeoutversionr   r   r   add   s    zFileBasedCache.addc             C   s   |  j  | |  } t j j |  r yJ t j | d  2 } |  j |  sg t j t	 j
 | j     SWd  QXWq t k
 r } z | j t j k r n  WYd  d  } ~ Xq Xn  | S)Nrb)_key_to_filer
   r   existsioopen_is_expiredpickleloadszlib
decompressreadIOErrorerrnoENOENT)r   r   defaultr   fnamefer   r   r   get$   s    &zFileBasedCache.getc             C   s   |  j    |  j | |  } |  j   t j d |  j  \ } } d } z t j | d  W }	 |  j |  }
 |	 j	 t
 j |
 t
 j   |	 j	 t j t
 j | t
 j    Wd  QXt | | d d d } Wd  | s t j |  n  Xd  S)Nr   FwbZallow_overwriteT)r   r   _culltempfilemkstempr   r   r    Zget_backend_timeoutwriter"   dumpsHIGHEST_PROTOCOLr$   compressr   r
   remove)r   r   r   r   r   r+   fdtmp_pathZrenamedr,   Zexpiryr   r   r   r   0   s    

+
zFileBasedCache.setc             C   s   |  j  |  j | |   d  S)N)_deleter   )r   r   r   r   r   r   deleteA   s    zFileBasedCache.deletec             C   s   | j  |  j  s& t j j |  r* d  Sy t j |  Wn= t k
 rz } z | j t j k rh   n  WYd  d  } ~ Xn Xd  S)N)	
startswithr   r
   r   r   r7   OSErrorr(   r)   )r   r+   r-   r   r   r   r:   D   s    &zFileBasedCache._deletec          	   C   sT   |  j  | |  } t j j |  rP t j | d   } |  j |  SWd  QXn  d S)Nr   F)r   r
   r   r   r   r    r!   )r   r   r   r+   r,   r   r   r   r   O   s
    zFileBasedCache.has_keyc             C   s   |  j    } t |  } | |  j k  r+ d S|  j d k rD |  j   St j | t | |  j   } x | D] } |  j |  qj Wd S)z
        Removes 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   r0   V   s    
	zFileBasedCache._cullc             C   s   t  j j |  j  s y t  j |  j d  Wq t k
 r{ } z- | j t j k ri t d |  j   n  WYd  d  } ~ Xq Xn  d  S)Ni  z=Cache directory '%s' does not exist and could not be created')	r
   r   r   r   makedirsr=   r(   EEXISTEnvironmentError)r   r-   r   r   r   r   h   s    zFileBasedCache._createdirc             C   s_   |  j  | d | } |  j |  t j j |  j d j t j t |   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    )make_keyZvalidate_keyr
   r   joinr   hashlibmd5r   	hexdigestcache_suffix)r   r   r   r   r   r   r   r   s    zFileBasedCache._key_to_filec             C   sA   t  j j |  j  s d Sx! |  j   D] } |  j |  q& Wd S)z-
        Remove all the cache files.
        N)r
   r   r   r   r>   r:   )r   r+   r   r   r   r@   |   s    zFileBasedCache.clearc             C   sO   t  j |  } | d k	 rK | t j   k  rK | j   |  j | j  d Sd S)z
        Takes an open cache file and determines if it has expired,
        deletes the file if it is has passed its expiry time.
        NTF)r"   loadtimecloser:   name)r   r,   expr   r   r   r!      s    
zFileBasedCache._is_expiredc                sL   t  j j   j  s g  S  f d d   t j   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   j |   q Sr   )r
   r   rJ   r   ).0r+   )r   r   r   
<listcomp>   s   	 z4FileBasedCache._list_cache_files.<locals>.<listcomp>z*%s)r
   r   r   r   globglob1rN   )r   rD   r   )r   r   r>      s
     z FileBasedCache._list_cache_files)__name__
__module____qualname__rN   r	   r   r   r.   r   r;   r:   r   r0   r   r   r@   r!   r>   r   r   )r   r   r      s   

	r   )__doc__r(   rV   rK   r   r
   rA   r1   rP   r$   Zdjango.core.cache.backends.baser   r   Zdjango.core.files.mover   Zdjango.utils.encodingr   Zdjango.utils.six.movesr   r"   ImportErrorr   r   r   r   r   <module>   s"   