From 7dc71adc6f4d4a1dcee467ed5b0162c75c45ca3b Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 2 Dec 2026 21:50:57 +0900 Subject: [PATCH 1/2] Apply the cache version namespace to the Memcached resource pool 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. --- CHANGELOG.md | 1 + src/StorageMemcachedModule.php | 3 +- tests-pecl-ext/StorageMemcachedModuleTest.php | 59 ++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b77f209a2..45dbf67f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A write whose query carries `null` for an `onGet()` parameter that allows null now addresses its own key, as BEAR.Resource binds that parameter; the key was taken as missing, so the write threw `UnmatchedQuery` or skipped its own key. - A PUT, PATCH or DELETE handler with both `#[RefreshCache]` and `#[Purge]`/`#[Refresh]`, on a class that is neither `#[Cacheable]` nor `#[CacheableResponse]`, whose query lacks a parameter its `onGet()` requires now throws `UnmatchedQuery` before the URIs it names are purged, as on a `#[CacheableResponse]` class. - A write whose purge fails, such as a CDN purge, still runs its other purges and its reads of its own URI and its `#[Refresh]` URIs, then throws the first failure as `CacheStoreException`; it stopped at the first failure and left the other entries stale in the local pool. To report such a write as done while the CDN is down, bind a `PurgerInterface` of your own that records the failure instead of throwing. +- `StorageMemcachedModule` keeps the entries of each `CacheVersionModule` version apart, so a deploy with a new version no longer serves the entries the previous one stored. The version must not contain any of `{}()/\@:`. ## [1.20.2] - 2026-11-13 diff --git a/src/StorageMemcachedModule.php b/src/StorageMemcachedModule.php index e2ed27f33..8ebee0ae2 100644 --- a/src/StorageMemcachedModule.php +++ b/src/StorageMemcachedModule.php @@ -37,7 +37,7 @@ protected function configure(): void $this->bind(LoggerInterface::class)->annotatedWith('poolError')->to(PoolErrorLogger::class); $this->bind(AdapterInterface::class)->annotatedWith(ResourceObjectPool::class)->toConstructor( MemcachedAdapter::class, - [], + ['namespace' => CacheNamespace::class], // Memcached swallows backend failures the same way Redis does: without the cache log // in the adapter's hands, a store that is down is a run of ordinary misses. (new InjectionPoints())->addMethod('setLogger', 'poolError'), @@ -48,7 +48,6 @@ protected function configure(): void [ 'itemsPool' => ResourceObjectPool::class, 'tagsPool' => TagsPool::class, - 'namespace' => CacheNamespace::class, ], (new InjectionPoints())->addMethod('setLogger', 'poolError'), ); diff --git a/tests-pecl-ext/StorageMemcachedModuleTest.php b/tests-pecl-ext/StorageMemcachedModuleTest.php index 943974edc..ed393790b 100644 --- a/tests-pecl-ext/StorageMemcachedModuleTest.php +++ b/tests-pecl-ext/StorageMemcachedModuleTest.php @@ -14,14 +14,32 @@ use Symfony\Component\Cache\Adapter\TagAwareAdapter; use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; +use function bin2hex; +use function explode; +use function extension_loaded; +use function fclose; +use function fsockopen; +use function getenv; +use function is_string; +use function phpversion; +use function random_bytes; +use function sprintf; +use function version_compare; + class StorageMemcachedModuleTest extends TestCase { - public function testNew(): void + private const MEMCACHED_DEFAULT_SERVER = '127.0.0.1:11211'; + private const ENTRY_LIFETIME_SECONDS = 60; + + protected function setUp(): void { - if (!extension_loaded('memcached') || version_compare(phpversion('memcached'), '2.2.0', '<')) { + if (! extension_loaded('memcached') || version_compare(phpversion('memcached'), '2.2.0', '<')) { $this->markTestSkipped('Memcached extension >= 2.2.0 is required'); } + } + public function testNew(): void + { // @see http://php.net/manual/en/memcached.addservers.php $servers = 'mem1.domain.com:11211:33,mem2.domain.com:11211:67'; $module = new QueryRepositoryModule(); @@ -36,4 +54,41 @@ public function testNew(): void $this->assertInstanceOf(MemcachedAdapter::class, $roPool); $this->assertInstanceOf(MemcachedAdapter::class, $etagPool); } + + public function testEntryStoredUnderPreviousCacheVersionIsNotRead(): void + { + $servers = $this->memcachedServer(); + $key = 'entry-' . bin2hex(random_bytes(8)); + $previous = $this->resourceObjectPool($servers, 'previous'); + $previous->save($previous->getItem($key)->set('stored')->expiresAfter(self::ENTRY_LIFETIME_SECONDS)); + + $this->assertTrue($this->resourceObjectPool($servers, 'previous')->getItem($key)->isHit()); + $this->assertFalse($this->resourceObjectPool($servers, 'next')->getItem($key)->isHit()); + } + + private function memcachedServer(): string + { + $server = getenv('MEMCACHED_SERVER'); + $server = is_string($server) && $server !== '' ? $server : self::MEMCACHED_DEFAULT_SERVER; + [$host, $port] = explode(':', $server); + $connection = @fsockopen($host, (int) $port, $errno, $errstr, timeout: 1); + if ($connection === false) { + $this->markTestSkipped(sprintf('no Memcached server on %s', $server)); + } + + fclose($connection); + + return $server; + } + + private function resourceObjectPool(string $servers, string $cacheVersion): TagAwareAdapterInterface + { + $module = new QueryRepositoryModule(); + $module->override(new StorageMemcachedModule($servers)); + $module->override(new CacheVersionModule($cacheVersion)); + $pool = (new Injector($module, __DIR__ . '/tmp'))->getInstance(TagAwareAdapterInterface::class, ResourceObjectPool::class); + $this->assertInstanceOf(TagAwareAdapterInterface::class, $pool); + + return $pool; + } } From 7581b910ba24cab89f992f8d39812aed8794c4bc Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 3 Dec 2026 22:26:42 +0900 Subject: [PATCH 2/2] Talk to Memcached over the binary protocol in the resource pool 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. --- CHANGELOG.md | 1 + src/BinaryProtocolMemcachedProvider.php | 24 +++++++++++++++++++ src/StorageMemcachedModule.php | 7 +++++- tests-pecl-ext/StorageMemcachedModuleTest.php | 13 ++++++++++ tests/Fake/Memcached.php | 7 ++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/BinaryProtocolMemcachedProvider.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 45dbf67f2..715a7414a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A PUT, PATCH or DELETE handler with both `#[RefreshCache]` and `#[Purge]`/`#[Refresh]`, on a class that is neither `#[Cacheable]` nor `#[CacheableResponse]`, whose query lacks a parameter its `onGet()` requires now throws `UnmatchedQuery` before the URIs it names are purged, as on a `#[CacheableResponse]` class. - A write whose purge fails, such as a CDN purge, still runs its other purges and its reads of its own URI and its `#[Refresh]` URIs, then throws the first failure as `CacheStoreException`; it stopped at the first failure and left the other entries stale in the local pool. To report such a write as done while the CDN is down, bind a `PurgerInterface` of your own that records the failure instead of throwing. - `StorageMemcachedModule` keeps the entries of each `CacheVersionModule` version apart, so a deploy with a new version no longer serves the entries the previous one stored. The version must not contain any of `{}()/\@:`. +- `StorageMemcachedModule` serves the resources it stored in earlier requests; they all missed. It talks to Memcached over the binary protocol, so the server and any proxy in front of it must accept that protocol. ## [1.20.2] - 2026-11-13 diff --git a/src/BinaryProtocolMemcachedProvider.php b/src/BinaryProtocolMemcachedProvider.php new file mode 100644 index 000000000..1030cd356 --- /dev/null +++ b/src/BinaryProtocolMemcachedProvider.php @@ -0,0 +1,24 @@ +setOption(Memcached::OPT_BINARY_PROTOCOL, true); + + return $memcached; + } +} diff --git a/src/StorageMemcachedModule.php b/src/StorageMemcachedModule.php index 8ebee0ae2..f7eae0b67 100644 --- a/src/StorageMemcachedModule.php +++ b/src/StorageMemcachedModule.php @@ -13,6 +13,7 @@ use Ray\Di\InjectionPoints; use Ray\PsrCacheModule\Annotation\CacheNamespace; use Ray\PsrCacheModule\MemcachedAdapter; +use Ray\PsrCacheModule\MemcachedProvider; use Ray\PsrCacheModule\Psr6MemcachedModule; use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Component\Cache\Adapter\TagAwareAdapter; @@ -35,9 +36,13 @@ public function __construct( protected function configure(): void { $this->bind(LoggerInterface::class)->annotatedWith('poolError')->to(PoolErrorLogger::class); + $this->bind(MemcachedProvider::class)->annotatedWith(ResourceObjectPool::class)->to(BinaryProtocolMemcachedProvider::class); $this->bind(AdapterInterface::class)->annotatedWith(ResourceObjectPool::class)->toConstructor( MemcachedAdapter::class, - ['namespace' => CacheNamespace::class], + [ + 'provider' => ResourceObjectPool::class, + 'namespace' => CacheNamespace::class, + ], // Memcached swallows backend failures the same way Redis does: without the cache log // in the adapter's hands, a store that is down is a run of ordinary misses. (new InjectionPoints())->addMethod('setLogger', 'poolError'), diff --git a/tests-pecl-ext/StorageMemcachedModuleTest.php b/tests-pecl-ext/StorageMemcachedModuleTest.php index ed393790b..d296b59d9 100644 --- a/tests-pecl-ext/StorageMemcachedModuleTest.php +++ b/tests-pecl-ext/StorageMemcachedModuleTest.php @@ -66,6 +66,19 @@ public function testEntryStoredUnderPreviousCacheVersionIsNotRead(): void $this->assertFalse($this->resourceObjectPool($servers, 'next')->getItem($key)->isHit()); } + public function testTaggedEntryIsReadByAnotherInjectorUntilItsTagIsInvalidated(): void + { + $servers = $this->memcachedServer(); + $key = 'entry-' . bin2hex(random_bytes(8)); + $tag = 'tag-' . bin2hex(random_bytes(8)); + $writer = $this->resourceObjectPool($servers, 'current'); + $writer->save($writer->getItem($key)->set('stored')->tag($tag)->expiresAfter(self::ENTRY_LIFETIME_SECONDS)); + + $this->assertTrue($this->resourceObjectPool($servers, 'current')->getItem($key)->isHit()); + $this->resourceObjectPool($servers, 'current')->invalidateTags([$tag]); + $this->assertFalse($this->resourceObjectPool($servers, 'current')->getItem($key)->isHit()); + } + private function memcachedServer(): string { $server = getenv('MEMCACHED_SERVER'); diff --git a/tests/Fake/Memcached.php b/tests/Fake/Memcached.php index c6cbc0082..7872da1e6 100644 --- a/tests/Fake/Memcached.php +++ b/tests/Fake/Memcached.php @@ -2,8 +2,15 @@ if (! class_exists(\Memcached::class)) { class Memcached { + public const OPT_BINARY_PROTOCOL = 18; + public function addServers(array $servers) { } + + public function setOption(int $option, mixed $value): bool + { + return true; + } } } \ No newline at end of file