DNS Cache for Node.js

1.0.2 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to enable `dnscache` and use both the `dnscache` instance and the wrapped `dns` module for hostname lookups, showcasing the transparent caching mechanism.

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);

view raw JSON →