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
   