python
@st.cache(allow_output_mutation=True)
def get_database_connection():
    return db.get_connection()
With just 3 lines of code, the database connection is created once and stored in the cache. Then, every subsequent time get_database_conection is called, the already-created connection object is reused automatically. In other words, it becomes a singleton.
Use the allow_output_mutation=True flag to suppress the immutability check. This prevents Streamlit from trying to hash the output connection, and also turns off Streamlit's mutation warning in the process.
What if you want to write a function that receives a database connection as input? For that, you'll use hash_funcs:
python
@st.cache(hash_funcs={DBConnection: id})
def get_users(connection):
    # Note: We assume that connection is of type DBConnection.
