npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

request-easy-cache

v1.3.1

Published

Caching proxy for request.

Downloads

12

Readme

NPM version NPM downloads MIT License Build Status

request-easy-cache

A caching wrapper around request.

npm install --save request-easy-cache

request-easy-cache wraps calls to request in a layer that caches HTTP responses automatically. Multiple identical requests produce a cached response and body.

Caching is done in-memory using lru-cache, keyed on {url,options}. Calls with identical url and options return the same response and a cloned body.


Usage

Provided in ES6 for clarity.

import request from 'request-easy-cache';

function get(callback) {
	request.get('http://pokeapi.co/api/v1/pokemon/1', {
		json: true
	}, (err, res, body) => {
		callback(body);
	});
}

// simple test example
get( body => {

	let start = Date.now();
	get( body2 => {
		let time = Date.now() - start;

		assert.ok(time<10, 'Should be a cache hit (<10ms)');

		assert.deepEqual(body, body2, 'Should return the same data for a cached call');
	});
});

Note: In addition to request's options, request-easy-cache adds a {boolean} cache option that, when set to false, disables caching for the request.


Methods

.get(url, callback)

Fetch a URL using the default options.

Callback signature is identical to request's: (err, res, body).

.get(url, options, callback)

Fetch a URL with the specified options.

Supports all request options.

.get(options, callback)

Fetch via the specified options.

One of options.url or options.uri must be set.

Supports all request options.


Configuration

The default export is an instance of RequestEasyCache with all default options.

  • max: 100: Maximum number of responses to keep cached. Least-recently-used responses are dropped in favor of new ones.
  • maxAge: 60000: Maximum time to keep cached responses, in milliseconds. Defaults to 1 minute.

You can override the default settings:

requestEasyCache.enableCache({
	max: 100,		   // 100 cached responses
	maxAge: 60000	   // 60 seconds, in milliseconds
});

It is also possible to create customized instances of RequestEasyCache:

import { RequestEasyCache } from 'request-easy-cache';

let cachedRequest = new RequestEasyCache({
	cacheHttpErrors: false,		// don't cache 4xx/5xx errors
	cache: {
		max: 200,		// max number of responses to keep
		maxAge: 5*6e4	// 5 minute cache lifetime
	}
});

cachedRequest.get('...', (err, res, body) => {});

How It Works

Caching uses a compound key { url, options }. Calls with identical url and options return the same data.


Version History

  • 1.3.0 - Passthrough support for non-GET requests, added request(opts) method.
  • 1.2.0 - Support time strings for cache.maxAge via ms.
  • 1.1.1 - Added preferred support for .get(). .cached() is now an alias.
  • 1.1.0 - Updated to be simpler and more similar to the request API (retains backwards compatibility).
  • 1.0.0 - Forked from request-again, rewritten in ES6.