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

cashbox

v2.0.0

Published

javascript cache library with configurable storage

Downloads

21

Readme

browser support

Build Status

cashbox

javascript cache library with configurable storage


npm install cashbox

Cashbox provides a common caching api on top of pluggable backend stores. memory, redis and memcached are the currently supported stores, with memory being the default. Cashbox fully supports custom stores as long as they implement the correct api (see the memory store for an example of what to implement). Implementing tagging is optional (see the memcached store for an example of a store that doesn't support tags)

Using cashbox is as follows:

var Cashbox = require('cashbox');

// creates an in-memory cache by defaut
var cache = new Cashbox();

cache.set('myKey', 'myValue', function(err, wasSet) {

  cache.get('myKey', function(err, value) {
    console.log(value); //myValue
  });

});

// you can also set a ttl in seconds or a time string
cache.set('myKey', 'myValue', 10, function(err, wasSet) {

});

cache.set('myKey', 'myValue', '1 hour', function(err, wasSet) {

});

Cashbox API


Cashbox(config) constructor

The Cashbox constructor accepts an optional config object

  • type can be set to specify cache store type. memory is the default, redis and memcached are also supported
  • store can be set to either an instance, or a constructor for a backend cache store. The constructor will be passed the config object should it need any special configuration.
// a custom store instance, should implement the same api as the memory/redis/memcached store
var myCustomStore = new MyCustomStore();

// you can pass in the instance
var cache = new Cashbox({ store: myCustomStore });

// or the constructor - it will receive this same config object when instantiated
cache = new Cashbox({ store: MyCustomStore, foo: 'bar' });

.get(key, [load], [ttl], callback)

  • key is a string value used as the cache key
  • load - Optionally pass a load function that will be called upon a cache miss.
// provide a function to load a missing value
function load(key, cb) {
  //load value from db or w/e
  doSomethingAsync(key, function(err, value) {
    if(err) return cb(err);

    // cb() expects error first, then value.  Optionally an array of tags can be passed in third
    cb(null, value);
  });
}

cache.get(key, load, function(err, v) {
	// load() will have been called upon a cache miss
	console.log(v); // value returned from load()
});
  • ttl is also optional, and may be specified w/ a load function. Supported formats for ttl are either a value in seconds, or a time string parseable by timestr (i.e. "1 hour"). Omitting ttl will cause value to be cached indefinitely.
  • callback is called upon completion of fetching the value from the cache store. It is passed an error first, and the value. undefined is returned on cache misses

.get() can also be called by passing in an object with key, load, ttl, and done (callback) properties.

.set(key, value, [tags], [ttl], [callback])

  • key is a string value used as the cache key
  • value is what you want to store in the cache
  • tags is an optional array of string tags that can be used as a means of retreiving keys.
  • ttl is an optional time-to-live. Supported formats for ttl are either a value in seconds, or a time string parseable by timestr (i.e. "1 hour"). Omitting ttl will cause value to be cached indefinitely.
  • callback is a function called upon completion of set() method. It's passed an error first, and a boolean value indicating if the value was set successfully.
// set "myValue" in cache w/ the key of "myKey", tagged with "awesome", expires after 1 hour
cache.set('myKey', 'myValue', ['awesome'], '1 hour', function(err, wasSet) {

   console.log('myKey was cached');
});

.mget(keys, [load], [ttl], callback)

  • keys is an array of keys you want to fetch values for
  • load is an optional function called on cache misses that is passed an array of missing keys, and a callback function you should call after loading the keys. The callback function should be called with an error (or null if no error) first, array of values (ordered the same as the keys array), and an optional array of tags, each entry corresponding to the entry of the same index in the values array.
// in it's simplest form
// assuming the following is cached { one: '1', two: '2' }
cache.mget(['one', 'two'], function(err, values) {
  console.log(values); // [1, 2]
});

// assuming the following is cached { one: '1' }
cache.mget(['one, two'], loadEm, 60, function(err, values) {
  console.log(values); // [1, 2]

  // 'one' and 'two' are cached for 60 seconds, and 'two' is tagged with 'awesome'
});
// loadEm is called once, with an array of all missing keys
function loadEm(missingKeys, done) {
  console.log(missingKeys); // ['two']

  // load missing keys and pass values along to callback
  done(null, [2], ['awesome']);
}

.mset(keysValuesMap, [tags], [ttl], [callback])

  • keysValuesMap is an object of keys => values to cache
  • tags is an optional object map of keys => tags
  • ttl is an optional time-to-live. Supported formats for ttl are either a value in seconds, or a time string parseable by timestr (i.e. "1 hour"). Omitting ttl will cause value to be cached indefinitely.
  • callback is a function called once the values have been set. It's passed an error first, and a boolean indicating if the set operation was successful.
// mset with a 60 second ttl
cache.mset({ 'one':1, 'two':2 }, 60, function(err, wasSet) {
  console.log('my values are cached for 60 seconds');
});

// set tags for one key and a ttl for both
cache.mset({ 'one':1, 'two':2 }, { one: ['awesome', 'pawesome'] }), 60, function(err, wasSet) {
  console.log('values set, "one" tagged with awesome and pawesome');
});

.expire(key, [ttl], [callback])

Used to set ttl on a key, or expire it right away

  • key to expire / set ttl on
  • ttl optional ttl value to set for key. If omitted then key will be expired right away
  • callback function is called after expiration with an error first, then a boolean indicating if key was expired
cache.set('myKey', 'myValue', function(err, wasSet) {

  // myKey was set w/o a ttl, let's add one
  cache.expire('myKey', 60, function(err, expired) {
    console.log(expired); //true
    console.log('myKey now has a 60second ttl set');
  });

  // we could also just expire it right away
  cache.expire('myKey', function(err, expired) {
    console.log(expired); //true
    console.log('myKey is no longer cached');
  });
});

.getKeys(tags, callback)

  • tags array of string tags, or single tag string
  • callback function is called with an error first, and an array of keys that match the given tags. This keys returned are all keys that have any of the given tags.
cache.set('myKey', 'myValue', ['awesome'], function(err) {

  cache.getKeys('awesome', function(err, keys) {
    console.log(keys); // ['myKey']
  });

});