/cache-wrapper

Wraps objects with a proxy that caches return values so that resource intensive functions can be called repeatedly at low performance cost.

Primary LanguageJavaScript

Build Status Dependency Status npm version

Installation

npm install cache-wrapper

Usage

The example below shows how you can use cache-wrapper to iteratively wrap each function in a resource intensive/slow data store object, so that repeat calls to its methods with the same arguments will use a cached response from the first call.

var cacheWrapper = require('cache-wrapper');
var dataStore = {
    getThing: function (id) {
        // Do something resource intensive
    }
};

var proxiedDataStore = cacheWrapper.wrap(dataStore);
var thing = proxiedDataStore.getThing(1);

Alternatively, you may wrap individual functions for more fine-grained control.

var proxiedGetThing = cacheWrapper.wrap(dataStore.getThing);
var thing = proxiedGetThing(1);

Cache Expiration

A TTL can be set on caches to ensure that cached data doesn't go stale.

var proxiedDataStore = cacheWrapper.wrap(dataStore, { ttl: 5000 }); // Keep for 5000ms