o b@sFdZddlmZddlmZmZddlmZmZGdddej Z dS)z An in-memory caching resolver. )defer)commondns)failurelogc@sReZdZdZdZdddZddZdd Zd d Zdd d Z dddZ ddZ dS) CacheResolverz A resolver that serves records from a local, memory cache. @ivar _reactor: A provider of L{interfaces.IReactorTime}. NrcCshtj|i|_||_i|_|durddlm}||_|r0| D]\}\}}| |||q"dSdS)Nr)reactor) r ResolverBase__init__cacheverbosecanceltwisted.internetr_reactoritems cacheResult)selfr r rquerysecondspayloadr5/usr/lib/python3/dist-packages/twisted/names/cache.pyr s  zCacheResolver.__init__c Csb||_|j}|jD]!\}\}\}}}||}|||D] } | j|kr-|j|=nq q dSN)__dict__rrr rttl) rstatenowkwhenansaddnsdiffrecrrr __setstate__&s  zCacheResolver.__setstate__cCs(|jD]}|q|j|jSr)r valuesclearr)rcrrr __getstate__1s  zCacheResolver.__getstate__c s|j}t|||}z |j|\}\}} } Wn"ty:|jdkr-tdt |t t t|YSw|jrGtdt |||zfdd|Dfdd| Dfdd| Df} Wnty{t t t|YSwt | S)NzCache miss for zCache hit for c .g|]}t|jj|j|j|j|jqSrrRRHeadernametypeclsrr.0rr"rr G z)CacheResolver._lookup..c r*rr+r0r3rrr4Mr5c r*rr+r0r3rrr4Sr5)rrrQueryr KeyErrorr rmsgreprrfailrFailure DomainError ValueErrorsucceed) rr-r/r.timeoutrqrrauthr resultrr3r_lookup7s6        zCacheResolver._lookupcCsttt|Sr)rr:rr;rr<)rr-r?rrrlookupAllRecords_szCacheResolver.lookupAllRecordscCs|jdkr td||p|j|f|j|<||jvr$|j|t|dt|dt|d}|rI|dj}|D]}t ||j}q?nd}|j ||j ||j|<dS)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)zAdding %r to cacherN) r rr8rrr r listrmin callLater clearEntry)rrr cacheTimesmr2rrrrbs  $ zCacheResolver.cacheResultcCs|j|=|j|=dSr)r r )rrrrrrIs zCacheResolver.clearEntry)NrNr) __name__ __module__ __qualname____doc__r r r$r(rCrDrrIrrrrrs   (  rN) rPrr twisted.namesrrtwisted.pythonrrr rrrrrs