Return 0 from MemcachedCache._increment when the result is 0 - #1082
Open
uttam12331 wants to merge 1 commit into
Open
Return 0 from MemcachedCache._increment when the result is 0#1082uttam12331 wants to merge 1 commit into
uttam12331 wants to merge 1 commit into
Conversation
_increment returned `incremented or delta`, where the `or delta` fallback is meant to cover the missing-key case (aiomcache incr/decr return None). But 0 is also falsy, so decrementing a counter to exactly 0 returned the delta instead of 0 -- e.g. decrementing a counter of 5 by 5 returned -5. Return delta only when the client returned None (missing key), so a real result of 0 is preserved. This matches the memory and valkey backends, which return the true post-increment value.
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.
Problem
MemcachedCache.incrementreports the wrong value when a counter is decremented to exactly0:aiomcache'sincr/decrreturn the new value, orNoneif the key is missing._incrementends with:The
or deltafallback is there to handle the missing-key case (incremented is None). But0is also falsy, so when the counter reaches exactly0,0 or deltareturnsdeltainstead of0.Impact
A decrement (or increment) whose true result is
0returns the delta instead —increment("counter", -5)on a counter of5returns-5while a subsequentgetreturns0. Thememoryandvalkeybackends both return the true post-increment value (andBaseCache.incrementdocuments the return as "Value of the key once incremented"), so only the memcached backend diverges.Fix
This keeps the missing-key behavior (return
deltawhen the client returnedNone) while correctly returning a real0.Tests
Added
test_increment_result_zerototests/ut/backends/test_memcached.py, asserting that when the client'sdecrreturns0,_incrementreturns0. It fails onmaster(returns-5) and passes with the fix.