-
Notifications
You must be signed in to change notification settings - Fork 4k
Add a TTLCache that supports release hooks. #13415
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
Conversation
This will be used to implement resource caches that close resources when they leave the cache.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
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.
Pull request overview
This PR adds a TTLCleanupCache class that extends cachetools.TTLCache to support release hooks, enabling automatic cleanup of resources when they are evicted from the cache. This is part of implementing session-scoped connections and resource caches.
Key changes:
- Adds
TTLCleanupCacheclass that overridespopitem()andexpire()to call release hooks when items are removed - Introduces
OnReleasetype alias for cleanup callback functions - Provides comprehensive unit tests covering both max-size and TTL-based eviction scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
lib/streamlit/runtime/caching/ttl_cleanup_cache.py |
New module implementing TTLCleanupCache class with release hook functionality |
lib/streamlit/runtime/caching/cache_utils.py |
Adds OnRelease type alias and reorganizes imports to support the new cache class |
lib/tests/streamlit/runtime/caching/ttl_cleanup_cache_test.py |
New test file with unit tests verifying release hook behavior for size-based and TTL-based evictions |
|
@cursor review |
Fix to match Copilot testing style guide.
SummaryThis PR adds a new Key changes:
Code QualityIssues Found:
Suggestions:
Test CoverageThe tests cover the two main automatic eviction scenarios:
Missing Test Cases:
Backwards CompatibilityThis is a significant version jump. Users who have The bump is necessary because:
Recommendation: Consider adding a note in the PR description or release notes about this dependency change. Security & Risk
RecommendationsRequired (blocking):
Recommended (non-blocking):
VerdictCHANGES REQUESTED: The PR is well-structured and the implementation is solid, but it needs to conform to codebase standards by adding This is an automated AI review. Please verify the feedback and use your judgment. |
|
Overall, lgtm 👍 but I think these two aspects from the AI review are probably valid:
It might be good to ensure that the cache is always cleaned, but I'm unsure whether we should simply log the error to the console or display it to the user.
Based on the spec, we likely want to call |
This is not how the base class works.
I already replied to the AI on this, but it doesn't seem smart enough to listen to older comments. The base class handles this. |
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.
Ok 👍 I guess if an exception happens within the release function, an uncaught exception error is shown to the user?
We'll be catching it and wrapping it if possible - but yes, it would end up shown to the user. The wrapping will happen in a higher layer. |
Describe your changes
Add a subclass of TTLCache that supports release hooks.
This will be used to implement resource caches that close resources when they leave the cache. See the spec.
Screenshot or video (only for visual changes)
GitHub Issue Link (if applicable)
Testing Plan
Added.
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.