Source analysis on MaxAge blind spot of Lru-cache in NPM

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.