Caching across runs

Hello,

I’ve got to set up a caching mechanism in order to avoid too many API calls (i.e., running into rate limit). I have successfully set up caching by means of @st.cache for the API calls and it appears to be working during a single run of Streamlit. However, I also need to cache the data across multiple runs.

As it seems, each time I interact with the Streamlit frontend, a new run is initiated. That’s fine for me but it eats up the API rate rather quickly because through each run, a new API connection is initiated and the cache of the previous run is flushed.

Is it possible to somehow cache/share data across multiple Streamlit runs? That would be awesome!

Cheers!

1 Like

Do you mean restarts of the python process?

It should be caching between interactions - what’s the code like that you’re caching?

The Streamlit session (streamlit run) keeps running. I meant for example pressing R on the frontend or selecting items in a Multiselect box. The page refreshes and the API client instances are all created anew, following that my caching, which is linked to the api client instance’s __hash__ function, is invalidated.

The API client instances are all initialized in my main.py's if __name__ == "__main__":... block. That block seems to be executed every time I interact with the frontend (pressing R and stuff, see above). So the question is more like, where do I have to initialize instances that I want to keep during interactions like that? The if ... __main__ block does not seem to be the appropriate place…

Ah so what you may want to do is put the API client instantiation into a cached function too.

@st.cache
def get_api_client():

You can set a TTL if the client needs refreshing occasionally.

2 Likes

Awesome! I had to add a hash_func to it but it seems to be working like a charm now!

Thank you very much, that was basically what I was looking for. Sometimes you don’t see the forest for all the trees.

1 Like