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

cacherole

v1.0.0

Published

A fluid interface for automagical in-memory caching.

Downloads

3

Readme

cacherole

A fluid interface for automagical in-memory caching.

Installation

npm install cacherole

Usage

cacherole is an abstraction over the get and put functionality of a traditional cache. It expects to do work in the form of an action and takes care of the caching behind-the-scenes.

You only need to provide a key and your typical arguments in the form of: cachedAction(key)(actionArguments)

const cacherole = require('cacherole');

// Let's define an action

const list = (...items) => {
  console.log('listing!');
  return items;
};

list('hello', 'there');
//=> 'listing!'
//=> ['hello', 'there']

// Put the action function in the cacherole!

let cachedList = cacherole.put(list);

// Storing values in the cache with a key

cachedList('fruits')('apple', 'mango', 'banana');
//=> 'listing!'
//=> ['apple', 'mango', 'banana']

// Retrieving values from the cache with a key

cachedList('fruits')('apple', 'mango', 'banana');
//=> ['apple', 'mango', 'banana']

// or if you're sure...

cachedList('fruits')();
//=> ['apple', 'mango', 'banana']

// it's okay to make mistakes!

cachedList('fruits')('a-apple', 'ummm... tomato!', 'broccoli?');
//=> ['apple', 'mango', 'banana']

// your action executes only when the key is not in the cache

// Updating a stored value in the cache

cachedList.update('fruits')('melon', 'blueberries', 'passionfruit');
//=> 'listing!'
//=> ['melon', 'blueberries', 'passionfruit']

More Features

// More features with object syntax!
// action - function to cache
// time - time until each item is removed, default is forever
// binding - object for binding to the action function if needed

cachedList = cacherole.put({
  action: list,
  time: 1000, // time to live in milliseconds
  binding: null
});

// Setting a timeout callback to execute when the new value is removed from the cache

const alert = (key, value) => console.log('the ' + key + ' have spoiled!');

cachedList('perishables', alert)('meat', 'fish', 'dairy');
//=> 'listing!'
//=> ['meat', 'fish', 'dairy']

// ...after a "while"
//=> 'the perishables have spoiled!'

// Using the internal cache

const internal = cacherole.cache;

internal.get('perishables');
//=> null

internal.put('fats', ['olive oil', 'coconut oil']);
//=> ['olive oil', 'coconut oil']

internal.del('fats');
//=> true

internal.get('fats');
//=> null

internal.clear(); // clears the cache

// Creating a new instance with the Cacherole constructor

const newCacherole = new cacherole.Cacherole();

newCacherole.cache !== cacherole.cache;
//=> true

For more documentation, see the unit tests under the test directory.

Notes

For the full internal cache API see the memory-cache documentation, as it is the cache implementation used in cacherole.

For development make sure to run all unit tests via Mocha with npm test after cloning the repo and installing all dependencies.

License

cacherole is licensed under the MIT license.