Optimize performance with st.cache
We're developing new cache primitives that are easier to use and much faster than @st.cache. 🚀 To learn more, read Experimental cache primitives.
Streamlit provides a caching mechanism that allows your app to stay performant even when loading data from the web, manipulating large datasets, or performing expensive computations. This is done with the @st.cache decorator.
When you mark a function with the @st.cache decorator, it tells Streamlit that whenever the function is called it needs to check a few things:
The input parameters that you called the function with
The value of any external variable used in the function
The body of the function
The body of any function used inside the cached function
If this is the first time Streamlit has seen these four components with these exact values and in this exact combination and order, it runs the function and stores the result in a local cache. Then, next time the cached function is called, if none of these components changed, Streamlit will just skip executing the function altogether and, instead, return the output previously stored in the cache.
