Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pn.cache only caches for a session. #5109

Closed
MarcSkovMadsen opened this issue Jun 14, 2023 · 3 comments
Closed

pn.cache only caches for a session. #5109

MarcSkovMadsen opened this issue Jun 14, 2023 · 3 comments

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Jun 14, 2023

panel==1.1.0

When working on the Streamlit Migration guide in #5027 I discovered that @pn.cache only caches for a user session. Not globally.

Globally was what I and @philippjfr expected. Globally is what I need most often.

Reproduce

from time import sleep

import numpy as np
import panel as pn
from matplotlib.figure import Figure


def get_data():
    print("running get_data func")
    sleep(0.1)
    return np.random.normal(1, 1, size=100)

def plot(data, bins):
    print("running plot func", bins)
    sleep(2)
    fig = Figure(figsize=(8,4))
    ax = fig.subplots()
    ax.hist(data, bins=bins)
    return fig

pn.extension(sizing_mode="stretch_width", template="bootstrap")

data = get_data()
bins = pn.widgets.IntSlider(value=20, start=10, end=30, step=1)
cplot = pn.cache(plot)
bplot = pn.bind(cplot, data, bins)
pn.Column(bins, pn.panel(bplot, loading_indicator=True)).servable()

You can see how it reruns the plot function after a reload of the browser below.

2023-06-14_05-47-41.mp4

Additional Context

You actually need to be able to both cache things for a users session and globally. Globally probably accounts for 99% of use cases though.

You could handle this by providing two caches:

  • pn.cache_user: caches for a user session
  • pn.cache_global: caches globally across user session.

If we just want the one we could describe in the documentation how to get the session id (I don't know how) and use this to cache for a user session. This documentation could be added to the cache documentation https://panel.holoviz.org/how_to/caching/index.html.

See also #5028 (comment) and #5028 (comment)

I'm on windows. Don't think it matters.

@MarcSkovMadsen MarcSkovMadsen added the type: bug Something isn't correct or isn't working label Jun 14, 2023
@MarcSkovMadsen MarcSkovMadsen added this to the next milestone Jun 14, 2023
@philippjfr
Copy link
Member

philippjfr commented Jun 15, 2023

I'm very confused by your example, the input to your cached function is a random array which is basically guaranteed to change each time. If you wrap the call to get_data in pn.cache then the random array stays the same each time and you'll cache hit on get_plot.

Closing this as there is no issue here.

@philippjfr philippjfr removed this from the next milestone Jun 15, 2023
@philippjfr philippjfr removed the type: bug Something isn't correct or isn't working label Jun 15, 2023
@philippjfr
Copy link
Member

I'll add an argument to pn.cache to support per-session caches.

@MarcSkovMadsen
Copy link
Collaborator Author

Looks like you are right. I overlooked this. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants