Skip to content

fix: Memcached decrement() gives the wrong sign on a missing key - #10512

Open
mdalikadar wants to merge 1 commit into
codeigniter4:developfrom
mdalikadar:fix/memcached-decrement-initial-value
Open

fix: Memcached decrement() gives the wrong sign on a missing key#10512
mdalikadar wants to merge 1 commit into
codeigniter4:developfrom
mdalikadar:fix/memcached-decrement-initial-value

Conversation

@mdalikadar

Copy link
Copy Markdown

Fixes #10510 (#10510)

Small follow-up to that issue: MemcachedHandler::decrement() was passing
$offset as the initial value for a key that doesn't exist yet, which made
a fresh key end up at +$offset instead of -$offset like every other
cache handler (File, Redis, Predis) gives you.

I swapped it to use 0 instead, since Memcached counters are unsigned and
can't actually go negative — so 0 is the closest sane starting point,
and it matches what Memcached::decrement() already defaults to on its
own.

What I changed:

  • MemcachedHandler::decrement() now passes 0 instead of $offset as
    the initial value
  • Updated the one test that was asserting the old (backwards) behavior
  • Added a changelog entry

@mergeable

mergeable Bot commented Aug 31, 2026

Copy link
Copy Markdown

Hi there, mdalikadar! 👋

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

MemcachedHandler::decrement() passed $offset as Memcached::decrement()'s
initial_value, which is used as-is (not decremented from) when a key
doesn't exist yet. That made a fresh key end up at +$offset instead of
the -$offset every other cache handler (File, Redis, Predis) produces.

Memcached counters are unsigned, so they can't hold a negative initial
value the way the other handlers effectively can. Use 0 instead (also
Memcached::decrement()'s own default), so a fresh key at least stops
going the wrong direction.

Updates the existing MemcachedHandlerTest::testDecrement() expectation
and adds a changelog entry.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: MemcachedHandler::decrement() produces the opposite sign of File/Redis/Predis for a non-existent key

1 participant