-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[9.x] Strict mode for RedisStore: preserve type of numeric values #38933
Conversation
src/Illuminate/Cache/RedisStore.php
Outdated
@@ -342,6 +355,10 @@ protected function serialize($value) | |||
*/ | |||
protected function unserialize($value) | |||
{ | |||
if ($this->strict && filter_var($value, FILTER_VALIDATE_INT)) { |
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 think this needs to be:
if ($this->strict && filter_var($value, FILTER_VALIDATE_INT) !== false) {
filter_var('0', FILTER_VALIDATE_INT)
evaluates to 0
which is PHP falsy so the string '0'
is unexpectedly returned.
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.
Thanks @derekmd Fixed
There are some other inconsistencies on how cache drivers treat numbers and increment/decrement functions:
Not all of the inconsistencies can be solved, but some can. |
I'm going to close this for now. Feel free to resubmit once you're ready to work on this again 👍 |
@driesvints I haven't come to a conclusion on how the issues should be handled. Is it OK to just add some notes to docs? e.g:
|
@halaei just feel free to attempt a PR |
In order to optimize memory consumption and make
RedisStore::increment()
andRedisStore::decrement()
work using the built-in Redisincrby
anddecrby
commands, numeric values are stored as their base 10 string representation. Sadly, it causes a type inconsistency issue. If we get a numeric string from Redis, we can't know if the original value was string or int/float. Currently Laraval returns all numeric values as string:This PR tries to fix this type inconsistency by adding a "strict" mode option to Redis cache configuration, with the default value being true. It may be a breaking change. If the non-strict behavior is more appropriate for an existing application, strict should be set to false.
Alternative choices regarding backward compatibility and the default behavior:
RedisStore
.Let me know if you prefer an alternative choice or the current PR. I can add some relevant tests later.
Note:
incrby
anddecrby
Redis commands doesn't work with float values. Also Redis doesn't store non-integer numbers in a compressed way.Fixes #31345