Giter VIP home page Giter VIP logo

Comments (9)

puzrin avatar puzrin commented on June 1, 2024

I've proposed a bit different thing. Not autorefresh, but background load after request. For examlpe:

  • i like to cache data for 60 seconds.
  • if next data requested between 60-70 seconts - cache is almost fine, use it and load new data in background.
  • if data not requested for a long time - it's completely expired, and loaded in ordinary way, via cache proxy.

No needs to reload each cache slot, only used ones. Tought, in my use cases, all data will be reloaded anyway. So, no principle difference, wich method will be used.

from memoizee.

medikoo avatar medikoo commented on June 1, 2024

@puzrin Thanks for clarification. I get the point, only if data is accessed again and close to expiry date, it's wise to refresh it in background and push forward expiry date (so likely future access won't have to wait).

Indeed if data is requested frequently it's beneficial, and it's more optimized approach than refreshing any cached value when it's expired, very good point, I'll make reFetch to work exactly that way.

The only difference to what you propose is that I would probably do re-fetch on calls made some time before expiry date rather than still serving cached value to those that were slightly later, I think it would be more what you expect. Serving value obtained 10 seconds ago when e.g. maxAge is set to 8 seconds is controversial. Let me know if I still miss something.

from memoizee.

puzrin avatar puzrin commented on June 1, 2024

Yeah, -(10-20%) before expire will make the same effect, as +(10-20%) after expire (in real life). No principle difference, but pre-fetching looks more logical, than post-loading.

Probably, this interval could be tuned via options, but i guess, 10% default will be "good for all".

from memoizee.

medikoo avatar medikoo commented on June 1, 2024

Thank you! It landed in v0.2.2. I used -33% as default, but it's customizable

from memoizee.

puzrin avatar puzrin commented on June 1, 2024

Thanks!

from memoizee.

puzrin avatar puzrin commented on June 1, 2024

I looked code a bit. Didn't understood, how do you resolve race conditions? If cache is about to expire, and has multiple async gets, will they cause multiple refresh requests? The same question for start state, when cache is empty.

from memoizee.

medikoo avatar medikoo commented on June 1, 2024

@puzrin async logic is separated from maxAge/preFetch logic.

When we're working with asynchronous function, cache hit occurs here: https://github.com/medikoo/memoize/blob/master/lib/ext/async.js#L37

preFetch logic that occurs on cache hit is here:
https://github.com/medikoo/memoize/blob/master/lib/ext/max-age.js#L48
Race condition is prevented by L49 and L51. There's no way that value would be reFetched multiple times.

Another call to memoized function at L55 invokes https://github.com/medikoo/memoize/blob/master/lib/ext/max-age.js#L23-27 again, so preFetch configuration is reinitialized then.

from memoizee.

puzrin avatar puzrin commented on June 1, 2024

Got it, thanks. I was not sure about preFetchCache intention. Probably, it worth to rename it to preFetchLock or similar, to reduce possible confusion. I fact, that's not cache, but singleton locks.

from memoizee.

medikoo avatar medikoo commented on June 1, 2024

Good hint, actually preFetchCache holds either timeout id or race condition lock, but still you're right it deservers a better name. I'll fix that. Thanks!

from memoizee.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.