user3649361 user3649361 - 3 years ago 111
Node.js Question

LRU Cache in Node js

I need to implement caching for my project(for my organisation), we are planing to have a in-memory LRU caching, I have got some packages but I am not sure about the licensing term, the best I found was this

https://www.npmjs.com/package/lru-cache

but here I am facing some issues when I declare my cache as

var LRU = require("lru-cache")
, options = { max: 2
, length: function (n, key) { return n * 2 + key.length }
, dispose: function (key, n) { n.close() }
, maxAge: 1000 * 60 * 60 }
, cache = LRU(options)
console.log(cache.length)
cache.set(1,1)
cache.set(2,2)
cache.set(3,3)
console.log(cache.length)
console.log(cache.get(1))
console.log(cache.get(2))
console.log(cache.get(3))
console.log(cache)


The output which I get for above code is

0
NaN
1
2
3
LRUCache {}


It doesn't set the max value and it's seems to be infinity
Even If the length is 2 it is not removing the LRU element and adding all three element in the cache

Is any other package available ?, I am also thinking to implement my own caching mechanism, what is the best practice for node js.

Answer Source

Let's modify a little bit your code so I can explain better what is wrong.

   var LRU = require("lru-cache")
  , options = { max: 2
              , length: function (n, key) { return n * 2 + key.length }
              , dispose: function (key, n) { n.close() }
              , maxAge: 1000 * 60 * 60 }
  , cache = LRU(options)
  console.log(cache.length)
  cache.set(1,10) // differentiate the key and the value
  cache.set(2,20)
  cache.set(3,30)
  console.log(cache.length)
  console.log(cache.get(1))
  console.log(cache.get(2))
  console.log(cache.get(3))
  console.log(cache)

The length function is called every time you set a value in the cache. When you call cache.set(1,10), the function length you defined earlier has as parameters: n (the number 10) and key (the number 1).

So you see here that key.length is undefined because a number doesn't have a length property and a sum with undefined will be NaN. In the documentation the author uses the property length because in general the cache key is a string. You can of course use a number as key but that's what breaks here.

After fixing this issue, you have to pay attention to the function dispose. I quote the author:

dispose: Function that is called on items when they are dropped from the cache. This can be handy if you want to close file descriptors or do other cleanup tasks when items are no longer accessible.

In this simple case, I don't think you need it so you can remove it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download