Giter VIP home page Giter VIP logo

Comments (8)

jperasmus avatar jperasmus commented on June 15, 2024 1

v3.2.0 includes functionality to automatically dedupe any requests from the same instance of swr. If you're interested in the implementation, you can look at PR #35

from stale-while-revalidate-cache.

jperasmus avatar jperasmus commented on June 15, 2024

To support a use case where cache functions with the same key are run in sequence (or just be aware of one another) would require adding much more complexity to the library. Can you provide a good real world example of where this feature would be useful? The example you gave explains the issue but I am struggling to picture where you would want to do that. With this example you can easily in "user land" make the requests in the order you require to ensure the cache is set.

from stale-while-revalidate-cache.

mdissel avatar mdissel commented on June 15, 2024

In my sample the load function is called in multiple async requests that are loaded concurrently using promise.all(..)

Based on the key we could check if the there's an active "load" action. If yes, wait until that is finished and continue. the waited request should just return the already cached data

from stale-while-revalidate-cache.

jperasmus avatar jperasmus commented on June 15, 2024

I thought about this request more and it is not something I want to add to the library at this stage. This will add unnecessary complexity to the library as a whole for a very specific use case. It is also fairly simple to handle this logic outside the library.

  1. You could await the first request before making the rest of the requests inside the Promise.all():
import { createStaleWhileRevalidateCache } from 'stale-while-revalidate-cache'

const swr = createStaleWhileRevalidateCache({
  storage: window.localStorage,
})
const key = "ListId";
const load = async () => {
  return (await this.list.select("Id")()).Id;
};

const result1 = await swr(key, load)

const [result2, result3] = await Promise.all( 
  [swr(key, load), swr(key, load)]
);
  1. You could add a fairly simple cache wrapper around it:
import { createStaleWhileRevalidateCache } from 'stale-while-revalidate-cache'

const swr = createStaleWhileRevalidateCache({
  storage: window.localStorage,
})

const requests = new Map()

async function cache(key, fn) {
  if (requests.has(key)) {
	// either return `previousRequest` here since it is cached in any case or pass it onto swr if you really want
    const previousRequest = await requests.get(key)
    return previousRequest
  }

  const request = swr(key, fn) // not awaiting it here
  requests.set(key, request)

  const result = await request
  requests.delete(key)

  return result  
}

I haven't tested this, but conceptually it should work.

from stale-while-revalidate-cache.

mdissel avatar mdissel commented on June 15, 2024

Thanks for the tips! nr 1 is not an option for me, the cached requests are deeply nested inside the sample load() function.

from stale-while-revalidate-cache.

jperasmus avatar jperasmus commented on June 15, 2024

I see. Hopefully, the second option is something that you can build on.

from stale-while-revalidate-cache.

TechnologicNick avatar TechnologicNick commented on June 15, 2024

I too falsely assumed multiple requests with the same key would be deduplicated, took me some time to discover this was not the case. I expected more people would need this feature, TanStack Query for example even lists request deduping in their motivation.

While the second option works perfectly for me, why would adding it into the library add unnecessary complexity? Isn't it just those couple lines you already posted or is there more to it I fail to see?

from stale-while-revalidate-cache.

jperasmus avatar jperasmus commented on June 15, 2024

I will relook at this feature request. I see now that it could be beneficial to have auto-deduplication of function calls.

The code example I gave was very rudimentary and not necessarily production-ready. It lacks error handling and just testing in general.

from stale-while-revalidate-cache.

Related Issues (9)

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.