Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
cacheable-lookup
Advanced tools
The cacheable-lookup npm package is designed to enhance the Node.js DNS module with caching capabilities. It provides a way to cache DNS lookup results in order to improve performance for repeated DNS queries. This is particularly useful for applications making numerous requests to the same domains, as it reduces the number of DNS queries that need to be performed over the network.
Caching DNS lookups
This feature allows you to cache DNS lookups to improve performance. The code sample demonstrates how to perform a DNS lookup for 'example.com' and cache the result.
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();
cacheable.lookup('example.com', (err, address, family) => {
console.log(address);
});
Integration with http.Agent
This feature demonstrates how cacheable-lookup can be integrated with Node.js http.Agent to automatically use cached DNS lookups for HTTP requests. This can significantly reduce DNS lookup times for repeated requests to the same domain.
const CacheableLookup = require('cacheable-lookup');
const http = require('http');
const cacheable = new CacheableLookup();
const agent = new http.Agent({
lookup: cacheable.lookup
});
http.get('http://example.com', { agent }, (res) => {
// Handle response
});
dns-cache is a simple DNS cache module. It provides basic caching functionalities similar to cacheable-lookup but lacks some of the advanced features and customizability options such as integration with http.Agent.
A cacheable
dns.lookup(…)
that respects TTL :tada:
Making lots of HTTP requests? You can save some time by caching DNS lookups.
Don't worry, this package respects TTL :smiley:
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();
http.get('https://example.com', {lookup: cacheable.lookup}, response => {
// Handle the response here
});
Returns a new instance of CacheableLookup
.
Type: Object
Default: {}
Options used to cache the DNS lookups.
Type: Keyv adapter instance
Default: new Map()
A Keyv adapter which stores the cache.
Type: number
Default: Infinity
Limits the cache time (TTL).
If set to 0
, it will make a new DNS query each time.
Type: Function
Default: new dns.Resolver()
An instance of DNS Resolver used to make DNS queries.
Type: Object
Type: string
The IP address (can be an IPv4 or IPv6 address).
Type: number
The IP family (4
or 6
).
Type: Array
DNS servers used to make the query. Can be overridden - then the new servers will be used.
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
.
Type: string
Type: Object
The same as the dns.lookup(…)
options.
Type: boolean
Default: false
Throw when there's no match.
If set to false
and it gets no match, it will return undefined
.
Note: This option is meant only for the asynchronous implementation! The synchronous version will always give an error if no match found.
Type: boolean
Default: false
If true
the entries returned by lookup(…)
and lookupAsync(…)
will have additional expires
and ttl
properties representing the expiration timestamp and the original TTL.
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.
An asynchronous function which makes a new DNS lookup query and updates the database. This is used by query(hostname, family)
if no entry in the database is present.
Returns an array of objects with address
, family
, ttl
and expires
properties.
See the benchmarks (queries localhost
, performed on i7-7700k):
CacheableLookup#lookupAsync x 219,396 ops/sec ±0.69% (285 runs sampled)
CacheableLookup#lookup x 219,296 ops/sec ±0.20% (284 runs sampled)
CacheableLookup#lookupAsync - zero TTL x 27.67 ops/sec ±51.06% (246 runs sampled)
CacheableLookup#lookup - zero TTL x 29.64 ops/sec ±49.60% (204 runs sampled)
dns#resolve4 x 27.03 ops/sec ±41.64% (246 runs sampled)
dns#lookup x 5,994 ops/sec ±0.26% (285 runs sampled)
Fastest is CacheableLookup#lookup
The package is based on dns.resolve4(…)
and dns.resolve6(…)
.
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).
MIT
FAQs
A cacheable dns.lookup(…) that respects TTL
The npm package cacheable-lookup receives a total of 7,228,792 weekly downloads. As such, cacheable-lookup popularity was classified as popular.
We found that cacheable-lookup demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.