Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions src/BinaryProtocolMemcachedProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace BEAR\QueryRepository;

use Memcached;
use Override;
use Ray\PsrCacheModule\MemcachedProvider;

/** @internal */
final class BinaryProtocolMemcachedProvider extends MemcachedProvider
{
#[Override]
public function get(): Memcached
{
$memcached = parent::get();
// TagAwareAdapter's tag version keys hold control characters, which the text protocol cannot read back,
// so every tagged entry would miss.
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

return $memcached;
}
}
8 changes: 6 additions & 2 deletions src/StorageMemcachedModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'),
Expand All @@ -48,7 +53,6 @@ protected function configure(): void
[
'itemsPool' => ResourceObjectPool::class,
'tagsPool' => TagsPool::class,
'namespace' => CacheNamespace::class,
],
(new InjectionPoints())->addMethod('setLogger', 'poolError'),
);
Expand Down
72 changes: 70 additions & 2 deletions tests-pecl-ext/StorageMemcachedModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
7 changes: 7 additions & 0 deletions tests/Fake/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Loading