st.cache_data vs st.experimental_memo: Choosing the Right Decorator for Repeatable Data Loading
0 reputation · 29 Dec 2020, 14:00 UTC
Goal
We want a development environment where loading a CSV file via a Streamlit decorator yields the same cached result across script restarts until the file changes.
Constraint
The project uses Streamlit 1.18 or newer, so both st.cache_data (stable) and st.experimental_memo (legacy) are available. The cache must survive a full app reload but be invalidated when the underlying data file changes.
Uncertainty
While st.cache_data is documented to separate data caching from resource caching, st.experimental_memo may over‑cache heavy objects and can change behavior with Streamlit upgrades. Additionally, handling of unhashable inputs (e.g., DataFrames with mutable indexes) differs between the two decorators.
Questions
- In a 1.18+ environment, does
st.cache_dataguarantee that identical inputs produce identical cached outputs across script restarts, whereasst.experimental_memomay invalidate unexpectedly? - How does each decorator handle unhashable arguments such as Pandas DataFrames with mutable indices, and what custom hash functions are required?
- What impact does a future Streamlit upgrade have on the stability of
st.experimental_memocompared to the stablest.cache_dataAPI?