Best way to cache movie metadata API responses without showing stale data?

Hi everyone,

I’m building a small Streamlit dashboard for a personal project related to NetMirror, where I display movie titles, posters, and some metadata from an external API.

The problem is that the API is a bit slow, so I started using st.cache_data(). Performance is much better now, but sometimes updated movie information doesn’t appear until the cache expires.

I’m trying to find a balance between fast page loading and keeping the data reasonably fresh.

Has anyone handled a similar situation?

  • Do you use a TTL with st.cache_data()?
  • Do you manually clear the cache after certain actions?
  • Or is there a better pattern for frequently updated API data?
  • I’d love to hear what has worked for your Streamlit apps. Thanks!

Hey there, thanks for sharing your question with the community! :balloon: This is a classic challenge with caching in Streamlit—balancing speed with data freshness. Using @st.cache_data with a ttl (time-to-live) parameter is the recommended approach for APIs that update regularly. Setting ttl ensures cached data is only kept for a set period (e.g., @st.cache_data(ttl=600) for 10 minutes), so users get fresher data without hitting the API on every rerun. You can also manually clear the cache with st.cache_data.clear() or your_cached_func.clear() if you want to force a refresh after certain actions, but this is less common for general API data refreshes. For most apps, a reasonable ttl is the best trade-off between speed and freshness—adjust it based on how often your API data changes and how critical it is to show the latest info right away.

If you want even more control, Streamlit is working on background refresh for cache, which will let you serve stale data instantly while updating in the background, but this isn’t generally available yet. For now, stick with ttl and manual clears as needed. If you have a reproducible example or code snippet, feel free to share it—others might have more targeted tips! For more details, check out the Streamlit caching docs and caching concepts. Community, feel free to jump in with your own strategies!

Sources: