"Universal" cache_data

Summary

using @st.cache(ttl=3600) for example correctly caches a function that requests an API but in an individual way for each user that uses my web application. My intention is to save unnecessary requests and therefore use a shared cache. Is there a way to do this? I mean, the application stores the cache from the first execution and then uses it for 60 minutes for the next executions regardless of the user that executes it?

In my experience, st.cache_data works exactly as you want.

Interesting, I wasnโ€™t aware of that either, I also thought until now that st.cache_data was applied to each individual session. I donโ€™t think that is clear from the documentation.
But you could just test that: insert debug code into the cached function and use it from different sessions?

The debug code would run on cache misses only.

Yes, of course, but that would be enough to confirm that the cache works across different sessions aka โ€œuniversalโ€? :thinking:

Oh, I have confirmed it. The debug code being just code that downloads and parses files large enough that you cannot avoid noticing whether it runs or not. Toy example below, using the proverbial time.sleep as an expensive operation.

import time

import streamlit as st


@st.cache_data
def square(number):
    time.sleep(5)
    return number**2


def main():
    number = st.number_input(label="Number to square", value=0)
    number_squared = square(number)
    st.write(f"Number squared: {number_squared}")


if __name__ == "__main__":
    main()

1 Like

I just fixed the issue, now only 1 request per hourโ€ฆ

but the point is, before i was using it accordingly with the documentation but everytime that someone entered the aplication a new request was made.

what i did is set a cached_time = None and cached_sfl_price = None for the variable that storage the API result.

Inside de function:

cached_sfl_price = sfl_price
cached_time = time.time()

after (outside) the function:

if cached_sfl_price is None or time.time() - cached_time > 3600:
    sfl_price = get_sfl_price()
else:
    sfl_price = cached_sfl_price

all this plus @st.cache_data(ttl=3600)

I have no idea how this relates to the issue you were having. If you think the behavior of cache_data does not match the documentation, posting an example would help to have it fixed.

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