Caching per user

Summary

I’m trying to figure out what’s the best way to separate and keep the cache per-user.

Would the following code the expected way, or any better ways?

@st.cache_data
def cached_results_for_you(email = st.experimental_user.email):
  ...

As long as cached_results_for_you() is returning data, that seems like it should work. Note that st.experimental_user works on apps deployed with Streamlit Community Cloud only.

1 Like

Thanks Caroline!

So, if the method does not need to return a value, would returning st.experimental_user.email make sure it would run once per user?

My intended use case is that I’m generating large files that needed to be cleared when there’s a new code deployed. It takes long to generate them, so don’t want it to run every time — generate the files only when they are cleared and missing. (the code path is idempotent and is safe to run at any random time, it’s just slow and redundant)

def run_once(email = st.experimental_user.email):
    clear_temp_files_from_previous_run()
    return st.experimental_user.email

Whether the function runs again depends on whether the function has been called before with the same value for the email parameter, so yes, this seems like it would accomplish what you’re looking for

1 Like

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