Hi community,
Is it possible to turn off cache for a particular object at run time?
For example I have an expensive calculation that I want to cache by default. However, if the user so chooses, the cache can be turned off and calculations re-run from scratch.
import streamlit as st
from streamlit import caching
import time
import random
@st.cache
def expensive_calculation(seed, from_scratch=False):
if from_scratch:
# we may do some more expensive calculation
# when cache is "turned off"
time.sleep(5)
else:
time.sleep(1) # less expensive calculation
return(random.random())
def main():
seed = st.sidebar.selectbox("input", [1, 2])
from_scratch = st.sidebar.checkbox("always re-run")
if from_scratch:
caching.clear_cache()
st.write(expensive_calculation(seed, from_scratch))
if __name__ == "__main__":
main()
Then random
here shows off the effect of clearing cache: the results are different every time when from_scratch
is turned on. In contrast, the result is the same when it’s turned off – and we only have to wait once.
But my problem is that I have many cached objects, and I want to turn off caching for this particular function, and not having to clear all cache.
Is it at all possible?