Unable to get caching working for my function that reads from database


I have a function that gets data from a database and I want to be able to cache it.
This data will then be stored in a “page” object and accessed by multiple components.
However when I change between the pages (using streamlit page structure), it is clearly querying the database each time, taking considerable time to load.

Steps to reproduce

Code snippet:

def get_table_head(_conn, database, column, program):
    query = f"SELECT TOP 500 * FROM {database} WHERE {column} LIKE '%{program}%'"

        return pd.read_sql(query, _conn)
        logging.error(f"ERROR: Failed to query database, {database}")

This data is then stored in the page object using a class method

def get_data(self):
        self.dash_data = dbaccess.db_common.get_table_head(self.dash_conn, 'Fact_TaskAndScheduleMilestones', "Program", self.name)

Expected behavior:

When switching between pages it should not have to query the database each time

Actual behavior:

It queries the database, seemingly ignoring the cache function.

Debug info

  • Streamlit version: 1.19
  • Python version: 3.9.12
  • Using Conda.
  • Browser version: Microsoft EdgeVersion 110.0.1587.63 (Official build) (64-bit)

What is this “page” object?

Just my own class that’s storing information on the page that I want to build

1 Like

The @ is missing, should be:


ah yes that was it, silly mistake, thank you