Cache issue when deployed on Streamlit Cloud

I have a cache issue. In my app I allow the user to upload a file and can do a bunch of things. When the app is deployed to Streamlit cloud what I notice is the file uploaded by say user #1 remains in the cache and when user #2 uploads their document it has no effect and only user #1’s document is used. I want to clear the cache programtically before the uploaded file is processed. How can I accomplish this?

def clear_submit():
    st.session_state["submit"] = False
    #st.cache_data.clear()

placeholder_upload = st.empty()
with placeholder_upload.container():
    uploaded_file = st.file_uploader(
        "Upload a pdf, docx, or txt file.",
        type=["pdf", "docx", "txt", "csv"],
        help="No scanned documents please!",
        on_change=clear_submit,
    )
TABULAR_INPUT_FILE_FLAG = False
index = None
doc = None
if uploaded_file is not None:
    if uploaded_file.name.endswith(".pdf"):
        doc = parse_pdf(uploaded_file)
        #st.write(type(doc))
    elif uploaded_file.name.endswith(".docx"):
        doc = parse_docx(uploaded_file)
        #st.write(type(doc))
    elif uploaded_file.name.endswith(".txt"):
        doc = parse_txt(uploaded_file)
        #st.write(type(doc))
    elif uploaded_file.name.endswith(".csv"):
        TABULAR_INPUT_FILE_FLAG = True
        doc = parse_csv(uploaded_file)
        st.session_state["api_key_configured"] = True
    else:
        raise ValueError("File type not supported!")

Hi @AI-Dad,

Let’s take a look at this basic Python function:

@st.cache_data
def foo(bar):
    return data

You can clear all cached entries for this function as follows:
foo.clear()

You can also clear values from all in-memory or on-disk cached functions, as follows:
st.cache_data.clear()

I’m not able to check whether the above code snippet works in your code as I don’t have access to the full code. Please let me know if it works or not. If not, please send us the full code and we can investigate further :slight_smile:

For more information about our new caching primitives, please see this document:

Best wishes,

Charly

I cannot reproduce the issue with my own implementations of parse_csv, parse_pdf and parse_txt. Maybe your implementations are different and that is where the issue lies.

1 Like

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