-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
Conversation
Codecov Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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. |
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. |
b846372
to
687fa01
Compare
095c680
to
eef754d
Compare
"\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", |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
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 explicitpn.state.cache
persists when autoreloading. This implementation also allows memoizing parameterized methods decorated withparam.depends
.ToDo