Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DNS lookup cache for Python using dnspython

License

NotificationsYou must be signed in to change notification settings

jayvdb/dns-cache

Repository files navigation

dns-cache is a Python client side DNS caching framework utilisingdnspython v1.15+ for DNSand supports various local key stores, and provides caching of lookup failures,and configurable expiration of cached responses.

Some reasons to use a client side cache include:

  • processing data containing many repeated invalid domains,
  • running a local DNS caching service is not practical or appropriate,
  • adding reporting of DNS activity performed within a job.

Installation

The recommended way to installdns-cache is by using pip as follows:

pip install dns-cache

Getting started

To quickly benefit from client side dns-caching in your existing application, install the system resolver.

importdns_cacheimportrequestsdns_cache.override_system_resolver()foriinrange(10):requests.get('http://www.coala.io/')

If you have a fast dns proxy, 10 requests will possibly show no performance improvement.Even 100 may not perform better in this contrived example.

However when many parts of a system are performing lookups on the same DNS records, or wheresessions are being closed and new ones created and need to access the same DNS records,the difference becomes more noticable, especially in jobs which takes hours to run.

For long running jobs, use themin_ttl argument to increase the default if 5 minutes isnt sufficient.It can be set todns_cache.NO_EXPIRY for a ttl of one week, which is not recommended except whenaccompanied with custom cache expiration logic.

Key stores

Multiple key stores are supported, and their dependencies need to added separately as required.

  1. pickle andpickle4 backport:dns_cache.pickle.PickableCache
  2. diskcache:dns_cache.diskcache.DiskCache
  3. stash.py:dns_cache.stash.StashCache
  4. sqlitedict:dns_cache.sqlitedict.SqliteDictCache
  5. disk_dict:dns_cache.disk_dict.DiskDictCache (Python 2.7 only)

stash.py support usespickle orjsonpickle on Python 3, however onlyjsonpickle works on Python 2.7.

Caching additions

The following classes can be used separately or together.

  1. dns_cache.resolver.AggressiveCachingResolver: indexes all qnames in the response, increasing the number of keys,but reducing the number of requests and cached responses when several related records are requested, such as a HTTP redirectfromwww.foo.com to foo.com (or vis versa) where one is a CNAME point to the other.
  2. dns_cache.resolver.ExceptionCachingResolver: caches lookup failures.
  3. dns_cache.hosts.HostsCache: preloads hosts (e.g./etc/hosts) into a cache

Note:dns_cache.override_system_resolver() can be used to install a customresolver orcache, which maybe derived from the above classes or your own implementation from scratch. It preloads/etc/hosts.

Similar projects

Python:

  1. velocity is a lighter weight approach, with aserious bug
  2. dnsplug, unfortunately not available on PyPI.

Other:

  1. dnscache (Go)
  2. native-dns-cache (Node)

About

DNS lookup cache for Python using dnspython

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2026 Movatter.jp