[11.x] Ensure datetime cache durations account for script execution time#53431
Merged
taylorotwell merged 1 commit intolaravel:11.xfrom Nov 7, 2024
Merged
[11.x] Ensure datetime cache durations account for script execution time#53431taylorotwell merged 1 commit intolaravel:11.xfrom
taylorotwell merged 1 commit intolaravel:11.xfrom
Conversation
6349d81 to
b9b50c0
Compare
b9b50c0 to
706913c
Compare
timacdonald
commented
Nov 6, 2024
Comment on lines
+627
to
+629
| $duration = (int) ceil( | ||
| Carbon::now()->diffInMilliseconds($duration, false) / 1000 | ||
| ); |
Member
Author
There was a problem hiding this comment.
I've changed this from diffInSeconds to diffInMilliseconds / 1000 because Carbon 2 would always return an int while carbon 3 returns a float.
This allows us to ensure we get the correct value on both versions.
browner12
reviewed
Nov 6, 2024
This file contains hidden or 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
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 addresses a rounding bug when datetime or intervals are used with the cache.
Surprisingly, this will not put the value into the cache. It will instead call
Cache::forgetunder the hood.This is because by the time we do a diff between
now()and$ttl = now()->addSecond()some time has passed during PHP execution, even if only microseconds. That means the diff is ~0.999 seconds, which we cast to anintresulting in0.This also impacts date time intervals, because we convert those to datetime instances.
I have a gut feeling this also impacts other parts of the framework where we pull the seconds off a datetime, such as the queue system. Also anything that uses
InteractsWithTimemay also be impacted. Some of those values are likely off by a second. I haven't investigated this to confirm, though.