I’m looking at using experimental_singleton, and we want the cache to expire every few hours. But it doesn’t seem like there’s native support for this.
(Unlike when using experimental_memo, where you can configure an expiration time using ttl)
FWIW, the use case here is that we have a web app that pulls data from a database that’s being updated online. We’re caching the query result and sharing it across users using experimental_singleton
Every few hours, we want the front-end to be able to pull the updated data from the database. In other words, we need to expire the singleton cache every few hours.
Any guidance on how to do this would be very much appreciated.
@asehmi OP wants to share the same data across all the users, memo caches for each user to my understanding so if they have many concurrent users memory will still increase greatly for the app as a whole, whereas if OP can use singleton then that would not be the case.
@bpiv400 One way you could go about this is to create a singleton function that is called similarly to all the others, that caches a datetime.datetime.now() response that can then be checked against the current time any user is using your app, and if that time delta is greater than a certain amount clear the cache and redo all the operations. Pseudo code below:
Hi - What you say isn’t clear from the docs. I think experimental_memo works across all user sessions, but could be wrong. It should be easy to test. Otherwise, use cache which also has a ttl parameter. Your solution should also work nicely.
@asehmi I think you are correct. streamlit is likely pickling the data then just pulling from on subsequent function calls. the main thing between singleton vs memo is that singleton is the same object (not copy) vs memo is copy. so each of them are values for user but singleton allows to create objects that perhaps (a) cant pickle easily (b) larger objects/other reasons may want shared across users (and not create copy for each user when called).
believe session_state is the only one that is user specific. perhaps i am wrong but reading more in docs seems like that is likely the case
Each caller of a memoized function gets its own copy of the cached data."
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.