using @st.cache(ttl=3600) for example correctly caches a function that requests an API but in an individual way for each user that uses my web application. My intention is to save unnecessary requests and therefore use a shared cache. Is there a way to do this? I mean, the application stores the cache from the first execution and then uses it for 60 minutes for the next executions regardless of the user that executes it?
Interesting, I wasn’t aware of that either, I also thought until now that st.cache_data was applied to each individual session. I don’t think that is clear from the documentation.
But you could just test that: insert debug code into the cached function and use it from different sessions?
Oh, I have confirmed it. The debug code being just code that downloads and parses files large enough that you cannot avoid noticing whether it runs or not. Toy example below, using the proverbial time.sleep as an expensive operation.
import time
import streamlit as st
@st.cache_data
def square(number):
time.sleep(5)
return number**2
def main():
number = st.number_input(label="Number to square", value=0)
number_squared = square(number)
st.write(f"Number squared: {number_squared}")
if __name__ == "__main__":
main()
I have no idea how this relates to the issue you were having. If you think the behavior of cache_data does not match the documentation, posting an example would help to have it fixed.