How long do cached data live if you do not set a TTL?

Does the cache reset each time you reset the page or start a new “session”?
Thanks

It lives as long as the application itself does.

I hosted my app. It has a URL, it seems the cache survives if I refresh the page. But if I close the tab in my browser and go to the app again, it will rerun the cached functions. Is it right?

No, the application is istill running after you close the browser.

Edit A better answer would be: not if your host is Streamlit Cloud , but it might be rigt with other hosts. An example would be stlite sharing, where the app runs in the browser, so if you close the tab you kill the application too.

The function I have is like this:

@st.cache_data()
def get_something_from_database():
#connect to database, get some result
return result

I did more testing. The function reruns each time I close the tab and go to the app in a new tab. This is actually a desired behavior for me as I do want to function to run once (check the database) each new “session”. But I don’t want the function to run repeatedly each time the user interact with the app.

But I’m confused by your response. Because that would mean the function shouldn’t rerun when I close the tab, but it does, because I have show_spinner = True.

FYI, I’m hosting my app using Posit (Rstudio Connect).

I don’t know how Posit works but they charge by the hour so maybe they are killing the application to save costs when there are no active sessions. I am used to Stremlit Cloud and I didn’t realize that other provides may not work the same way.

You can test an small application I deployed to Streamlit Cloud. It queries a small database, running the query takes 10 seconds so the result is cached. If you close your browser, the cache will be there when you return. Well, unless another user clears the cache in the meantime.

If you feel curious, you can take the code to other platforms to see how it works there.

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