Optimize performance
Streamlit provides powerful cache primitives for data and global resources. They allow your app to stay performant even when loading data from the web, manipulating large datasets, or performing expensive computations.
Cache data
Function decorator to cache functions that return data (e.g. dataframe transforms, database queries, ML inference).
@st.cache_data
def long_function(param1, param2):
# Perform expensive computation here or
# fetch data from the web here
return data
Cache resource
Function decorator to cache functions that return global resources (e.g. database connections, ML models).
@st.cache_resource
def init_model():
# Return a global resource here
return pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english"
)
Clear cached data
Clear all in-memory and on-disk data caches.
@st.cache_data
def long_function(param1, param2):
# Perform expensive computation here or
# fetch data from the web here
return data
if st.checkbox("Clear All"):
# Clear values from *all* cache_data functions
st.cache_data.clear()
Clear cached resources
Clear all st.cache_resource
caches.
@st.cache_resource
def init_model():
# Return a global resource here
return pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english"
)
if st.checkbox("Clear All"):
# Clear values from *all* cache_resource functions
st.cache_data.clear()
Important
All the below commands were deprecated in version 1.18.0. Use the new commands above instead. Learn more in Caching.
Deprecated commands
This command was deprecated in version 1.18.0. Use
st.cache_data
orst.cache_resource
instead.
Caching
Function decorator to memoize function executions.
@st.cache(ttl=3600)
def run_long_computation(arg1, arg2):
# Do stuff here
return computation_output
This command was deprecated in version 1.18.0. Use
st.cache_data
instead.
Memo
Experimental function decorator to memoize function executions.
@st.experimental_memo
def fetch_and_clean_data(url):
# Fetch data from URL here, and then clean it up.
return data
This command was deprecated in version 1.18.0. Use
st.cache_resource
instead.
Singleton
Experimental function decorator to store singleton objects.
@st.experimental_singleton
def get_database_session(url):
# Create a database session object that points to the URL.
return session
This command was deprecated in version 1.18.0. Use
st.cache_data.clear
instead.
Clear memo
Clear all in-memory and on-disk memo caches.
@st.experimental_memo
def fetch_and_clean_data(url):
# Fetch data from URL here, and then clean it up.
return data
if st.checkbox("Clear All"):
# Clear values from *all* memoized functions
st.experimental_memo.clear()
This command was deprecated in version 1.18.0. Use
st.cache_resource.clear
instead.
Clear singleton
Clear all singleton caches.
@st.experimental_singleton
def get_database_session(url):
# Create a database session object that points to the URL.
return session
if st.button("Clear All"):
# Clears all singleton caches:
st.experimental_singleton.clear()