Caching expensive python function calls in panel

Data-driven apps sometimes require expensive queries / function calls, which one may want to cache for them to be fast the next time around.

The documentation mentions that panel supports server-side caching as well as the pn.state.cache store, and I’m wondering what patterns people are using to do this.

I’m asking this because I noticed that one of the standard python approaches (just decorating the expensive function with @functools.lru_cache) doesn’t work in panel - e.g. running the following “panel app” results in the “function executed” string being printed on every reload.

from functools import lru_cache

@lru_cache(maxsize=1)
def test():
    print("function executed")
    return 1

test()

I know too little about the panel implementation to know why this is the case (are different requests handled in separate threads?), but it would be very useful if there was a similarly convenient way of caching function calls in a panel app.

P.S. One thing I just noticed is that lru_cache does work, when the function is not defined in the main panel file.

E.g. in the following case, the cache will be used:

cache.py

from functools import lru_cache

@lru_cache(maxsize=1)
def test():
    print("function executed")
    return 1

app.py

from cache import test

test()
panel serve app.py
1 Like