Does @st.cache_data cache across sessions?

Question:

When a function has the @st.cache_data decorator, is the result cached for only that session or for all sessions as long as the app is running?

I ask because both the st.cache_data docs and the caching docs explicity mention that things cached via @st.cache_resource persist across reruns, sessions, and users. However, the docs do not explicitly mention how @st.cache_data acts. Is it cached only for that session?

Goal

Originally, I implemented the caching to return results more quickly and only wanted the caching to persist for that single session. If @st.cache_data persists for all sessions, is using st.session_state the best way to do this?

Context

My deployed app calculates how many ballroom points an individual has. The app reaches out to external websites to retreive the data for the calculations. A user must enter the name of a person they want to calculate points for. I currently have @st.cache_data decorating the calculation function, where the input is the individual’s name.

Issue

I am running into an issue where when the external website updates with new results, the new results sometimes do not appear in my calculator. I was able to resolve this by redeploying the app. I am wondering if this is because if someone has searched “John Smith” before, then John Smith has new results, my calculator is always using the cached results and therefore will never see the new results John Smith has.

1 Like

“persist across reruns, sessions, and users” is intended to imply that it is not “cached only for that session”

Yes, if you really want it to persist only for the session.

Yes, that is probably what is happening. The most obvious solutions are:

  • Keep using cache_data and specify a TTL so that the cache is invalidated after the specified time.
  • Store the data in session_state instead.
1 Like

Great, thank you for the suggestions!

The “persist … and users” was for cache_RESOURCE, not cache_DATA. I was specifically referring to this part in the caching doc, which seemed to imply that cache_DATA may be for things where you don’t want to cache globally:

st.cache_resource is the right command to cache “resources" that should be available globally across all users, sessions, and reruns. […]

It seems like cache_RESOURCE and cache_DATA effectively act the same way to the end user. Is that correct? They both cache things globally but perhaps work differently on the back end.

1 Like

Sorry, I overlooked that and now I understand why you feel confused.

Yes, they both cache globally, I can confirm that. The main difference is that cache_resource stores the object as is while cache_data stores a serialized version of it. But regarding the issue you describe, they both behave the same.

1 Like

Thank you! Is there a process to request an update to the documentation? I think that would help prevent others like me from running into the same confusion. Tried searching around but couldn’t find a process to request documentation changes.

1 Like

I guess you can open an issue in the gh repo.

1 Like

Submitted this issue. There is a new issue template for documentation improvement requests, so it seems like that is the intended process!

1 Like

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