|
1 | | -# dns-cache |
| 1 | +# dns-cache |
| 2 | + |
| 3 | +`dns-cache` is a Python client side DNS caching framework utilising |
| 4 | +[`dnspython`](https://github.com/rthalley/dnspython) v1.15+ for DNS |
| 5 | +and supports various local key stores, and provides caching of lookup failures, |
| 6 | +and configurable expiration of cached responses. |
| 7 | + |
| 8 | +Some reasons to use a client side cache include: |
| 9 | +- processing data containing many repeated invalid domains, |
| 10 | +- running a local DNS caching service is not practical or appropriate, |
| 11 | +- adding reporting of DNS activity performed within a job. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +The recommended way to install `dns-cache` is by using pip as follows: |
| 16 | + |
| 17 | +`pip install dns-cache` |
| 18 | + |
| 19 | +## Getting started |
| 20 | + |
| 21 | +To quickly benefit from client side dns-caching in your existing application, install the system resolver. |
| 22 | + |
| 23 | +```python |
| 24 | +import dns_cache |
| 25 | +import requests |
| 26 | + |
| 27 | +dns_cache.override_system_resolver() |
| 28 | + |
| 29 | +for i in range(10): |
| 30 | + requests.get('http://www.coala.io/') |
| 31 | +``` |
| 32 | + |
| 33 | +If you have a fast dns proxy, 10 requests will possibly show no performance improvement. |
| 34 | +Even 100 may not perform better in this contrived example. |
| 35 | + |
| 36 | +However when many parts of a system are performing lookups on the same DNS records, or where |
| 37 | +sessions are being closed and new ones created and need to access the same DNS records, |
| 38 | +the difference becomes more noticable, especially in jobs which takes hours to run. |
| 39 | + |
| 40 | +For long running jobs, use the `min_ttl` argument to increase the default if 5 minutes isnt sufficient. |
| 41 | +It can be set to `dns_cache.NO_EXPIRY` for a ttl of one week, which is not recommended except when |
| 42 | +accompanied with custom cache expiration logic. |
| 43 | + |
| 44 | +## Key stores |
| 45 | + |
| 46 | +Multiple key stores are supported, and their dependencies need to added separately as required. |
| 47 | + |
| 48 | +1. `pickle` and [`pickle4`](https://github.com/moreati/pickle4) backport: `dns_cache.pickle.PickableCache` |
| 49 | +2. [`diskcache`](https://github.com/grantjenks/python-diskcache): `dns_cache.diskcache.DiskCache` |
| 50 | +3. [`stash.py`](https://github.com/fuzeman/stash.py/): `dns_cache.stash.StashCache` |
| 51 | +4. [`sqlitedict`](https://github.com/RaRe-Technologies/sqlitedict): `dns_cache.sqlitedict.SqliteDictCache` |
| 52 | +5. [`disk_dict`](https://github.com/AWNystrom/DiskDict): `dns_cache.disk_dict.DiskDictCache` (Python 2.7 only) |
| 53 | + |
| 54 | +`stash.py` support uses `pickle` or `jsonpickle` on Python 3, however only `jsonpickle` works on Python 2.7. |
| 55 | + |
| 56 | +## Caching additions |
| 57 | + |
| 58 | +The following classes can be used separately or together. |
| 59 | + |
| 60 | +1. `dns_cache.resolver.AggressiveCachingResolver`: indexes all qnames in the response, increasing the number of keys, |
| 61 | + but reducing the number of requests and cached responses when several related records are requested, such as a HTTP redirect |
| 62 | + from www.foo.com to foo.com (or vis versa) where one is a CNAME point to the other. |
| 63 | +2. `dns_cache.resolver.ExceptionCachingResolver`: caches lookup failures. |
| 64 | + |
| 65 | +**Note:** `dns_cache.override_system_resolver()` can be used to install a custom `resolver` or `cache`, which may |
| 66 | +be derived from the above classes or your own implementation from scratch. |
| 67 | + |
| 68 | +## TODO |
| 69 | + |
| 70 | +1. Support [`python-benedict`](https://github.com/fabiocaccamo/python-benedict) |
| 71 | +2. Use [`dnsbin`](https://github.com/ettic-team/dnsbin) for testing |
| 72 | +3. Add redis, memcached and cloud caching backends |
| 73 | + |
| 74 | +## Similar projects |
| 75 | + |
| 76 | +Python: |
| 77 | +1. [`velocity`](https://github.com/s0md3v/velocity) is a lighter weight approach, with a [`serious bug`](https://github.com/s0md3v/velocity/issues/2) |
| 78 | +2. [`dnsplug`](https://github.com/nresare/dnsplug), unfortunately not available on PyPI. |
| 79 | + |
| 80 | +Go: |
| 81 | +1. [`dnscache`](https://github.com/rs/dnscache) |
0 commit comments