This might relate to some of the SessionState stuff mentioned here, but is there any way to make caching session specific?
I’ve noticed that cached values are served across all sessions. For example I have a set of demo inputs to my prediction function. If one user runs the demo, the results are cached and then served up to any other user that tries to access the demo. Is there any way to make caching session specific, or serve a fresh session to each new user?
On a related note, does caching have a set lifetime? Are cached values deleted after some time, or do they persist indefinitely? Is there a scenario where cached predictions stored in memory pile up over time until the instance running the app runs out of memory?
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
I elaborated on tvst’s awesome answer above to write a gist defining a @fancy_cache decorator, which is similar to @st.cache, but it add two cool new keyword arguments:
ttl : The maximum number of seconds that this item will remain in the cache.
unique_to_session : True means that the cache is unique to this session.
@Adrien_Treuille’s workaround doesn’t appear to work with versions after 0.53.0…
For the TTL option, though, I added a defaulted parameter to my cached function that changes every minute: i.e. func(*args, ttl=math.floor(datetime.datetime.now().timestamp()/60)*60).
thank you for the update. Unfortunately, it doesn’t work im with my Streamlit-version 0.62.
I get the following error:
“AttributeError: ‘Server’ object has no attribute ‘_session_infos’”.
I copied your code in a separate file and imported it. Do you have any suggestions. (I am running on an EC2 Instance on AWS).
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.