Git Product home page Git Product logo

hangar's Introduction

hangar

A lightweight Node.js application cache powered by LevelDB.

Build Status Dependencies

NPM

Install

npm install hangar --save

Usage

var Hangar = require('hangar');
var h = new Hangar({ location: './db' });

h.start(function (err) {
  if (err) console.error(err);
});

h.set('foo', { k: 'v' }, function (err) {
  if (err) console.error(err);
  h.get('foo', function (err, value) {
    console.log(value);
  });
});

h.stop(function (err) {
  if (err) console.error(err);
});

API

Hangar(options)

Create a hangar instance

Example:

var Hangar = require('hangar');
var h = 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
  • [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([callback])

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:

h.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([callback])

Stop and close the connection to the cache

Example:

h.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

drop([callback])

Stop, close, and destroy the cache contents

Example:

h.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

get(key, [callback])

Retrieve an entry from the cache

Example:

h.get('foo', function (err, value) {
  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

getMany(keys, [callback])

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

Example:

h.get(['foo', 'bar'], function (err, values) {
  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(key, value, [callback])

Set an entry in the cache

Example:

h.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

setMany(key, value, [callback])

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

Example:

h.setMany(['k1', 'k2'], ['v1', 'v2'], function (err) {
  h.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

setObject(obj, [callback])

Populate the cache with properties of an object literal

Example:

var multi = { 'k1': 'v1', 'k2': 'v2' }

h.setObject(multi, function (err) {
  h.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

del(key, [callback])

Remove an entry from the cache

Example:

h.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

delMany(keys, [callback])

Remove multiple entries from the cache

Example:

h.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

Tests

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

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.