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

cache-out

v1.1.5

Published

A response caching tool for those slow/flaky services that power our express apps

Downloads

16

Readme

Coverage Status Build Status npm version Dependency Status

💶 💷 Cache Out 💵 💴

Cache out is simple tool to use in place of your service requests that will cache service responses for a specified time so all your users don't have to have a bad day because one of your services is.

It is backed by Redis so you will have to have a Redis instance running for this work. It also uses ES6, ioredis and returns a promise!

Install:

npm i cache-out --save

API:

cache-out(requestMethod, requestOptions, [redisOptions], [secondsToCache])

Calls your requestMethod with the requestOptions and caches the response in redis for the secondsToCache. If something goes wrong it's just a call through to your request.

| Param | Type | Default | Description | | --- | --- | --- | --- | | requestMethod | function | required | the function that you would be replacing cache-out with (ex request-promise-native) | | requestOptions | object | required | the options object you would be passing to the function above | | redisOptions | object | If no argument is passed a default ioredis client will be created | this is not required but if you want to specify you're redis setup (which you definitely are going to have to do at some point) then make sure it follows the options object specified for ioredis. Alternatively, you can pass in a pre instantiated redis client here too! Cache-out will use the passed in client if this is the case. | | secondsToCache | number | 86400 (which 24 hours in seconds) | The amount of time in seconds you want to cache the response in redis for |

Example usage:

const cacheOut = require('cache-out');
const request = require('request-promise');
const requestOpts = {url: 'http://www.url.com/uncool/why/', method: 'GET'};
const redisConfig = {port: 6379, host: 'localhost', db: 0, password: ''};

cacheOut(request, requestOpts, redisConfig, 604800)
  .then((res) => console.log('Wow, that was fast! ', res));
  
//
// This is also valid!
//

const cacheOut = require('cache-out');
const request = require('request-promise');
const requestOpts = {url: 'http://www.url.com/uncool/why/', method: 'GET'};
const ioredis = require('ioredis');
const redisClient = new ioredis({port: 6379, host: 'localhost', db: 0, password: ''});

cacheOut(request, requestOpts, ioredis, 604800)
  .then((res) => console.log('Nice, you used your own redis client! ', res));
  
//
// Don't worry, you can also use cache-out without ioredis!
//

const cacheOut = require('cache-out');
const request = require('request-promise');
const requestOpts = {url: 'http://www.url.com/uncool/why/', method: 'GET'};
const redis = require('redis');
const redisClient = redis.createClient({port: 6379, host: 'localhost', db: 0, password: ''});

cacheOut(request, requestOpts, ioredis, 604800)
  .then((res) => console.log('Nice, you are not using ioredis! ', res));

Testing:

I recommend using a mock redis client and passing this into cache-out if your NODE_ENV === 'test'.

Thanks! 🐕

nice code