npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hangar

v3.1.0

Published

A lightweight Node.js application cache powered by LevelDB

Downloads

8

Readme

hangar

A lightweight Node.js application cache powered by LevelDB.

Build Status Code Climate

Install

NPM

Usage

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(err, value) {
    console.log(value);
  });
});

API

  • hangar.start()
  • hangar.stop()
  • hangar.drop()
  • hangar.get()
  • hangar.getMany()
  • hangar.set()
  • hangar.setMany()
  • hangar.setObject()
  • hangar.del()
  • hangar.delMany()

Hangar(options)

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([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:

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

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


drop([callback])

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


get(key, [callback])

Retrieve an entry from the cache

Example:

cache.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:

cache.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:

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) {...})


setMany(key, value, [callback])

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) {...})


setObject(obj, [callback])

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) {...})


del(key, [callback])

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


delMany(keys, [callback])

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


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