hangar

A lightweight Node.js application cache powered by LevelDB

hangar

A lightweight Node.js application cache powered by LevelDB.

var Hangar = require('hangar');
var cache = new Hangar({ location: './cache' });
 
cache.start(function(err) {
  if (err) console.error(err);
});
 
cache.set('foo', { k: 'v' }, function(err) {
  if (err) console.error(err);
  cache.get('foo', function(errvalue) {
    console.log(value);
  });
});

Create a hangar instance

Example:

var Hangar = require('hangar');
var cache = new Hangar(options);

options:

  • location - The location for the backing datastore. note: location is required
  • [ttl] - The time-to-live for cache entries. default: 4 hours
  • [checkFrequency] - The frequency to check TTL values. default: 5 minutes
  • [cacheSize] - The size of the in-memory LRU cache: default: 8MB
  • [keyEncoding] - The encoding to use for the keys. default: utf8 (string)
  • [valueEncoding] - The encoding to use for the values. default: json

Parameters

[options] Object - A set of options to use in the cache


Start and open a connection to the cache. Note, start() is an asynchronous operation. Whether you provide an error-handling callback or not, all read/write operations will be queued until the backing datastore is fully opened.

Example:

cache.start(function(err) {
  console.error(err);
});

Parameters

[callback] Function - The callback will receive an error as the first parameter if the cache cannot be started


Stop and close the connection to the cache

Example:

cache.stop(function(err) {
  console.error(err);
});

Parameters

[callback] Function - The callback will receive an error as the first parameter if the cache cannot be started


Stop, close, and destroy the cache contents

Example:

cache.drop(function(err) {
  console.error(err);
});

Parameters

[callback] Function - The callback will receive an error as the first parameter if the cache cannot be started


Retrieve an entry from the cache

Example:

cache.get('foo', function(errvalue) {
  if (err) {
    console.error(err);
  }
  console.log(value);
});

Parameters

key String - The key to lookup

[callback] Function - The callback will receive an error as the first parameter if value cannot be found, or the value as the second paramater if it was found


Retrieve entries from the cache. Order of key index will be maintained.

Example:

cache.get(['foo', 'bar'], function(errvalues) {
  if (err) {
    console.error(err);
  }
  console.log(values);
});

Parameters

keys Array - The keys to lookup

[callback] Function - The callback will receive an error as the first parameter if all of the values cannot be found, or all of the values as the second paramater if they were found


Set an entry in the cache

Example:

cache.set('foo', 'bar', function(err) {
  if (err) {
    console.error(err);
  }
});

Parameters

key String - The key to set

value Object - The value to set

[callback] Function - The callback will receive an error as the first parameter if the key cannot be set or the value that was set as the second parameter (function((null, value) {...})


Set multiple entries in the cache. Order of key and value index will be maintained.

Example:

cache.setMany(['k1', 'k2'], ['v1', 'v2'], function(err) {
  cache.getMany(['k1', 'k2'], function(values) {
    console.log(values); //=> ['v1', 'v2'] 
  });
});

Parameters

key Array - The keys to set

value Array - The values to set

[callback] Function - The callback will receive an error as the first parameter if all of the keys cannot be set or the values that were set as the second parameter (function((null, values) {...})


Populate the cache with properties of an object literal

Example:

var multi = { 'k1': 'v1', 'k2': 'v2' }
 
cache.setObject(multi, function(err) {
  cache.getMany(['k1', 'k2'], function(values) {
    console.log(values); //=> ['v1', 'v2'] 
  });
});

Parameters

obj Object - The key/values to set in object literal form

[callback] Function - The callback will receive an error as the first parameter if all of the keys cannot be set or the object values that were set as the second parameter (function((null, object.values) {...})


Remove an entry from the cache

Example:

cache.del('foo', function(err) {
  if (err) {
    console.error(err);
  }
});

Parameters

key String - The key of the entry to remove

[callback] Function - The callback will receive an error as the first parameter if all of the keys cannot be removed


Remove multiple entries from the cache

Example:

cache.delMany(['foo', 'bar'], function(err) {
  if (err) {
    console.error(err);
  }
});

Parameters

keys Array - The keys of the entries to remove

[callback] Function - The callback will receive an error as the first parameter if all of the keys cannot be removed


Linting is done through jshint with settings from ./.jshintrc. This happens automatically as part of the pretest script when running tests.

Tests are written with tape and can be run through the npm test script.

$ npm test

MIT