What is the best way to cache data and views when using panel serve?

So I have a multiple Panel applications that retrieve data from a database. It’s been a learning experience but based on server limitations the way the project is structured is that I have:

  • a data.py file full of classes that fetch and process the relevant data from an external database source and provides methods to return the appropriate data for a particular chart (without preprocessing)
  • a chart.py which takes data returned from data.py and processes the data properly and builds an interactive Bokeh chart
  • a views.py which has separate classes for each “page” of the application returning a built template with the layout of the charts and widget binding done here

The views.py has classes something like this:

# views.py
import Page1Charts from chart

class Page1:
    def __init__(self):
        self.chart_builder = Page1Charts()
        self.rendered_template = page1_template

    def page1_template(self):
        # builds a pn.template using self.chart_builder
        return template

    def view(self):
        # some additional configurations
        return self.rendered_template()

where the Page1Charts would initialize a Page1Data object that would then pull the relevant data.
Afterwards each page would typically use the view function of its object and whenever I would deploy using pn.serve as a function call inside python app.py it would work alright. Unfortunately due to some issues with our specific server that method doesn’t work, meaning we have refactored our code to use panel serve via the CLI.

In that instance every time I load a new page, although the data.py portion of the code stores the fetched Database query in pn.state.cache I notice that loading the new page fetches data from the database again and builds everything from scratch again as opposed to using the cache. Additionally the codebase has evolved to this state because of the original stakeholders’ requirements.

What might I be doing wrong/what information can I provide to clarify what I may be doing wrong such that both my views and data are being fetched every time a page is loaded.

Sidenote: another issue is I am applying global_css rules to each template to hide parts of the default Panel template, and this I initialize using pn.extension but that doesn’t seem to hold whenever someone refreshes the page.

Any assistance either by post or other medium would be greatly appreciated. Thank you.