Resolver Caching Classes¶
The dnspython resolver does not cache by default, but caching can be enabled by creating a cache and assigning it to the resolver’s cache attribute. If a cache has been configured, the resolver caches both positive and negative responses. The cache respects the DNS TTL of the data, and will not return expired entries.
Two thread-safe cache implementations are provided, a simple dictionary-based Cache, and an LRUCache which provides cache size control suitable for use in web crawlers. Both are subclasses of a common base class which provides basic statistics. The LRUCache can also provide a hits count per cache entry.
- class dns.resolver.CacheBase[source]¶
- class dns.resolver.Cache(cleaning_interval=300.0)[source]¶
Simple thread-safe DNS answer cache.
cleaning_interval, a
float
is the number of seconds between periodic cleanings.- flush(key=None)[source]¶
Flush the cache.
If key is not
None
, only that item is flushed. Otherwise the entire cache is flushed.key, a
(dns.name.Name, int, int)
tuple whose values are the query name, rdtype, and rdclass respectively.
- class dns.resolver.LRUCache(max_size=100000)[source]¶
Thread-safe, bounded, least-recently-used DNS answer cache.
This cache is better than the simple cache (above) if you’re running a web crawler or other process that does a lot of resolutions. The LRUCache has a maximum number of nodes, and when it is full, the least-recently used node is removed to make space for a new one.
max_size, an
int
, is the maximum number of nodes to cache; it must be greater than 0.- flush(key=None)[source]¶
Flush the cache.
If key is not
None
, only that item is flushed. Otherwise the entire cache is flushed.key, a
(dns.name.Name, int, int)
tuple whose values are the query name, rdtype, and rdclass respectively.