-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Cache key policies #13791
Merged
Merged
Cache key policies #13791
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chrisguidry
approved these changes
Jun 5, 2024
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.
I dig it! It's a nice interface, just have a think about whether we want those add/subtract operations to be idempotent
chrisguidry
approved these changes
Jun 5, 2024
Comment on lines
+27
to
+55
def __sub__(self, other: str) -> "CompoundCachePolicy": | ||
if not isinstance(other, str): | ||
raise TypeError("Can only subtract strings from key policies.") | ||
if isinstance(self, Inputs): | ||
exclude = self.exclude or [] | ||
return Inputs(exclude=exclude + [other]) | ||
elif isinstance(self, CompoundCachePolicy): | ||
new = Inputs(exclude=[other]) | ||
policies = self.policies or [] | ||
return CompoundCachePolicy(policies=policies + [new]) | ||
else: | ||
new = Inputs(exclude=[other]) | ||
return CompoundCachePolicy(policies=[self, new]) | ||
|
||
def __add__(self, other: "CachePolicy") -> "CompoundCachePolicy": | ||
# adding _None is a no-op | ||
if isinstance(other, _None): | ||
return self | ||
elif isinstance(self, _None): | ||
return other | ||
|
||
if isinstance(self, CompoundCachePolicy): | ||
policies = self.policies or [] | ||
return CompoundCachePolicy(policies=policies + [other]) | ||
elif isinstance(other, CompoundCachePolicy): | ||
policies = other.policies or [] | ||
return CompoundCachePolicy(policies=policies + [self]) | ||
else: | ||
return CompoundCachePolicy(policies=[self, other]) |
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.
🔥 Awesome API
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces the final user-facing feature in the transactions story: cache policies (formerly known as both idempotency key policies and transaction key policies).
At its core, the idea with cache policies is relatively simple: they expose configuration for how your task should cache its results across executions. In this spirit, this PR provides a menu of options for configuring this behavior. Some examples include:
NONE
: never cacheINPUTS
: cache based on a hash of the inputs to the taskTASKDEF
: cache based on the source code of the task functioncache_key_fn
: in a future PR this will create aCustom
policy definitionThis gets interesting in two respects: first off, these policies can be manipulated through basic operations to create more advanced policies:
At a deeper level, these policies configure the transactional behavior of tasks by specifying the location of the transaction record. These records are only written whenever a task commits itself.
cc: @EmilRex