Skip to content

[1.x] feat: auto-detect phpredis, fall back to Predis#18

Merged
imorland merged 5 commits into
1.xfrom
im/phpredis-auto-detect
Mar 6, 2026
Merged

[1.x] feat: auto-detect phpredis, fall back to Predis#18
imorland merged 5 commits into
1.xfrom
im/phpredis-auto-detect

Conversation

@imorland

@imorland imorland commented Mar 6, 2026

Copy link
Copy Markdown
Member

Summary

  • If ext-redis (phpredis) is installed, it is used as the Redis driver automatically. Otherwise Predis is used as a fallback.
  • No configuration change required by consumers — the driver is selected at runtime.
  • Documents persistent connections (persistent, persistent_id), Unix socket connections, and compression options in the README.

Why

Predis opens a new TCP connection per request. phpredis supports persistent connections, reusing the socket across requests within the same PHP-FPM worker. At scale this significantly reduces connection overhead and Valkey/Redis network bandwidth consumption.

Test plan

  • Verify with ext-redis loaded: driver resolves to phpredis
  • Verify without ext-redis: driver falls back to predis
  • Confirm persistent connections work with persistent: true + persistent_id
  • Confirm Unix socket connections work with host: /path/to/redis.sock, port: 0

🤖 Generated with Claude Code

If ext-redis is loaded, use phpredis as the driver. Otherwise fall back
to Predis. No configuration change required by consumers.

Documents persistent connections, Unix socket, and compression options
in the README.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@imorland imorland changed the title feat: auto-detect phpredis, fall back to Predis [1.x] feat: auto-detect phpredis, fall back to Predis Mar 6, 2026
imorland and others added 4 commits March 6, 2026 22:20
The cache subscriber hardcoded a Predis client check, causing it to
immediately abort with "Expected Predis client" when phpredis (ext-redis)
is the active driver.

Route to a new `subscribePhpRedis()` method when the underlying client is
a `\Redis` instance. phpredis `subscribe()` is blocking and callback-based
rather than an iterator — set `OPT_READ_TIMEOUT = -1` for infinite timeout,
and close the connection from within the callback to exit on `--once`.

Predis path is unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
phpredis 6.x accepts a Closure as the second argument to subscribe() at
runtime, but the PHPStan stubs incorrectly type it as array|string. Add
@phpstan-ignore-next-line to suppress the false positive.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Without stubs, IDEs and PHPStan have no type information for \Redis
(provided by ext-redis, not autoloaded via composer). The jetbrains stubs
also carry the correct callable signature for Redis::subscribe(), so the
@phpstan-ignore-next-line workaround is no longer needed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The larastan stubs (which CI resolves) still type subscribe()'s second
argument as array|string, causing a false positive. The jetbrains stubs
fix this locally but don't win the precedence race in CI.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@imorland imorland marked this pull request as ready for review March 6, 2026 22:36
@imorland imorland requested a review from a team as a code owner March 6, 2026 22:36
@imorland imorland merged commit 72da286 into 1.x Mar 6, 2026
27 checks passed
@imorland imorland deleted the im/phpredis-auto-detect branch March 6, 2026 22:36
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