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.
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.
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.
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.