Skip to content

thody/cache-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published