Is it possible to check (return True or False) if a cached function has been computed given a specific key. Example:
@st.cache
expensive_function(a)
Then
has_cached = st.has_cache(expensive_function, a)
Or something like that?
Is it possible to check (return True or False) if a cached function has been computed given a specific key. Example:
@st.cache
expensive_function(a)
Then
has_cached = st.has_cache(expensive_function, a)
Or something like that?
Hi @quba9210 -
The Streamlit cache is meant to be an internal implementation detail for the most part, meaning, there’s always the possibility that we refine the caching mechanism for improvements. So we wouldn’t want people to hard-code in key logic, since we could change that at some point in the future.
What use case are you imagining? Something like checking the cache for a value, and if not present, asking a user if they want to run an expensive calculation?
@randyzwitch: I have a streamlit app that calls a very expensive function that can take up to several hours to compute. There are many inputs to this function and I don’t want it to be called before a user has carefully selected all parameters. Therefore I introduced a “Compute” button. If this button is not pressed the app will not automatically invoke the expensive function. On the other hand, I have a result section that should be displayed, provided that the “Compute” button has already been performed for the specific settings. My solution is to create a persistent vector of keys, and show the results only if the function was already computed for the key in the persistent vector. Does it make sense? Or do you have a better solution? Thanks in advance.
Just quoting here to say this sounds reasonable…
I feel like this is how the current cache system is supposed to work. If you have the results for expensive_function()
in the cache for a set of arguments, if you hit the compute button again, it will immediately return the result of expensive_function()
and move on to the next section, which would be your result section.
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.
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.
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.
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.