[12.x] Introduce ComputesOnceableHashInterface
#56009
Merged
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.
When using the
once
helper and using object used variables in the callable, thespl_object_hash
function is used.This becomes an issue when using value objects, as each value object would very likely be unique, therefore trying to utilize the Onceable cache doesn't work, even if those value objects carry the same value inside.
Imagine a scenario, where we have a value object named
PositiveInteger
, which self-validates that the carried value is an integer of value higher than zero:Now imagine we're trying to utilize the onceable helper for caching some database queries during the request/job:
Calling the same method twice, passing a value object which carries the same value (and therefore returns the same result) would still make the callable execute twice, even though that would be undesired:
Instead now, we could tell our value object to specify it's hash, allowing the onceable helper to call a method on the used object dependency.
Now, we can do this instead:
We've now effectively ensured that the database would only be called once.