31 lines
954 B
Markdown
31 lines
954 B
Markdown
Node does not cache queries to `dns.lookup`, which can lead application doing a lot of connections to have perf issues and to saturate Node threads pool.
|
|
|
|
This library attempts to mitigate these problems by providing a version of this function with a version short cache, applied on both errors and results.
|
|
|
|
> Limitation: `verbatim: false` option is not supported.
|
|
|
|
It has exactly the same API as the native method and can be used directly:
|
|
|
|
```js
|
|
import { createCachedLookup } from '@vates/cached-dns.lookup'
|
|
|
|
const lookup = createCachedLookup()
|
|
|
|
lookup('example.net', { all: true, family: 0 }, (error, result) => {
|
|
if (error != null) {
|
|
return console.warn(error)
|
|
}
|
|
console.log(result)
|
|
})
|
|
```
|
|
|
|
Or it can be used to replace the native implementation and speed up the whole app:
|
|
|
|
```js
|
|
// assign our cached implementation to dns.lookup
|
|
const restore = createCachedLookup().patchGlobal()
|
|
|
|
// to restore the previous implementation
|
|
restore()
|
|
```
|