Skip to content

A cacheable dns.lookup(…) that respects TTL 🎉

License

Notifications You must be signed in to change notification settings

yoannmoinet/cacheable-lookup

 
 

Repository files navigation

cacheable-lookup

A cacheable dns.lookup(…) that respects TTL 🎉

Node CI Coverage Status npm install size

Making lots of HTTP requests? You can save some time by caching DNS lookups ⚡

Usage

Using the lookup option

const http = require('http');
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();

http.get('https://example.com', {lookup: cacheable.lookup}, response => {
	// Handle the response here
});

Attaching CacheableLookup to an Agent

const http = require('http');
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();

cacheable.install(http.globalAgent);

http.get('https://example.com', response => {
	// Handle the response here
});

API

new CacheableLookup(options)

Returns a new instance of CacheableLookup.

cache

Type: TTLMap | Keyv
Default: new TTLMap()

Custom cache instance. If undefined, it will create a new one.

Note: If you decide to use Keyv instead of the native implementation, the performance will drop by 10x. Memory leaks may occur as it doesn't provide any way to remove all the deprecated values at once.

options

Type: object
Default: {}

Options used to cache the DNS lookups.

options.maxTtl

Type: number
Default: Infinity

Limits the cache time (TTL in seconds).

If set to 0, it will make a new DNS query each time.

options.resolver

Type: dns.Resolver | dns.promises.Resolver
Default: new dns.promises.Resolver()

An instance of DNS Resolver used to make DNS queries.

options.customHostsPath

Type: string
Default: undefined (OS-specific)

The full path to the hosts file. Set this to false to prevent loading entries from the hosts file.

Entry object

Type: object

address

Type: string

The IP address (can be an IPv4 or IPv6 address).

family

Type: number

The IP family (4 or 6).

expires

Type: number

Note: This is not present when using the native dns.lookup(...)!

The timestamp (Date.now() + ttl * 1000) when the entry expires.

ttl

Note: This is not present when using the native dns.lookup(...)!

The time in seconds for its lifetime.

Entry object (callback-style)

When options.all is false, then callback(error, address, family, expires, ttl) is called.
When options.all is true, then callback(error, entries) is called.

CacheableLookup instance

servers

Type: Array

DNS servers used to make the query. Can be overridden - then the new servers will be used.

lookupAsync(hostname, options)

The asynchronous version of dns.lookup(…).

Returns an entry object.
If options.all is true, returns an array of entry objects.

Note: If entry(ies) were not found, it will return undefined by default.

hostname

Type: string

options

Type: object

The same as the dns.lookup(…) options.

options.throwNotFound

Type: boolean
Default: true

If set to false and it gets no match, it will return undefined. If set to true and it gets no match, it will throw ENOTFOUND error.

Note: This option is meant only for the asynchronous implementation! The callback version will always pass an error if no match found.

query(hostname)

An asynchronous function which returns cached DNS lookup entries.
This is the base for lookupAsync(hostname, options) and lookup(hostname, options, callback).

Note: This function has no options.

Returns an array of objects with address, family, ttl and expires properties.

queryAndCache(hostname)

An asynchronous function which makes two DNS queries: A and AAAA. The result is cached.
This is used by query(hostname) if no entry in the database is present.

Returns an array of objects with address, family, ttl and expires properties.

tick()

Removes outdated entries.

updateInterfaceInfo()

Updates interface info. For example, you need to run this when you plug or unplug your WiFi driver.

Note: Running updateInterfaceInfo() will also trigger clear()!

clear()

Clears the cache.

High performance

Performed on:

  • Query: example.com
  • CPU: i7-7700k
  • CPU governor: performance
CacheableLookup#lookupAsync                x 2,024,888 ops/sec ±0.84%  (87 runs sampled)
CacheableLookup#lookupAsync.all            x 2,093,860 ops/sec ±1.00%  (88 runs sampled)
CacheableLookup#lookupAsync.all.ADDRCONFIG x 1,898,088 ops/sec ±0.61%  (89 runs sampled)
CacheableLookup#lookup                     x 1,905,060 ops/sec ±0.76%  (90 runs sampled)
CacheableLookup#lookup.all                 x 1,889,284 ops/sec ±1.37%  (87 runs sampled)
CacheableLookup#lookup.all.ADDRCONFIG      x 1,740,616 ops/sec ±0.83%  (89 runs sampled)
CacheableLookup#lookupAsync - zero TTL     x 226       ops/sec ±3.55%  (56 runs sampled)
CacheableLookup#lookup      - zero TTL     x 228       ops/sec ±2.48%  (62 runs sampled)
dns#resolve4                               x 346       ops/sec ±3.58%  (55 runs sampled)
dns#lookup                                 x 20,368    ops/sec ±38.31% (53 runs sampled)
dns#lookup.all                             x 13,529    ops/sec ±31.35% (29 runs sampled)
dns#lookup.all.ADDRCONFIG                  x 6,211     ops/sec ±22.92% (26 runs sampled)
Fastest is CacheableLookup#lookupAsync.all

The package is based on dns.resolve4(…) and dns.resolve6(…).

Why not dns.lookup(…)?

It is not possible to use dns.lookup(…) because underlying calls like getaddrinfo have no concept of servers or TTL (caching is done on OS level instead).

Related

License

MIT

About

A cacheable dns.lookup(…) that respects TTL 🎉

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 96.3%
  • TypeScript 3.7%