Possible to check if key is in cache?

Is it possible to check (return True or False) if a cached function has been computed given a specific key. Example:



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.