Skip to content

Fix the Memcached resource pool: apply the cache version and use the binary protocol - #333

Open
koriym wants to merge 2 commits into
1.xfrom
memcached-cache-namespace
Open

koriym wants to merge 2 commits into
1.xfrom
memcached-cache-namespace

Conversation

@koriym

@koriym koriym commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Fixes two faults in the resource pool that StorageMemcachedModule binds.

  • Cache version: the CacheVersionModule namespace now reaches the pool's MemcachedAdapter, so each version keeps its entries apart. It was passed to TagAwareAdapter, which takes no namespace.
  • Protocol: the pool's Memcached client uses the binary protocol. Over the text protocol, the tag version keys TagAwareAdapter writes could not be read back, so every tagged entry missed. Only the resource pool changes; the Shared pool of Psr6MemcachedModule keeps its protocol.

Users need a Memcached server, and any proxy in front of it, that accepts the binary protocol. A cache version must not contain any of {}()/\@:. Both are noted in the CHANGELOG.

tests-pecl-ext/StorageMemcachedModuleTest.php covers both against a running server.

Root cause and verification: #333 (comment)

Summary by CodeRabbit

  • Bug Fixes
    • Memcached entries are now isolated by cache version, preventing data from an earlier version from being returned.
    • Tagged entries can be read by other application instances until their tags are invalidated.
    • Memcached now uses the binary protocol. The Memcached server and any intervening proxy must support it.

StorageMemcachedModule built the MemcachedAdapter that stores resources
without a namespace and passed CacheNamespace to TagAwareAdapter, which
has no such parameter. Entries stored under one CacheVersionModule
version were therefore read under the next.

Pass CacheNamespace to the MemcachedAdapter, as Psr6MemcachedModule and
StorageRedisDsnModule do, and drop the name-map entry TagAwareAdapter
ignored. The tags pool is the same adapter, so tag versions are kept
per version too. Without CacheVersionModule the namespace stays empty.

A version that holds a character Symfony reserves in a cache key, such
as '/' or '@', now fails when the pool is built. clear() on this pool
bumps the namespace version and never flushes the whole server; nothing
in the package calls it.
TagAwareAdapter keeps tag versions under keys that hold control characters
("\1tags\1"). libmemcached's text protocol stores such keys but cannot read
them back, so every tagged entry, which is every resource entry, missed from
a new pool instance. Symfony's MemcachedAdapter requires the binary protocol
under TagAwareAdapter and enables it in createConnection(); the
MemcachedProvider of ray/psr-cache-module does not.

The resource pool's adapter takes a MemcachedProvider subclass that enables
the binary protocol, bound under the ResourceObjectPool qualifier, so the
application's Shared pool keeps the protocol it had.

The fake Memcached class defined when the extension is absent gains
setOption() and OPT_BINARY_PROTOCOL: PHPStan reads it in place of the
extension stub.
@koriym

koriym commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Root cause and verification

Cache version. TagAwareAdapter::__construct() takes $itemsPool, $tagsPool and $knownTagVersionsTtl, so the 'namespace' => CacheNamespace::class given to it matched nothing. MemcachedAdapter was bound with an empty name map, so its $namespace kept its default ''.

Protocol.

  • TagAwareAdapter (symfony/cache 8.2.0) keeps each tag's version under {tag} + "\1tags\1". A version it cannot read counts as invalidated, so the entry misses.
  • With php-memcached 3.4.0 (libmemcached 1.0.18) on the text protocol, such a key is stored, but the client cannot read it back, although a raw get over a socket returns the value. memcached's doc/protocol.txt excludes control characters from text-protocol keys.
  • The docblock of Symfony's MemcachedAdapter discourages using it with TagAwareAdapter and says OPT_BINARY_PROTOCOL must be enabled; MemcachedAdapter::createConnection() enables it by default. Ray's MemcachedProvider returns new Memcached() with only its servers added, so the text protocol was in use.

Tests against the 1.x StorageMemcachedModule (memcached 1.6.45):

  • testEntryStoredUnderPreviousCacheVersionIsNotRead fails at line 66: version next reads the entry version previous stored.
  • testTaggedEntryIsReadByAnotherInjectorUntilItsTagIsInvalidated fails at line 77: a second injector misses the tagged entry.

Both pass on this branch, as does the full suite (946 tests; 14 skipped for Redis and Windows). composer cs and composer sa are clean.

Injector paths. A compiled injector (ray/compiler) gives the resource pool a binary-protocol client, keeps the Shared pool's client on the text protocol, and serves a hit. A MemcachedAdapter restored by unserialize() keeps the binary protocol.

Not verified: a server or proxy that accepts only the text protocol. The READMEs of twemproxy and mcrouter list only the ASCII protocol; managed Memcached services were not checked.

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (aca076f) to head (7581b91).

Additional details and impacted files
@@             Coverage Diff             @@
##                 1.x      #333   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       885       886    +1     
===========================================
  Files            123       124    +1     
  Lines           1965      1971    +6     
===========================================
+ Hits            1965      1971    +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koriym

koriym commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Memcached storage module now uses a provider that enables the binary protocol and passes provider and cache namespace bindings to the resource-object-pool adapter. Tests cover cache-version isolation and tagged-entry access across injectors.

Changes: Memcached storage

Layer / File(s) Summary
Provider configuration and cache behavior
src/BinaryProtocolMemcachedProvider.php, src/StorageMemcachedModule.php, tests/Fake/Memcached.php, tests-pecl-ext/StorageMemcachedModuleTest.php, CHANGELOG.md
The provider enables Memcached's binary protocol. The module binds it to the resource-object-pool adapter and supplies the cache namespace. Tests cover version isolation and tagged entries shared across injectors, including tag invalidation. The changelog records these changes and the Memcached server protocol requirement.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Possibly related PRs

Suggested reviewers: apple-x-co

Merge Risk: Minimal · up to 7581b

The change makes the Memcached resource pool honor the cache version and use the binary protocol so tagged entries read back correctly. No merge-blocking issues were found. Deployments need a Memcached server and any proxy in front of it that accept the binary protocol, as the changelog states.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names two changes that appear in the changeset: applying the cache version to the resource pool's MemcachedAdapter, and switching that pool to the binary protocol via a new BinaryProtocolMem…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names two changes that appear in the changeset: applying the cache version to the resource pool's MemcachedAdapter, and switching that pool to the binary protocol via a new BinaryProtocolMemcachedProvider. The title matches the main change described in the objectives and summaries. It is specific, uses the same terms as the code, and does not include noise.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between aca076f and 7581b91.

Files selected for processing: 5

  • CHANGELOG.md
  • src/BinaryProtocolMemcachedProvider.php
  • src/StorageMemcachedModule.php
  • tests-pecl-ext/StorageMemcachedModuleTest.php
  • tests/Fake/Memcached.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

This branch has not been deployed

No deployments
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.

1 participant