lru cache

A cache object that deletes the least-recently-used items.


var LRU = require("lru-cache")
  , options = { max: 500
              , length: function (n) { return n * 2 }
              , dispose: function (key, n) { n.close() }
              , maxAge: 1000 * 60 * 60 }
  , cache = LRU(options)
  , otherCache = LRU(50) // sets just the max size

cache.set("key", "value")
cache.get("key") // "value"

cache.reset()    // empty the cache

If you put more stuff in it, then items will fall out.

If you try to put an oversized thing in it, then it’ll fall out right away.

Keys should always be Strings or Numbers

Note: this module will print warnings to console.error if you use a key that is not a String or Number. Because items are stored in an object, which coerces keys to a string, it won’t go well for you if you try to use a key that is not a unique string, it’ll cause surprise collisions. For example:

// Bad Example!  Dont' do this!
var cache = LRU()
var a = {}
var b = {}
cache.set(a, 'this is a')
cache.set(b, 'this is b')
console.log(cache.get(a)) // prints: 'this is b'