Skip to content

Better way to cache an expensive Observable source? #7546

Discussion options

You must be logged in to vote

Sure, it looks like you want a cache that implements the stale-while-revalidate pattern but only while there are subscribers.

// The resource you want to cache doesn't totally matter as long as it eventually completes
// so the repeat operator can kick in. This is the reason I've broken it out into a separate 
// constant for this example.
const resource = of('A', 'B', 'C', 'D', 'E').pipe(toArray());

const cached = resource.pipe(
     // This is the actual refresh mechanism. It supports quite a few different arguments to meet
     // your needs. In this case I am keeping it in relation to your example. 
     // 
     // It's worth noting that the delay will not start until the source obs…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@thomasnield
Comment options

@alexanderharding
Comment options

Answer selected by thomasnield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants