Home

Awesome

cache-service-node-cache

Features

Basic Usage

Add to your package.json. node-cache is a peer dependency of cache-service-node-cache. This repo is currently tested against node-cache#4.x.

  ...
  "dependencies": {
    "cache-service-node-cache": "2.x",
    "node-cache": "4.x"
  },
  ...

Require and instantiate

var csNodeCache = require('cache-service-node-cache');

var cacheModuleConfig = {defaultExpiration: 60};
var nodeCache = new csNodeCache(cacheModuleConfig);

Cache!

nodeCache.set('key', 'value');

Cache Module Configuration Options

cache-service-node-cache's constructor takes an optional config object with any number of the following properties:

type

An arbitrary identifier you can assign so you know which cache is responsible for logs and errors.

defaultExpiration

The expiration to include when executing cache set commands. Can be overridden via .set()'s optional expiration param.

backgroundRefreshInterval

How frequently should all background refresh-enabled keys be scanned to determine whether they should be refreshed. For a more thorough explanation on background refresh, see the Using Background Refresh section.

backgroundRefreshMinTtl

The maximum ttl a scanned background refresh-enabled key can have without triggering a refresh. This number should always be greater than backgroundRefreshInterval.

backgroundRefreshIntervalCheck

Whether to throw an exception if backgroundRefreshInterval is greater than backgroundRefreshMinTtl. Setting this property to false is highly discouraged.

verbose

When used with cache-service, this property is overridden by cache-service's verbose value.

When false, cache-service-node-cache will log only errors. When true, cache-service-node-cache will log all activity (useful for testing and debugging).

API

Although this is a node-cache wrapper, its API differs in some small cases from node-cache's own API because all cache-service-compatible cache modules match cache-service's API.

.get(key, callback (err, response))

Retrieve a value by a given key.

.mget(keys, callback (err, response))

Retrieve the values belonging to a series of keys. If a key is not found, it will not be in response.

.set(key, value, [expiration], [refresh(key, cb)], [callback])

See the Using Background Refresh section for more about the refresh and callback params.

Set a value by a given key.

.mset(obj, [expiration], [callback])

Set multiple values to multiple keys

This function exposes a heirarchy of expiration values as follows:

.del(keys, [callback (err, count)])

Delete a key or an array of keys and their associated values.

.flush([cb])

Flush all keys and values from.

.db

This is the underlying node-cache instance. If needed, you can access node-cache functions I haven't abstracted.

Using Background Refresh

With a typical cache setup, you're left to find the perfect compromise between having a long expiration so that users don't have to suffer through the worst case load time, and a short expiration so data doesn't get stale. cache-service-cache-module eliminates the need to worry about users suffering through the longest wait time by automatically refreshing keys for you. Here's how it works:

How do I turn it on?

By default, background refresh is off. It will turn itself on the first time you pass a refresh param to .set().

Configure

There are three options you can manipulate. See the API section for more information about them.

Use

Background refresh is exposed via the .set() command as follows:

cacheModule.set('key', 'value', 300, refresh, cb);

If you want to pass refresh, you must also pass cb because if only four params are passed, cache-service-node-cache will assume the fourth param is cb.

The Refresh Param

refresh(key, cb(err, response))

The refresh param MUST be a function that accepts key and a callback function that accepts err and response as follows:

var refresh = function(key, cb){
  var response = goGetData();
  cb(null, response);
}