smomen
January 10, 2023, 3:52pm
37
Hi @jrieke -
I’ve seen a few users looking for a user-specific cache (across-sessions) and session-specific cache. They’re doing hacks to accomplish this, and if they’re building multitenant apps, risking leaking data/it’s quite unsafe.
Example threads:
Caching in Streamlit
Goal -
1. Caching per session - Here we should be able to cache functions per session of a user. Here the user is not unique so the cache will be limited only to that particular session and will not be stored or preserved on the server. Modifications to data or files by the user will be limited to that session only and will not be applied globally.
2. Caching per user globally - Here the user is unique and is identified by the server, so here cache should be applied to th…
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 …
My question
Is using st.session_state as a session specific cache correct and intended usage of the session state?
Does it release its values when the session closes and are they collected by GC?
Background and details
I wanted a session specific cache mechanism to set, for example, stateful deep learning models,
and I found this thread: Session Specific Caching where using st.cache with session_id has been recommended.
However, I think st.cache with session_id can’t release the cached obj…
plus @whitphx 's post above We want to deprecate st.cache! …and need your input ❤️ - #18 by whitphx .
The solutions out there right now:
inject a hacked up session_id into cached/memoized methods (there are gists out there for this; they’ve broken after some releases)
inject a user_id, e.g. experimental_user - not great cuz this isn’t available for public apps or private third party deployments
use session_state - the ergonomics are poor/unsafe, and need to rig your own TTL/etc.:
some_df = get_data(...)
st.session_state['my_df'] = some_df
plot(st.session_state['my_df']) # BETTER NOT USE some_def!
other_df = transform(st.session_state['my_df'])...
vs a better alternative IMO:
some_df = get_data(...)
plot(some_df)
...
@st.memo(per_user=True)
def get_data(...)
...
Any plans for something that supports more ergonomic user and/or session-specific cache-ing?