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

lru-pool

v0.0.2

Published

A keyed pool that recycles the least-recently-used objects.

Downloads

10

Readme

lru pool

A keyed pool that recycles the least-recently-used objects. Requires Node.js v0.12.x or greater for Symbol and Map types.

Circle CI codecov.io

Usage

var LRU = require("lru-pool");
var options = {
  create: function(callback) {
    callback(null, new Object());
  },
  init: function(key, obj, callback) {
    obj.load(key);
    callback(null, obj);
  },
  destroy: function(key, obj) {
    obj.destroy();
  },
  max: 500,
  maxAge: 1000 * 60 * 60,
  allowStale: true,
  destroyStale: true
};
var pool = new LRU(options);

pool.acquire("key", function(err, key, obj) {
  pool.release(obj);
  // OR pool.destroy(obj);
});

// Non-string keys ARE fully supported
var pool = new LRU({
  create: function(callback) { callback(null, {}) },
  init: function(key, obj, callback) {
    obj.type = typeof key;
    callback(null, obj);
  }
});

var someObject = {};
pool.acquire(someObject, function(err, key, obj) {
  pool.release(obj);
  
  pool.acquire('[object Object]', function(err, key, obj) {
    assert.equal(obj.type, 'string');
    pool.release(obj);
    
    pool.acquire(someObject, function(err, key, obj) {
      assert.equal(obj.type, 'object');
    });
  });
});

If you put more stuff in it, then least recently used objects will be recycled.

Options

  • create [Required] Function to construct new objects for the pool. Pass the constructed object to the callback in err, obj form.

  • init [Optional] Function called with key, obj, callback on objects when they are created or recycled. This can be used to customize an object for the given key. callback(err) will pass an error to the acquire callback, while callback(null, obj) will pass the initialized object through.

  • destroy [Optional] Function called on objects when they are dropped from the pool (with pool.destroy(obj) or because they are stale). This can be handy if you want to close file descriptors or do other cleanup tasks when objects are no longer accessible. Called with key, obj after removing the object from the internal cache.

  • max [Optional] The maximum size of the pool. Not setting this is kind of silly, since that's the whole purpose of this lib, but it defaults to Infinity.

  • maxAge [Optional] Maximum age in milliseconds. Objects are not pro-actively pruned out as they age, but if you try to acquire an object that is too old, the pool will instead return a newly initialized object.

  • allowStale [Optional] If you try to acquire a stale object, the default behavior is to return a newly initialized object, leaving the stale object in the pool to be recycled. If you set allowStale: true, the pool will return the stale object and then recycle it when it is released back to the pool.

  • destroyStale [Optional] The default behavior is to recycle stale objects on acquire (unless allowStale: true is set) and on release. If you set destroyStale: true, stale objects will be destroyed instead of being returned to the pool to be recycled.

API

  • acquire(key, function(err, key, obj) {})

    This will update the "recently used"-ness of the object, unless it is stale.

    If the key matches an available object, the object will be passed to the callback directly, without passing through the optional init function. If the key is not found (no matching objects or all matching objects are busy) and the current size of the pool is smaller than the max size, a new object will be created and passed through the init function, then passed on to the callback. If the key is not found and the pool is already at max capacity, the least recently used object will be passed through the init function then passed to the callback.

    The acquired object will be flagged as busy and unavailable until it has been released back to the pool.

    The key and object can be any value.

  • release(obj)

    Releases the object back to the pool so that it will be available for future calls to acquire.

  • destroy(obj)

    Destroys a damaged object instead of releasing it back to the pool.

  • length

    Returns total quantity of objects currently in pool. Includes both available and busy objects. Note, that stale (see options) objects are returned as part of this count.