Hi,
i want to cache a function, but without passing the args everytime. Instead, getting them from session_state. There’s any native way to do this?
current behavior
@st.cache_data
def get_organization(user):
return "organization from the user"
# write this 2 lines, every time in the app
user = st.session_state['user']
organization = get_organization(user)
desired behavior
@st.cache_data_state
def get_organization(user):
return "organization from the user"
# the user is inherited from session_state automatically
organization = get_organization()
proposal
something like below will do. But i’m wondering if there’s already something native.
def cache_data_kwargs(state_keys: list, **cache_options):
def decorator(function):
cached_function = st.cache_data(**cache_options)(function)
def wrapped_function(**function_args):
state_values = {key: st.session_state[key] for key in state_keys}
state_values.update(function_args)
return cached_function(**state_values)
return wrapped_function
return decorator
@st.cache_data_kwargs(['user'])
def get_organization(user):
return "organization from the user"
organization = get_organization()
Session state is a cache of sorts (session specific), so not sure what your intention is trying to “add caching to a cache”. I think you should revisit this requirement in your design and ask yourself why, and if the implementation is appropriate. Session state is intended to be mutated in different contexts throughout your application, whereas data cached functions are intended to encapsulate state that isn’t. Tying the two concepts together is a code smell in my opinion.
Given an app in a server, can be access by multiple users.
Cache_data is unified across all users. While session_state is particular to a session (different state in different tabs)
What i want to do is caché certain functions, but not overall, but by user instead.
Following example above, i don’t want to compute “get_organization” every single time (imagine needs to access a slow DDBB, and is called several times in the app).
Neither i want to return same organization for all users (that’s a data leak)
My question is if there’s a native decorator (like cache_data_kwargs above)
to do this (or it’s planned).
So you don’t need to write a func for each _func
Perhaps this will work because the func params is already a kwargs dict:
@st.cache_data()
def get_organization(email: str = st.session_state["user"]):
assert email, "user email must be in session state"
return "organization from the user"
organization = get_organization()
I tried with hash_funcs param on cache_data and that didn’t work, so apparently it’s not possible to have a param(s) in a cached function and then expect to be able to call it without any. The cache_data decorator has no new information to decide that its cache should be busted.
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.