Session Specific Caching

Hey Karl! Lots of great questions. I’ll answer one by one:

is there any way to make caching session specific?

Yes, this is possible, but currently a bit hacky. For this, I modified the code in the thread you linked above to make it grab a session-specific ID: Hack to get a session-specific ID in Streamlit. See https://discuss.streamlit.io/t/session-specific-caching/271 · GitHub

Now you can pass the get_session_id() function from that Gist to make cache session -specific:

user_session_id = get_session_id()

@st.cache
def session_specific_addition(arg1, arg2, user_session_id):
  # Your function body goes here. You don't need to do anything with
  # the user_session_id argument. Just leave it in the function signature.
  return arg1 + arg2 

result = session_specific_addition(10, 20, user_session_id)

# And if you want to hide the user_session_id arg,
# just wrap the function:

def addition(arg1, arg2):
  return session_sepecific_addition(10, 20, user_session_id)

On a related note, does caching have a set lifetime?

Not yet, but thanks for the reminder! We’ve had a ticket about this in our old tracker for a couple of months now, but never got to it. So I created a new issue on the new public tracker, to shame us into doing the right thing :wink:

4 Likes