Copy an example below
VarLru=Require"Lru-cache= {max: 500, lengthfunction (n, key) {return n * 2 + Key. length}, dispose: function (key, n) {n.close ()}, MaxAge1000 * 60 * 60}, Cache = LRU (options)
I'm supposed to be familiar with this part of you. MaxAge This parameter is very easy to misleading, here I detailed the maxAge. I always thought that when the set is over a key-value, the LRU will automatically delete the key-value when it exceeds the value set by MaxAge. However, after some analysis, the fact is not true ~
First MaxAge This parameter is verified only when the set value and the status are judged (Isstale method).
And Isstale This method is useful in the value, the judging value and the method foreach
Expired (more than maxage) values are removed by the Del method, and the Execute instance object is the Dispose method that is set when the execution is deleted.
Therefore, the MaxAge parameter value is valid, but the time check is performed when the get and ForEach are executed. Timeout executes the Dispose method.
The value of the Key-value can be taken for the timeout, depending on the stale parameter of the setting.
Parameter maxAge, stale parameter default
When get is judged whether to timeout, if timeout, return undefined after deletion
Parameter MaxAge, stale set true
When the get is judged whether to time out, if it times out, after the value is returned after the deletion.
Welcome to the vomit-trough, discussion.
Source analysis on MaxAge blind spot of Lru-cache in NPM