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-promise-cache

v2.0.1

Published

request promise with cache

Downloads

2,043

Readme

request-promise-cache

Request promise with cache

2.0.0 Breaks backward compatibility

The resolved first argument is no longer {response, body, ?error} but just the body. But, you can pass in resolveWithFullResponse=true to the request({..params}) to get the full response object instead of the body.

Other promise libraries?

By default, this module uses the native javascript Promise introduced in Node.js 0.12+, however you can use it with others, by passing your own Promise constructor


// if you want to use bluebird for example
// just do this once, somewhere in your app, ideally whatever file loads first, i.e. app.js
var request = require('request-promise-cache').use( require('bluebird').Promise )

// you dont have to do it again in the same app's other files

Tested with

if you want me to test another one, just add it and make a pull request to the promiseTypes

Usage

var request = require('request-promise-cache');

var query = { recordId: 27 };

var queryString = Object.keys(query).sort().map(function (k) { return k + '=' query[k] }).join('&');
var cacheKey = url + '?' + queryString;

var url = 'http://google.com';
request({
    url: url,
    cacheKey: url,
    cacheTTL: 3600,
    cacheLimit: 12,
    /* bust the cache and get fresh results
    qs: query || {
        _: +new Date()
    },
    */
    // like https://github.com/request/request-promise#get-the-full-response-instead-of-just-the-body
    resolveWithFullResponse: false,
  })
  .then(function(body) {
    // ...
  })
  .catch(function(error) {
    // ...
  });

Options

All of the original request library's options, plus the following:

  • cacheKey: string, the cache key use, typically, it's just the URL, maybe add the query string
  • cacheTTL: milliseconds, automatically expire a cache entry after Y number of milliseconds, if used with cacheLimit, whichever comes first will take precedence
  • cacheLimit: integer, automatically expire a cache entry after X amount of reads, if used with cacheTTL, whichever comes first will take precedence
  • fresh: true/false, delete the cached entry and get a fresh one
  • qs._: 123456789 /* anything truthy */, same as fresh however, this query param will be sent over to the remote server, so it will, most likely, bypass the cache on the other end if there is one
  • resolveWithFullResponse: true/false, copied from request-promise options, defaults to false, basically instead of resolving with the body, it uses the response, which then you need to do response.body to access the body

Asynchronous calls with the same cacheKey

If you make 2 or more requests with the same cacheKey at the same time, and of course, the response comes back within the cacheTTL of the first request, only 1 request will go out, the rest will wait for it and resolve at the same time.

Extras

On the returned request object, you can:

  • request.original access the original request function,
  • request.defaults() another request object function generator, which is used exactly like the original request.defaults but this one will return a promisified request with the caching.
  • request.cache is the cache instance using, which is a nano-cache instance, say you need to request.cache.clear()

License

MIT