diff --git a/CHANGELOG.md b/CHANGELOG.md index b77f209a..715a7414 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,8 @@ 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 `{}()/\@:`. +- `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 00000000..1030cd35 --- /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 e2ed27f3..f7eae0b6 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, - [], + [ + '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'), @@ -48,7 +53,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 943974ed..d296b59d 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,54 @@ 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()); + } + + 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'); + $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; + } } diff --git a/tests/Fake/Memcached.php b/tests/Fake/Memcached.php index c6cbc008..7872da1e 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