question about LRU cache max size

360 views
Skip to first unread message

MC

unread,
May 3, 2015, 9:53:56 PM5/3/15
to nod...@googlegroups.com
Hello,
I'm looking for clarification on how to set cache max size. In this example:
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

What value does 'length' function operate on? Is n related to key or value stored in cache?
I store documents of an average size of 1.5 kB in cache. Key is an MD5 hash (constant length).
Since I know average document size can I return 1 as length function output, and treat max as document count?
I.e. if max = 500 then it's an indicator of 500 documents, each of size 1.5 kB (750 kB total)?
Thanks,

M

Jimb Esser

unread,
May 4, 2015, 5:02:28 PM5/4/15
to nod...@googlegroups.com
The lru-cache code is pretty small and worth reading through if you're using any of the fancy options.  It looks like length calculation functions are passed the values (not keys, so, in your case, your documents, so you probably want it to return n.length or something).  Additionally "max" refers to the same units as you return from length(), so if your documents are 1.5kb your max needs to be something on the order of KBs or MBs.

If you know your average size, don't want specific size calculations going on, then just use the simple example, don't pass any options, just pass a max size (default length function already returns 1, so the max is just max number of elements).

And, reading the documentation on GitHub it appears to say almost exactly what I just said when describing the various options.
Reply all
Reply to author
Forward
0 new messages