What is cacheable-request?
The cacheable-request npm package is a wrapper for the native HTTP/HTTPS request functionality in Node.js, providing an easy-to-use cache layer for repeat requests. It is designed to cache responses based on their Cache-Control header and supports the caching of GET and HEAD requests.
What are cacheable-request's main functionalities?
Caching GET and HEAD requests
This feature allows you to cache GET and HEAD requests. The code sample shows how to create a cacheable request using the cacheable-request package with a custom cache adapter.
const CacheableRequest = require('cacheable-request');
const http = require('http');
const cacheAdapter = require('./some-cache-adapter');
const cacheableRequest = new CacheableRequest(http.request, cacheAdapter);
const req = cacheableRequest('http://example.com', response => {
// Use the response here
});
req.on('request', request => request.end());
Custom cache adapters
This feature allows the use of custom cache adapters to store and retrieve cached responses. The code sample demonstrates how to use a custom cache adapter with the cacheable-request package.
const CacheableRequest = require('cacheable-request');
const http = require('http');
const myCacheAdapter = {
get: (key) => { /* custom get implementation */ },
set: (key, value) => { /* custom set implementation */ }
};
const cacheableRequest = new CacheableRequest(http.request, myCacheAdapter);
HTTP and HTTPS support
The package supports both HTTP and HTTPS protocols. The code sample illustrates how to create a cacheable HTTPS request.
const CacheableRequest = require('cacheable-request');
const https = require('https');
const cacheAdapter = require('./some-cache-adapter');
const cacheableRequest = new CacheableRequest(https.request, cacheAdapter);
const req = cacheableRequest('https://secure.example.com', response => {
// Use the response here
});
req.on('request', request => request.end());
Other packages similar to cacheable-request
axios-cache-adapter
axios-cache-adapter is a cache adapter for the Axios HTTP client. It allows you to easily add caching capabilities to your Axios requests. Unlike cacheable-request, which is a standalone request wrapper, axios-cache-adapter is specifically designed to work with Axios and provides more advanced features like cache invalidation and per-request cache configuration.
node-fetch-cache
node-fetch-cache is a caching layer for node-fetch, a lightweight module that brings window.fetch to Node.js. It provides similar caching functionality to cacheable-request but is tailored for use with node-fetch. It also offers features like cache size limits and custom cache keys.
got
got is a powerful HTTP request library for Node.js that includes caching as one of its features. It is more feature-rich than cacheable-request, offering a wide range of HTTP capabilities such as retries, streams, and more. got's caching is built-in and does not require a separate adapter, making it a more integrated solution.