logo

Caching for Performance

Since Streamlit reruns your script constantly, expensive operations (loading data, API calls, computations) need caching.

import streamlit as st

@st.cache_data
def load_data():
    # This only runs once, then results are cached
    return pd.read_csv("big_file.csv")

df = load_data()  # Fast after first call

@st.cache_data caches the return value. Next time the function is called with the same arguments, it returns the cached result instantly.

For resources that shouldn't be serialized (database connections, ML models):

@st.cache_resource
def load_model():
    return load_heavy_ml_model()

Cache invalidation happens when:

  • Function code changes
  • Input arguments change
  • You manually clear with st.cache_data.clear()

Caching is crucial for responsive apps with heavy data.

I cover caching strategies in my Streamlit course.