Functools.cache versus st.cache_data

Is there a guideline for when to use functools.cache or functools.lru_cache and when to use st.cache_data? It seems like the data in st.cache_data is invalidated when a new session is started, and for a website that multiple people may access, functools.cache would be more efficient for (for example) holding on to a large downloaded file. Is that correct?

Hi there @michaelbilow ,

Have a look at the caching documentation, and in particular at the st.cache_resource method. It allows caching across users and sessions.

Hope this helps!

Got it; maybe worth adding to the tl;dr version of the docs that immutable data structures are better saved with st.cache_resource rather than st.cache_data; all of the pd.DataFrame use cases point you to st.cache_data.

Thank you!

1 Like

pd.DataFrame is not an immutable data structure. As far as I can tell, data cached using st.cache_data is not invalidated when a new session is started. I think that would be a bug if it happened.

Right, pd.Dataframe is not an immutable class, but if your streamlit application treats the data as immutable (by only reading from it, for example), it seems like it would be better to use st.cache_resource rather than st.cache_data, since the resource is cached across multiple sessions. That seems to bear out in my testing, is that incorrect?

Data cached using st.cache_data is also available across multiple sessions. If the cached object is not mutated then the only difference is performance, since cache_data stores the object in serialized form.

Wanted to get back to the OP’s question of functools.cache (or lru_cache) vs streamlit.cache_data.

As far as I can tell, you can use the functools cache instead of the streamlit cache. Streamlit’s caching pre-dates functools (Python 3.9) and is generally more flexible. functools.cache requires that all arguments be hashable. I work a lot with dataframes and ndarrays which are not hashable so you can’t really jam them into functools.cache without more work.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.