How to use st.cache

Hello all from the documentation of streamlit I understand that using the @st.cache will just execute the function once. Because I wouldn’t want to keep connect to database every time the script is reruns. From my code below I have this function that does the initialization of the database and other function would make use of this object to get data from the database. However, when script is started, after running for quite sometime there will be this internal hash error. It says that maximum recursion depth exceed in comparison. Streamlit encountered an object of type builtins.method which it does not know how to hash.

Is this the correct way to cache an object for use in other function?

from database import db 
import streamlit as st 

@st.cache(hash_funcs={db: lambda  _: None}) 
def init_db():
      database = db()
      database.init()
      return database

database = init_db()

def get_data():
        st.text(database.get_data())

Hi @qt.qt -

What happens when you name your objects something other than database? In your code, you have a reference to a module database, but then you also run database = init_db(). Mentally, I’m not sure exactly what would happen here with the Streamlit processing model, as database name binding would (possibly?) go back and forth between being a module, being a database object that’s cached, and back.

Best,
Randy