o
    ¯bÃ  ã                   @   sF   d Z ddlmZ ddlmZmZ ddlmZmZ G dd„ dej	ƒZ
dS )z 
An in-memory caching resolver.
é    )Údefer)ÚcommonÚdns)ÚfailureÚlogc                   @   sR   e Zd ZdZdZddd„Zdd„ Zdd	„ Zd
d„ Zddd„Z	ddd„Z
dd„ ZdS )ÚCacheResolverzƒ
    A resolver that serves records from a local, memory cache.

    @ivar _reactor: A provider of L{interfaces.IReactorTime}.
    Nr   c                 C   sh   t j | ¡ i | _|| _i | _|d u rddlm} || _|r0| 	¡ D ]\}\}}|  
|||¡ q"d S d S )Nr   )Úreactor)r   ÚResolverBaseÚ__init__ÚcacheÚverboseÚcancelÚtwisted.internetr   Ú_reactorÚitemsÚcacheResult)Úselfr   r   r   ÚqueryÚsecondsÚpayload© r   ú5/usr/lib/python3/dist-packages/twisted/names/cache.pyr
      s   þzCacheResolver.__init__c           
      C   sb   || _ | j ¡ }| j ¡ D ]!\}\}\}}}|| }|| | D ]}	|	j|k r-| j|=  nq qd S ©N)Ú__dict__r   r   r   r   Úttl)
r   ÚstateÚnowÚkÚwhenÚansÚaddÚnsÚdiffÚrecr   r   r   Ú__setstate__&   s   

þ€ýzCacheResolver.__setstate__c                 C   s(   | j  ¡ D ]}|  ¡  q| j  ¡  | jS r   )r   ÚvaluesÚclearr   )r   Úcr   r   r   Ú__getstate__1   s   

zCacheResolver.__getstate__c              
      s  | j  ¡ }t |||¡}z| j| \}\}}	}
W n" ty:   | jdkr-t dt	|ƒ ¡ t
 t t |¡¡¡ Y S w | jrGt dt	|ƒ ¡ || ‰ z‡ fdd„|D ƒ‡ fdd„|	D ƒ‡ fdd„|
D ƒf}W n ty{   t
 t t |¡¡¡ Y S w t
 |¡S )Né   zCache miss for zCache hit for c              	      ó.   g | ]}t  |jj|j|j|jˆ  |j¡‘qS r   ©r   ÚRRHeaderÚnameÚtypeÚclsr   r   ©Ú.0Úr©r"   r   r   Ú
<listcomp>G   ó    ýÿÿz)CacheResolver._lookup.<locals>.<listcomp>c              	      r*   r   r+   r0   r3   r   r   r4   M   r5   c              	      r*   r   r+   r0   r3   r   r   r4   S   r5   )r   r   r   ÚQueryr   ÚKeyErrorr   r   ÚmsgÚreprr   Úfailr   ÚFailureÚDomainErrorÚ
ValueErrorÚsucceed)r   r-   r/   r.   Útimeoutr   Úqr   r   Úauthr    Úresultr   r3   r   Ú_lookup7   s6   

ý
ü
ü
üóÿ
zCacheResolver._lookupc                 C   s   t  t t |¡¡¡S r   )r   r:   r   r;   r   r<   )r   r-   r?   r   r   r   ÚlookupAllRecords_   s   zCacheResolver.lookupAllRecordsc                 C   s²   | j dkrt d| ¡ |p| j ¡ |f| j|< || jv r$| j|  ¡  t|d ƒt|d ƒ t|d ƒ }|rI|d j}|D ]}t	||jƒ}q?nd}| j 
|| j|¡| j|< dS )a±  
        Cache a DNS entry.

        @param query: a L{dns.Query} instance.

        @param payload: a 3-tuple of lists of L{dns.RRHeader} records, the
            matching result of the query (answers, authority and additional).

        @param cacheTime: The time (seconds since epoch) at which the entry is
            considered to have been added to the cache. If L{None} is given,
            the current time is used.
        r)   zAdding %r to cacher   é   N)r   r   r8   r   r   r   r   Úlistr   ÚminÚ	callLaterÚ
clearEntry)r   r   r   Ú	cacheTimeÚsÚmr2   r   r   r   r   b   s   

$
ÿzCacheResolver.cacheResultc                 C   s   | j |= | j|= d S r   )r   r   )r   r   r   r   r   rI   �   s   zCacheResolver.clearEntry)Nr   Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r$   r(   rC   rD   r   rI   r   r   r   r   r      s    

(
r   N)rP   r   r   Útwisted.namesr   r   Útwisted.pythonr   r   r	   r   r   r   r   r   Ú<module>   s
   