How can I cache things in a robust way that doesn't cause page loda delays?

  • Page Owner: Not Set
  • Last Reviewed: 2022-07-07

I have to pull data from an API which can be slow and unreliable. I'd like the page that uses this data to be fast, even when the data is out-of-date. Is there a way to return stale data while fresh data is fetched in the background?


Answer

I'm so glad you asked this question, me!

There is a ContentCaching service that works as such:

  1. If this is the first request, and no data is available, it will block and fetch data. Then it will cache data for the length of time requested.
  2. On subsequent requests, if the data is stale, it will still be returned from cache while a background thread fetches new data. Once the new data has returned, it will replace the stale data.
  3. It also has options for some limited retry mechanics.

You can find it in the RW Baird repo and also the StoneX repo: https://dev.azure.com/BlendInteractive/_git/StoneX?path=/FCStone.Web/Business/Caching/ContentCachingService.cs