Clearing Cache

Dear friends,

I am currently writing a web-application and need to “refresh the page” by clearing the cache. So the user should be able to refresh the page. For this reason I created a button with the following statement in it st.legacy_caching.caching.clear_cache(). However, even I have the most updated version (1.12.0) I get the following error “module ‘streamlit’ has no attribute 'legacy_caching’”

my code looks like this

@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def load_General():
    engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
    con=engine.connect()
    general_query_raw='''string'''
    General_raw=pd.read_sql(general_query_raw,con)
    
    general_query_cleansed='''string'''
    General_cleansed=pd.read_sql(general_query_cleansed,con)
    return General_raw, General_cleansed


@st.cache(allow_output_mutation=True,suppress_st_warning=True)
def load_Role():
    engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
    con=engine.connect()
    role_query_raw='''string'''
    Role_raw=pd.read_sql(role_query_raw,con)
    
    role_query_cleansed='''string'''
    Role_cleansed=pd.read_sql(role_query_cleansed,con)  
    return Role_raw,Role_cleansed

With the following line(s) I try to clean the cache in order to be able to get the latest data from the DB

def clear_cache():
    st.legacy_caching.caching.clear_cache()

st.sidebar.button("Refresh Program",on_click=clear_cache)

Any Idea what could help me here?

Hi @Isaak_Saba :wave:

The .clear_cache() method for st.cache isn’t part of the public API. We frequently refactor parts of Streamlit’s internals to fix bugs, improve performance, and add new features; we’re not able to maintain backwards compatibility for non-public APIs.

I know this can be frustrating! There are a couple of options here: the quickest fix is to use st.runtime.legacy_caching.clear_cache(), as legacy_caching is now part of a streamlit.runtime package. The more durable option is to use the newer cache primitives @st.experimental_singleton and @st.experimental_memo and their respective .clear() commands.

You may also be interested in contributing to the discussion in: We want to deprecate st.cache! …and need your input ❤️

3 Likes

Thanks so much :), really saved my life

I decided to go with st.experimental_memo which works smooth

1 Like

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