DNS Cache for Node.js
dnscache is an unmaintained Node.js module (last published 7 years ago, current version 1.0.2) that transparently wraps the built-in `dns` module to provide an application-level caching layer for DNS lookup results. It operates on a GOF Proxy design pattern, intercepting all `require('dns')` calls to cache frequently accessed domain resolutions, aiming to reduce network latency and improve application performance. It offers configurable cache size and Time-To-Live (TTL) settings, and supports injecting a custom cache implementation. While it historically addressed Node.js's lack of built-in DNS caching, it is now largely superseded by more modern, actively maintained libraries and evolving Node.js DNS behaviors. There is no active release cadence, and it does not support modern ECMAScript Modules (ESM) syntax.
Common errors
-
TypeError: dnscache is not a function
cause Attempting to import `dnscache` using ECMAScript Modules (ESM) syntax (e.g., `import dnscache from 'dnscache'`).fixUse CommonJS `require` syntax: `const dnscache = require('dnscache');` -
Error: getaddrinfo ENOTFOUND www.example.com
cause While using `dnscache`, receiving ENOTFOUND errors, potentially due to DNS resolution issues or (less commonly) stale cache entries if a domain's IP changed but the cache was not updated.fixCheck network connectivity and DNS server configuration. If suspecting stale cache, reduce `ttl` or explicitly clear the cache if the library supports it (though `dnscache` does not expose a public cache clearing API, it relies on TTL expiration). The library configuration includes `ttl` and `cachesize` parameters. -
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
cause `dnscache` is callback-based and does not return Promises. This warning indicates a misuse in an `async/await` context or an attempt to chain `.then()`/`.catch()` on a non-Promise return.fixEnsure `dnscache` methods are used with callbacks, e.g., `dnscache.lookup('host', (err, result) => { /* handle */ });`. If you need Promise-based DNS resolution, consider `node:dns/promises` or other modern alternatives.
Warnings
- breaking The `dnscache` package is abandoned, with its last publish date 7 years ago (April 22, 2019). It is not actively maintained, which poses security risks, compatibility issues with newer Node.js versions, and a lack of support for modern DNS features or practices.
- breaking This module alters the error handling behavior of Node.js's built-in `dns` functions. While original `dns` functions might throw errors in certain situations, `dnscache` intercepts these and passes all errors as the first argument to the callback function, standardizing error propagation. Developers expecting synchronous throws might encounter unexpected behavior.
- gotcha Setting a high `ttl` (Time-To-Live) for cache entries can lead to stale DNS information if the IP addresses for a domain change. This can result in your application attempting to connect to an old, incorrect IP, leading to connection errors or service unavailability.
Install
-
npm install dnscache -
yarn add dnscache -
pnpm add dnscache
Imports
- dnscache
import dnscache from 'dnscache';
const dnscache = require('dnscache'); - dns
import * as dns from 'dns';
const dns = require('dns'); - Configuration options
dnscache({ "enable": true, "ttl": 300, "cachesize": 1000 });
Quickstart
const dns = require('dns');
const dnscache = require('dnscache')({
"enable": true,
"ttl": 300,
"cachesize": 1000
});
console.log('DNS Caching enabled with TTL:', dnscache.ttl, 'and cache size:', dnscache.cachesize);
// Using the dnscache instance directly
dnscache.lookup('www.yahoo.com', function(err, result) {
if (err) {
console.error('Yahoo lookup error:', err.message);
return;
}
console.log('Lookup www.yahoo.com (via dnscache.lookup):', result);
});
// Using the wrapped 'dns' module
dns.lookup('www.google.com', function(err, result) {
if (err) {
console.error('Google lookup error:', err.message);
return;
}
console.log('Lookup www.google.com (via wrapped dns.lookup):', result);
});
// Example of a second lookup to demonstrate caching (though output won't show it directly)
setTimeout(() => {
dnscache.lookup('www.yahoo.com', function(err, result) {
if (err) {
console.error('Second Yahoo lookup error:', err.message);
return;
}
console.log('Second lookup www.yahoo.com (should be cached):', result);
});
}, 500);
// To prevent the process from exiting immediately for async operations
setTimeout(() => console.log('Exiting example.'), 2000);