Skip to content

gschwa/bluebird-cachify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bluebird Cachify Build Status Gitter

A non-invasive way to cache your Bluebird promises. It leverages lru-cache-plus to cache the results.

Quick Start

var Promise = require('bluebird');
var BluebirdCachify = require('bluebird-cachify')();


// assume you have an instance 'sample' with a function named 'foo' that returns a promise 
BluebirdCachify.cachify(sample, 'foo', 5000); // cache for 5 seconds

sample.foo(10) 
  .then(function (result) {
    return sample.foo(10); // returns cached result
  })

tl; dr

Multiple Caches and Configuration

By default, BluebirdCachify uses a single cache instance to store results. It may be desirable to have multiple caches with different parameters and default TTLs. This can easily be done. Examples:

// using a different namespace
var BluebirdCachify = require('bluebird-cachify')('different namespace');

// setting default options
var BluebirdCachify = require('bluebird-cachify')({max: 1000, maxAge: 8000});

// creating a new namespace with custom options
var BluebirdCachify = require('bluebird-cachify')('namespace2', {max: 1000, maxAge: 8000});

See: lru-cache-plus for possible options and documentation

API

#####require('bluebird-cachify')([String namespace] [, Object options]) -> BluebirdCachify

Creates or returns a BluebirdCachify object for the given namespace. Without the optional namespace the default is returned. Optionally, an options object can be provided to customize the cache. The default options are: {max: 500, maxAge: 3600000} (see: lru-cache-plus)

#####BluebirdCachify.cachify(Object instance, String functionName [, Number ttl])

Caches the given functionName for the given instance. Optionally, pass ttl in milliseconds.

#####instance.function.restore()

Removes caching from the function.

About

Easily cache Bluebird promies using lru-cache

Resources

License

Stars

Watchers

Forks

Packages

No packages published