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

zb-promise-cache

v1.0.1

Published

A cache object that can cache result of a promise for certain period of time

Downloads

6,462

Readme

zb-promise-cache

A cache object that can cache result of a promise for certain period of time

Simple usage


const PromiseCache = require('zb-promise-cache');


const simplePromise = function() {
  return new Promise((resolve, reject) => {
    resolve('hello world');
  });
}

const promiseCache = new PromiseCache(simplePromise);

p.get('testing')
  .then(function(val) {
    console.log(val); // 'hello world'

    // At this point 'hello world' is already cached
    // into 'promiseCache' object. Next time you call
    // p.get('testing'), the value will be retrieved
    // from cache.
  });

Advanced usage


const PromiseCache = require('zb-promise-cache');


const advancedPromise = function(someValue) {
  return new Promise((resolve, reject) => {
    resolve('hello world ' + someValue);
  });
}

const promiseCache = new PromiseCache(advancedPromise);

p.get('advanced testing', [1])
  .then(function(val) {
    console.log(val); // 'hello world 1'

    // At this point 'hello world 1' is already cached
    // into 'promiseCache' object with key 'advanced testing'. Next time you call
    // p.get('advanced testing', '2'), the value will be retrieved from cache
    // and will resolve 'hello world 1' in stead of 'hello world 2' unless cache expired.
  });

const PromiseCache = require('zb-promise-cache');


const advancedPromise = function(someValue, anotherValue) {
  return new Promise((resolve, reject) => {
    const sum = someValue + anotherValue;
    resolve('hello world ' + sum);
  });
}

const promiseCache = new PromiseCache(advancedPromise);

p.get('advanced testing', [2, 3])
  .then(function(val) {
    console.log(val); // 'hello world 5'

    // At this point 'hello world 5' is already cached
    // into 'promiseCache' object with key 'advanced testing'. Next time you call
    // p.get('advanced testing', [1111, 2222]), the value will be retrieved from cache
    // and will resolve 'hello world 5' in stead of 'hello world 3333' unless cache expired.
  });

Options


const PromiseCache = require('zb-promise-cache');


const simplePromise = function() {
  return new Promise((resolve, reject) => {
    resolve('hello world');
  });
}

const options = {
  'max': 200 // Max lru cache size, default INVINITY
  'maxAge': 100000 // Max cache life time in ms, default, INVINITY
  'debug': true // Display debug msg in console
};

const promiseCache = new PromiseCache(simplePromise, options);

p.get('testing')
  .then(function(val) {
    console.log(val); // 'hello world'

	// Stored in lru cache with max cache size of 200
    // cache life time 100000ms
    // and showing debug logs in console
  });

Functions

/**
 * Get the cached value with cacheKey.
 * If cacheKey does not have value in lru cache,
 * promise will be called with params distributed to the promise called.
 * Value resolved from promise will be stored in lru cache with cacheKey
 * provided and resolved in this function at the same time.
 * @param {Promise} cacheKey -cacheKey that is going to be get the value.
 * @param {array} params - An array of params that will fit into the promise
 *                         in order to retrieve value to be cached when
 *                         there is a cache miss.
 */
get(cacheKey, params)



/**
 * Manually set a value into cacheKey.
 * @param {string} cacheKey - cacheKey that is going to be set with value.
 * @param {any} value - value to be set into cacheKey, default ''.
 */
set(cacheKey, value)



/**
 * Clear all caches.
 */
clear()