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

Implement cache function for memoization support #2411

Merged
merged 16 commits into from
Aug 13, 2022
Merged

Conversation

philippjfr
Copy link
Member

@philippjfr philippjfr commented Jun 21, 2021

This PR implements a cache decorator which enables memoization for any function. It heavily borrows from streamlit without copying any of the code and does not implement any of the code hashing functionality, which allows them to quickly reload the application. Since we hash explicitly on the function or method autoreload will always invalidate the cache. Only the explicit pn.state.cache persists when autoreloading. This implementation also allows memoizing parameterized methods decorated with param.depends.

ToDo

  • Consider additional types to implement hashing on
  • Document
  • Write tests

@codecov
Copy link

codecov bot commented Jun 22, 2021

Codecov Report

Merging #2411 (eef754d) into master (e683957) will increase coverage by 0.08%.
The diff coverage is 90.14%.

@@            Coverage Diff             @@
##           master    #2411      +/-   ##
==========================================
+ Coverage   84.12%   84.20%   +0.08%     
==========================================
  Files         211      213       +2     
  Lines       30542    30958     +416     
==========================================
+ Hits        25692    26067     +375     
- Misses       4850     4891      +41     
Flag Coverage Δ
ui-tests 34.02% <25.24%> (-0.12%) ⬇️
unitexamples-tests 77.09% <90.14%> (+0.17%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
panel/__init__.py 100.00% <ø> (ø)
panel/io/cache.py 82.43% <82.43%> (ø)
panel/io/state.py 69.10% <90.90%> (+0.99%) ⬆️
panel/io/__init__.py 85.00% <100.00%> (+0.78%) ⬆️
panel/tests/conftest.py 95.73% <100.00%> (+0.07%) ⬆️
panel/tests/io/test_cache.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Jun 16, 2022

Please support Time to live/ Expiration. A lot of caching use cases (for me) is avoiding too many loads from databases. But we should load from db every day, hour o 5 minutes.

Please consider the maintenance burden. There could be lots of requests coming in for specialized objects. DiskCache is a great, existing package that could be made easier to use in Panel. And it provides persistence which is key both for speeding up in development and production.

@philippjfr
Copy link
Member Author

TTL is already supported. I'm also happy to accept hashing function contributions and you can already provide your own hashing function to supplement/override others. Definitely looking into DiskCache again.

"\n",
"#### Disk caching\n",
"\n",
"If you have `diskcache` installed you can also cache the results to disk by setting `to_disk=True`. The `diskcache` library will then cache the value to the supplied `cache_path` (defaulting to `./cache`). Making use of disk caching allows you to cache items even if the server is restarted.\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the support for diskcache

"\n",
"data = pn.state.as_cached('data', load_data, *args, **kwargs)\n",
"```\n",
"\n",
"The first time the app is loaded the data will be cached and subsequent sessions will simply look up the data in the cache, speeding up the process of rendering. If you want to warm up the cache before the first user visits the application you can also provide the `--warm` argument to the `panel serve` command, which will ensure the application is initialized as soon as it is launched. If you want to populate the cache in a separate script from your main application you may also provide the path to a setup script using the `--setup` argument to `panel serve`. If you want to periodically update the cache look into the ability to [schedule tasks](Deploy_and_Export.ipynb#Scheduling-task-with-pn.state.schedule_task)."
"The first time the app is loaded the data will be cached and subsequent sessions will simply look up the data in the cache, speeding up the process of rendering. If you want to warm up the cache before the first user visits the application you can also provide the `--warm` argument to the `panel serve` command, which will ensure the application is initialized as soon as it is launched. If you want to populate the cache in a separate script from your main application you may also provide the path to a setup script using the `--setup` argument to `panel serve`. If you want to periodically update the cache look into the ability to [schedule tasks](Deploy_and_Export.ipynb#Scheduling-task-with-pn.state.schedule_task).\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this looks like extensive and great documentation.

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

Successfully merging this pull request may close these issues.

None yet

2 participants