The first two examples ship in the repository's examples/ directory. Run them with php -d extension=/path/to/resp3.so examples/<file>.php against a local Redis or Valkey on 127.0.0.1:6379.
Client smoke test
Uses Resp3Client directly: commands, a pipeline and error replies. Error replies come back as RedisException instances and are not thrown.
<?php declare(strict_types=1);/** * Smoke test for Resp3Client against a local Redis or Valkey on 127.0.0.1:6379. * * php -d extension=/path/to/resp3.so examples/smoke.php */ require __DIR__ . '/../vendor/autoload.php'; use Resp3\Laravel\Client\Resp3Client;use Resp3\RedisException; if (!extension_loaded('resp3')) { fwrite(STDERR, "ext-resp3 not loaded; run with -d extension=...\n"); exit(1);} $c = new Resp3Client(host: '127.0.0.1', port: 6379); $c->command('DEL', 'r3:smoke:str', 'r3:smoke:list', 'r3:smoke:hash', 'r3:smoke:counter'); $c->command('SET', 'r3:smoke:str', 'hello');$got = $c->command('GET', 'r3:smoke:str');assert($got === 'hello', 'GET returned ' . var_export($got, true)); $missing = $c->command('GET', 'r3:smoke:nonexistent');assert($missing === null, 'expected null on missing key, got ' . var_export($missing, true)); $incr = $c->command('INCR', 'r3:smoke:counter');assert(is_int($incr), 'INCR should return int'); $c->command('RPUSH', 'r3:smoke:list', 'a', 'b', 'c');$range = $c->command('LRANGE', 'r3:smoke:list', '0', '-1');assert($range === ['a', 'b', 'c'], 'LRANGE mismatch: ' . json_encode($range)); $c->command('HSET', 'r3:smoke:hash', 'k1', 'v1', 'k2', 'v2');// HGETALL under RESP3 returns a map (associative array) instead of a flat list.$hash = $c->command('HGETALL', 'r3:smoke:hash');assert($hash === ['k1' => 'v1', 'k2' => 'v2'], 'HGETALL mismatch: ' . json_encode($hash)); // Pipeline: send three commands, read three replies in one round trip.$pipe = $c->pipeline([ ['SET', 'r3:smoke:p1', 'one'], ['SET', 'r3:smoke:p2', 'two'], ['MGET', 'r3:smoke:p1', 'r3:smoke:p2'],]);assert($pipe[2] === ['one', 'two'], 'pipeline MGET mismatch: ' . json_encode($pipe)); // Error replies come back as RedisException instances, not thrown.$err = $c->command('SET'); // missing argsassert($err instanceof RedisException, 'expected RedisException for bad args');echo "error preserved: ", $err->getMessage(), "\n"; $c->command('DEL', 'r3:smoke:str', 'r3:smoke:list', 'r3:smoke:hash', 'r3:smoke:counter', 'r3:smoke:p1', 'r3:smoke:p2',); echo "smoke.php: OK\n";
Client-side caching
Builds a Resp3Store with client tracking enabled, reads a key twice (the second read is served locally), lets a second client overwrite the key and reads again. It needs Redis 6+ or Valkey 6+.
<?php declare(strict_types=1);/** * Client-side caching demo against a local Redis or Valkey on 127.0.0.1:6379. * * Builds a Resp3Store with the `client_tracking` block enabled, reads a key * twice (the second read is served from the process-local store), lets a * second client overwrite the key, reads again (the server pushed an * invalidation, so the store fetches the new value) and prints the commands * that actually reached the server. * * php -d extension=/path/to/resp3.so examples/client_tracking.php * * Needs Redis 6+ or Valkey 6+ (CLIENT TRACKING). */ require __DIR__ . '/../vendor/autoload.php'; use Illuminate\Container\Container;use Illuminate\Events\Dispatcher;use Illuminate\Redis\Events\CommandExecuted;use Illuminate\Redis\RedisManager;use Resp3\Laravel\Cache\Resp3Store;use Resp3\Laravel\Client\Resp3Client;use Resp3\Laravel\Connectors\Resp3Connector;use Resp3\Laravel\Tracking\TrackingConfig;use Resp3\RedisException; if (!extension_loaded('resp3')) { fwrite(STDERR, "ext-resp3 not loaded; run with -d extension=...\n"); exit(1);} $node = ['host' => '127.0.0.1', 'port' => 6379, 'database' => 0]; // The cache store, wired by hand. In a Laravel app this is the config block// of a store with 'driver' => 'resp3'.$redis = new RedisManager(new Container(), 'resp3', ['default' => $node]);$redis->extend('resp3', fn () => new Resp3Connector()); $store = new Resp3Store($redis, 'r3ex:', 'default');$store->setClientTracking(TrackingConfig::fromArray([ 'enabled' => true, 'mode' => 'optin', 'local_store' => 'array', // 'auto' picks APCu only for persistent connections 'local_ttl' => 60, 'max_entries' => 1000, 'max_value_bytes' => 65536,])); // Record every command that reaches the server.$seen = [];$events = new Dispatcher();$events->listen(CommandExecuted::class, function (CommandExecuted $event) use (&$seen): void { $seen[] = strtoupper($event->command) . ' ' . implode(' ', array_map('strval', $event->parameters));});$store->connection()->setEventDispatcher($events); // The other application server: a plain client that writes behind our back.$other = new Resp3Client(host: $node['host'], port: $node['port']);if ($other->command('CLIENT', 'TRACKINGINFO') instanceof RedisException) { fwrite(STDERR, "This server has no CLIENT TRACKING.\n"); exit(1);} $step = static function (string $label, array &$seen): void { echo str_pad($label, 46), $seen === [] ? '(no command sent)' : implode(' | ', $seen), "\n"; $seen = [];}; $store->forget('greeting');$seen = []; $store->put('greeting', 'hello', 60);$step('put', $seen); $first = $store->get('greeting');$step("get -> '{$first}'", $seen); $second = $store->get('greeting');$step("get again -> '{$second}'", $seen); // Serialized the same way the store does, so get() can read it back.$other->command('SET', 'r3ex:greeting', serialize('hello from elsewhere'));$third = $store->get('greeting');$step("get after other SET -> '{$third}'", $seen); assert($first === 'hello' && $second === 'hello', 'first two reads should return the stored value');assert($third === 'hello from elsewhere', 'invalidation should force a fresh read'); $store->forget('greeting');$other->close(); echo "client_tracking.php: OK\n";
Hash tags on a cluster
Keys that share a {tag} hash to one slot, so a multi-key read is one round trip. Without the tag, the keys land on different nodes and a raw MGET raises CROSSSLOT keys in request don't hash to the same slot.
Cache::put('{user:42}.profile', $profile);Cache::put('{user:42}.cart', $cart);Cache::many(['{user:42}.profile', '{user:42}.cart']); // one round trip
Pub/Sub
subscribe() and psubscribe() work on single-node and sentinel connections. The loop opens a dedicated socket so the original connection stays free. Returning false from the callback exits the loop cleanly. With pcntl loaded, SIGTERM and SIGINT break the loop and clean up.
use Illuminate\Support\Facades\Redis; Redis::subscribe(['user-events'], function (string $message, string $channel) { echo "[$channel] $message\n";}); Redis::psubscribe(['user.*'], function (string $message, string $channel, string $pattern) { // ...});
On a cluster these two raise BadMethodCallException. Use sharded pub/sub instead.
Sharded pub/sub on a cluster
Channels hash to a slot like keys. Every channel in one ssubscribe() call must hash to the same slot, otherwise a RuntimeException with a CROSSSLOT-style hint is raised. Publish with SPUBLISH so the message lands on the same shard.
use Illuminate\Support\Facades\Redis; Redis::connection()->ssubscribe( ['orders.{user42}.created', 'orders.{user42}.updated'], function (string $message, string $channel) { // ... },); Redis::connection()->command('SPUBLISH', ['orders.{user42}.created', $payload]);
SUNSUBSCRIBE runs on every exit path (callback returning false, SIGTERM/SIGINT with pcntl loaded, socket drop).
Feature overrides
Force a server feature off or back to automatic detection per connection.
'redis' => [ 'client' => 'resp3', 'options' => [ 'features' => ['msetex' => false, 'delex' => 'auto'], ], 'default' => [/* ... */],],