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

Add support for resolving instance attributes in the cache decorator #1235

Merged
merged 3 commits into from
Jun 4, 2024

Conversation

swrichards
Copy link
Collaborator

Taiga issue #2524.

This will be followed by a PR to actually use this in the ZGW clients (and will probably also remove a few tests that were really just testing the underlying caching logic, which I've included here in a more isolated form by testing the cache behavior directly).

@swrichards swrichards force-pushed the tasks/2524-cache-keys-with-instance-attrs branch from 2a9424f to db97f03 Compare June 3, 2024 09:55
This commit makes the documentation and typing around the utility
cache decorator a bit more explicit. It also removes generic
kwarg cache option in favour of the explicit timeout argument.
The only other option accepted by the cache setter, other than
`timeout`, is `version`, which we should avoid until there is an
explicit reason to do so to keep the interface explicit.
The `skip_cache` option is currently unusued, and in
order to use it, the decorated method would have to
explicitly declare `skip_cache` as a kwarg option to
control the decorator behavior, which is a pattern we
should ideally avoid.
@swrichards swrichards force-pushed the tasks/2524-cache-keys-with-instance-attrs branch from db97f03 to 19296c1 Compare June 3, 2024 10:34
@swrichards swrichards marked this pull request as ready for review June 3, 2024 10:36
@codecov-commenter
Copy link

codecov-commenter commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 94.07895% with 9 lines in your changes are missing coverage. Please review.

Project coverage is 95.22%. Comparing base (95f207f) to head (e26b691).

Files Patch % Lines
src/open_inwoner/utils/tests/test_utils.py 92.30% 9 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1235      +/-   ##
===========================================
- Coverage    95.22%   95.22%   -0.01%     
===========================================
  Files          968      969       +1     
  Lines        35249    35392     +143     
===========================================
+ Hits         33566    33701     +135     
- Misses        1683     1691       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@swrichards swrichards requested a review from pi-sigma June 3, 2024 10:39
@swrichards swrichards force-pushed the tasks/2524-cache-keys-with-instance-attrs branch from 19296c1 to e26b691 Compare June 3, 2024 12:49
Copy link
Contributor

@pi-sigma pi-sigma left a comment

Choose a reason for hiding this comment

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

Couple minor remarks. I see no tests for the error cases (ValueError, AttributeError). Would be nice to have those, but it's not critical IMO and I don't want to hold you up, so it's optional.

@swrichards
Copy link
Collaborator Author

Thanks @pi-sigma, useful improvements all.

I see no tests for the error cases (ValueError, AttributeError).

I had those in DynamicCacheKeyTest, but that doesn't make much sense, so I've moved them to the CacheBehaviorTest where they properly belong.

This commit expands the cache decorator's key logic to
interpolate instance attribute for methods, in addition
to the regular arguments already supported. The typical
use-case is an API client, where you will want to scope
your method cache keys to instance specific attributes,
like a specific endpoint, to avoid cache hits on cross-API
calls.
@swrichards swrichards force-pushed the tasks/2524-cache-keys-with-instance-attrs branch from e26b691 to e883163 Compare June 3, 2024 16:00
@swrichards swrichards requested a review from pi-sigma June 3, 2024 16:01
@swrichards swrichards requested a review from alextreme June 4, 2024 07:12
@alextreme alextreme merged commit efe3de2 into develop Jun 4, 2024
16 checks passed
@alextreme alextreme deleted the tasks/2524-cache-keys-with-instance-attrs branch June 4, 2024 17:31
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.

4 participants