How can I clear a specific cache only?

I run a script with a few caches.

When I use the following code it clears all the caches:

from streamlit import caching

caching.clear_cache()

I would like to clear only a specific cache. How can I do this?

Hey @theletz, welcome to Streamlit!

This isn’t currently (easily) doable. Can you give some context for what you’re trying to achieve? We have some caching improvements in the pipeline, but it may also be the case that there’s an easier workaround for your use case. Thanks!

Actually, I tried to save the last choice.

For example:

x = st.sidebar.slider("Choose x value: ", min_x, max_x)
y_list = change_y_list(x)
y = st.sidebar.selectbox("Choose y value: ", options=y_list, index=y_idx)

if x was changed, the default value of y_idx will be 0.
I want to check if the previous choice of y is in y_list, and if so, to take the index of the previous choice as the index.

The only way I found to do this is using allow_output_mutation=True, like this:

@st.cache(allow_output_mutation=True)
def get_y_ids():
    return []

def get_y_idx(last_y_ids, y_list):
    if not last_y_ids:
        y_idx = 0
    else:
        last_y_id = last_y_ids[-1]
        if last_y_id in y_list:
            y_idx = y_list.index(last_y_id)
        else:
            y_idx = 0
    return y_idx

if name == '__main__':
    x = st.sidebar.slider("Choose x value: ", min_x, max_x)
    y_list = change_y_list(x)
    cached_y_ids = get_y_ids()
    y_idx = get_y_idx(cached_y_ids, y_list)
    y = st.sidebar.selectbox("Choose y value: ", options=y_list, index=y_idx)
    cached_y_ids.append(y_id)

I just thought:

  1. is there a better way to do this?
  2. Is it possible to clear the cache get_y_ids, without clearing the other caches? (of the data for example)

Hi @theletz,
this is really interesting. Let me know if my understanding is correct. You are trying to store a mutable object in the st.cache, so that you can use this object to store a value that lives across runs. Specifically, you want to keep track of the “last choice” of a widget. allow_output_mutation=True allows you to do so, as you pointed out.

This snipped is doing the above:

import streamlit as st
@st.cache(allow_output_mutation=True)
def get_mutable():
    return []

x = st.sidebar.slider("Choose x value: ", 0, 10)
mutable_object = get_mutable()
mutable_object.append(x)
st.write(f"Choice history is {mutable_object}")

mutable

Matteo

Yes, that`s exactly what I meant.

Now, I just wondered:

  1. is there a better way to do this?
  2. Is it possible to clear the “get_mutable” cache, without clearing the other caches? (of the data for example)

Hey @theletz,

1. Yes, you could do it using @tvst’s SessionState module

import streamlit as st
# SessionState module from https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92
import SessionState

state = SessionState.get(slider_history=[])


def addToHistory(value):
    if len(state.slider_history) < 1 or state.slider_history[-1] != value:
        state.slider_history.append(value)


if st.button("Clear history"):
    state.slider_history.clear()

value = st.sidebar.slider("Choose a value", 0, 10)
addToHistory(value)

st.write(f"Choise history is {state.slider_history}") 

2. As you are using a mutable object you could use .clear() and that’s it, for example:

import streamlit as st

@st.cache(allow_output_mutation=True)
def get_mutable():
    return []


mutable_object = get_mutable()

if st.button("Clear history cache"):
    mutable_object.clear()

x = st.sidebar.slider("Choose x value: ", 0, 10)

if len(mutable_object) < 1 or x != mutable_object[-1]:
    mutable_object.append(x)

st.write(f"Choice history is {mutable_object}")

hello @monchier,
thank you for sharing the awesome code with us. In my project, I need to reuse my cache data by using multi-select or any other widgets. I’m using text_input and multi-select in a single function. what I’m trying to do is just collecting the cache data from text_input and want to use that in multi-select. but I still don’t know how to do that. could you please help me.

@monchier @arraydude

any way to clear the cache created as part of that session.

Hey @Revathi, welcome to the forums! :tada:
To programmatically clear the cache from Python you could do the following

from streamlit import caching
caching.clear_cache()

There’s also a Clear Cache item in the Hamburger Menu in the app, just FYI.

Hi @arraydude
@tim
Thanks for the reply.
After the user logins to my app, after the first attempt gets failed because of the server issue, he hasn’t not able to get the results although its working fine. I suspect its because of the caching, and I just want to clear the cache for that particular selections. Is there any possible way to mitigate it.