
ɀ^               P   @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z y d  d l m Z m Z Wn Yn Xd  Z d Z	 e	 d Z
 e	 d Z e	 d Z e d Z e d Z e d	 Z e d
 Z e d Z e d Z e d Z e d	 Z e d Z e d Z e d Z e
 Z d e d e d e d e d e d e d e d e d e d e d e d e d e d e d e d e d  e d! e d" e d# e d$ e d% e d& e d' e d( e d) e d* e d+ e d, e d- e d. e d/ e d0 e d1 e d2 e d3 e d4 e i% Z d5 d6   Z d7 d8   Z Gd9 d:   d:  Z d S);    N)datetime)UnionAny<                                 im  zapplication/javascriptzapplication/x-javascriptztext/javascriptztext/cssztext/x-cross-domain-policyzapplication/vnd.ms-fontobjectzfont/eotzfont/opentypezapplication/x-font-ttfzapplication/font-woffzapplication/x-font-woffz	font/woffzapplication/font-woff2z	audio/oggz	image/bmpz	image/gifz
image/jpegz	image/pngzimage/svg+xmlz
image/webpz	video/mp4z	video/oggz
video/webmzimage/vnd.microsoft.iconzimage/x-iconzapplication/manifest+jsonzapplication/atom+xmlzapplication/rss+xmlzapplication/jsonzapplication/ld+jsonzapplication/schema+jsonzapplication/vnd.geo+jsonzapplication/xmlztext/xmlz	text/htmlz&application/x-web-client-manifest+jsonztext/cache-manifestc             C   s   t  j |  t  S)N)mime_expire_listget
EXPIRE_NOW)mime r   (/var/www/zmirror/zmirror/cache_system.pyget_expire_from_mimeO   s    r   c             C   sY   t  |  t t f  r |  Sy+ t t j t j |  d  j     } Wn d } Yn X| S)zH
    :type timestring: Union[str, int]
    :rtype: Union[int, None]
    z%a, %d %b %Y %H:%M:%S %ZN)
isinstanceintfloattimemktimer   strptime	timetuple)Z
timestringtr   r   r   _time_str_to_unixS   s    +r   c               @   s   e  Z d  Z d d d  Z d d   Z e d d d d d	  Z d
 d   Z d d   Z d d d  Z	 d d   Z
 d d   Z d d   Z d d d  Z d d   Z d d   Z d S)	FileCachei    c             C   s   i  |  _  | d |  _ d  S)Ni   )
items_dictmax_size_byte)selfZmax_size_kbr   r   r   __init__b   s    	zFileCache.__init__c             C   s   |  j    d  S)N)	flush_all)r#   r   r   r   __del__f   s    zFileCache.__del__r   Nc       	      C   s   | d k s | |  j  k r d S|  j |  t j d d d d d d  } t j | | d t j | j | t t	 j	    | t
 |  f } | j   | |  j | <d	 S)
u  
        将一个对象存入缓存
        :param key: key
        :param last_modified: str  format: "Mon, 18 Nov 2013 09:02:42 GMT"
        :param obj_size: too big object should not be cached
        :param expires: seconds to expire
        :param info_dict: custom dict contains information, stored in memory, so can access quickly
        :type key: str
        :type last_modified: str
        :type info_dict: dict or None
        :type obj: Any
        r   FprefixZzmirror_suffixz.tmpdeleteprotocolT)r"   r)   tempfileNamedTemporaryFilepickledumpHIGHEST_PROTOCOLnamer   r   r   closer!   )	r#   keyobjexpiresobj_sizelast_modified	info_dict	temp_fileZ
cache_itemr   r   r   put_obji   s    
zFileCache.put_objc             C   sM   |  j  |  rI |  j | d } |  j | =t j j |  rI t j |  d  S)Nr   )_is_item_existr!   ospathexistsremove)r#   r2   	file_pathr   r   r   r)      s
    
zFileCache.deletec             C   s1   x* t  |  j j    D] } |  j |  q Wd  S)N)listr!   keysr)   )r#   r2   r   r   r   r%      s    zFileCache.flush_allFc             C   sl   | r |  j    d  Sg  } x- |  j D]" } |  j |  r$ | j |  q$ Wx | D] } |  j |  qQ Wd  S)N)r%   r!   
is_expiresappendr)   )r#   force_flush_allZkeys_to_deleteZitem_keyr2   r   r   r   check_all_expire   s    
zFileCache.check_all_expirec             C   s;   |  j  |  s d S|  j |  r3 |  j |  d Sd Sd  S)NFT)r:   rB   r)   )r#   r2   r   r   r   	is_cached   s    zFileCache.is_cachedc             C   sw   |  j  |  ro |  j | d } y, t | d   } t j |  } Wd  QRXWn |  j |  d  SYqs X| Sn d  Sd  S)Nr   rb)rF   r!   openr-   loadr)   )r#   r2   r?   fpr3   r   r   r   get_obj   s    	zFileCache.get_objc             C   s&   |  j  |  r |  j | d Sd  Sd  S)N   )rF   r!   )r#   r2   r   r   r   get_info   s    zFileCache.get_infoc             C   s[   |  j  |  s | d  k r  d S|  j | d } | d  k rA d S| t |  k rW d Sd  S)NFr
   T)rF   r!   r   )r#   r2   r6   ctr   r   r   is_unchanged   s    zFileCache.is_unchangedc             C   s3   |  j  | } t j   | d | d k r/ d Sd S)Nr   r	   TF)r!   r   )r#   r2   itemr   r   r   rB      s    zFileCache.is_expiresc             C   s   | |  j  k S)N)r!   )r#   r2   r   r   r   r:      s    zFileCache._is_item_exist)__name__
__module____qualname__r$   r&   DEFAULT_EXPIREr9   r)   r%   rE   rF   rK   rM   rO   rB   r:   r   r   r   r   r    a   s    	
r    )r;   r+   r   r-   r   typingr   r   r   ZEXPIRE_1MINZEXPIRE_5MINZEXPIRE_30MINZ
EXPIRE_1HRZ
EXPIRE_2HRZ
EXPIRE_3HRZ
EXPIRE_4HRZ
EXPIRE_6HRZEXPIRE_12HRZEXPIRE_1DAYZEXPIRE_2DAYZEXPIRE_4DAYZEXPIRE_1WEEKZEXPIRE_1MOUTHZ
EXPIRE_1YRrT   r   r   r   r    r   r   r   r   <module>   s   













