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

memory-cacheable

v1.2.2

Published

minimal in memory completely volatile cache store with a "stale concept"

Downloads

1

Readme

cacheable

I built this because I had the need to store expensive and frequently called functions into memory. It's not a framework for caches, it's not really a class, or a cache wrapper. It's minimal, and encourages you to decide how you want to interact with the available states of your cached collections. Think of this as a function that keeps state of points in time, with a couple helpers to manage cached collections.

install

npm install --save memory-cacheable

api

cacheable({ key, cache, ttl, offset, verbose })

see below table for options:

| key | required | summary | | --- | --- | --- | | key | false | key for debugging purposes, mostly | | ttl | false | defaults to 1 hour, uses the ms module for easy formatting | | cache | false | defaults to an array, but will also support cleaning out objects | | offset | false | defaults to 0.65 (roughly 2/3 the ttl), used to create the stale value by multiplying the ttl by offset | | verbose | false | defaults to false turn this on for really noisey cache stats and debugging |

cacheable api

| function | summary | | --- | --- | | cacheable(store).debug() | debug logging, noisey, expensive stuff | | cacheable(store).expired() | check if cache state is expired | | cacheable(store).fetch() | fetches the cached collection | | cacheable(store).flush() | flush cache, supports Objects and Arrays | | cacheable(store).set(collection) | sets cached collection from provided param | | cacheable(store).size() | gets the cache size, if an object Object.keys() is called | | cacheable(store).stale() | determines if the cache is stale or not | | cacheable(store).state() | returns whether the cache is expired or not, if cache is expired, state will reset and flush the cache | | cacheable(store).reset() | restarts timers, will not flush cache |

example

const { cacheable } = require('memory-cacheable');

const store = {
  key: 'expensiveCacheExample',
  ttl: '1h'
};

const expired = cacheable(store).state; // pulls state out to manage state and check if expired
const { stale, reset, flush } = cacheable(store); // other helpful tings

// simplest example usage
function doSomethingExpensive(fn) {
  if (!expired()) return fn(null, store.cache);
  return funcToExpensiveThings((err, resp) => {
    if (err) return fn(new Error(err), null);
    store.cache = resp;
    return fn(null, resp);
  });
}

// example usage for expensive or long running functions with 
// stale support
function doSomethingExpensive(fn) {
  const isStale = stale();
  if (!expired() && !isStale) return fn(null, store.cache);
  if (isStale) fn(null, store.cache);
  return funcToExpensiveThings((err, resp) => {
    if (err) return fn(new Error(err), null);
    store.cache = resp;
    if (isStale) return reset();
    return fn(null, resp);
  });
}

debugging

Uses the debug module, and you can set the following environment variable:

export DEBUG=cacheable:*

test

npm install --save-dev && npm test

Made with ♥