RedisProxy
class RedisProxy implements Connection mixin RedisConnection
Pool-aware Redis connection proxy.
Each proxy represents a named Redis connection. Commands are executed by checking out a connection from the pool, running the command, and releasing the connection back. Context management ensures that stateful commands reuse the same connection within a coroutine.
Traits
Coroutine multi-exec trait.
Constants
| CONNECTION_CONTEXT_PREFIX |
Context key prefix for per-connection pool state. |
| private DEFERRED_RELEASE_OWNER_CONTEXT_KEY_PREFIX |
Context key prefix for the coroutine owning deferred pool cleanup. |
| private CONNECTION_BOUND_METHODS |
Methods that must be called while explicitly holding a pool connection. These methods must remain excluded from Redis facade generation. |
Methods
Execute commands in a pipeline.
Execute commands in a transaction.
Create a new Redis proxy instance.
Get the connection name.
Determine if this connection uses Redis Cluster.
Register a custom Redis connection macro.
Mix another object into the Redis connection.
Determine if a Redis connection macro is registered.
Flush the registered Redis connection macros.
Scan keys matching a pattern.
Scan hash fields matching a pattern.
Scan sorted set members matching a pattern.
Scan set members matching a pattern.
Pass dynamic method calls to a pooled Redis connection.
Release the connection stored in coroutine context.
Discard the connection stored in coroutine context.
Handle subscribe/psubscribe using the coroutine-native subscriber.
Determine which commands must reuse the same connection.
Get a connection from coroutine context, or from redis connection pool.
The key to identify the connection object in coroutine context.
Execute callback with a pinned connection from the pool.
Pin a pool connection in coroutine context for the duration of a callback.
Execute a callback with serialization and compression temporarily disabled.
Create a coroutine-native Redis subscriber.
Subscribe to a set of given channels with wildcards.
Run a command against the Redis database.
Throttle a callback for a maximum number of executions over a given duration.
Funnel a callback for a maximum number of simultaneous executions.
Flush (delete) all Redis keys matching a pattern.
No description
Details
in
MultiExec at line 23
Redis : array<int, mixed>|false)
pipeline(callable|null $callback = null)
Execute commands in a pipeline.
in
MultiExec at line 33
Redis|RedisCluster : array<int, mixed>|false)
transaction(callable|null $callback = null)
Execute commands in a transaction.
at line 85
__construct(PoolFactory $factory, string $poolName, RedisSentinelFactory $sentinelFactory)
Create a new Redis proxy instance.
at line 95
string
getName()
Get the connection name.
at line 103
bool
isCluster()
Determine if this connection uses Redis Cluster.
at line 115
void
macro(string $name, callable|object $macro)
Register a custom Redis connection macro.
Boot-only. Macros persist for the worker lifetime and affect every Redis connection.
at line 125
void
mixin(object $mixin, bool $replace = true)
Mix another object into the Redis connection.
Boot-only. Registered macros persist for the worker lifetime and affect every Redis connection.
at line 133
bool
hasMacro(string $name)
Determine if a Redis connection macro is registered.
at line 143
void
flushMacros()
Flush the registered Redis connection macros.
Boot or tests only. Concurrent coroutines may resolve different methods during a flush.
at line 154
scan(mixed $cursor, mixed ...$arguments)
Scan keys matching a pattern.
at line 166
hScan(mixed $key, mixed $cursor, mixed ...$arguments)
Scan hash fields matching a pattern.
at line 178
zScan(mixed $key, mixed $cursor, mixed ...$arguments)
Scan sorted set members matching a pattern.
at line 190
sScan(mixed $key, mixed $cursor, mixed ...$arguments)
Scan set members matching a pattern.
at line 200
__call(mixed $name, mixed $arguments)
Pass dynamic method calls to a pooled Redis connection.
at line 310
void
releaseContextConnection()
| internal |
Release the connection stored in coroutine context.
at line 327
void
discardContextConnection()
| internal |
Discard the connection stored in coroutine context.
at line 345
protected void
handleSubscribe(string $name, array $arguments)
Handle subscribe/psubscribe using the coroutine-native subscriber.
Creates a dedicated socket connection (not from the pool) and bridges the channel-based subscriber to the Laravel-style callback API.
at line 373
protected bool
shouldUseSameConnection(string $methodName)
Determine which commands must reuse the same connection.
at line 389
protected RedisConnection
getConnection(bool $hasContextConnection, bool $transform = true)
Get a connection from coroutine context, or from redis connection pool.
at line 408
protected string
getContextKey()
The key to identify the connection object in coroutine context.
at line 436
mixed
withConnection(callable $callback, bool $transform = true)
Execute callback with a pinned connection from the pool.
Use this for operations requiring multiple commands on the same connection (e.g., evalSha + getLastError, multi-step Lua operations). The connection is automatically returned to the pool after the callback completes.
If a connection is already stored in coroutine context (e.g., from an active multi/pipeline), that connection is reused and not released.
at line 459
mixed
withPinnedConnection(callable $callback)
Pin a pool connection in coroutine context for the duration of a callback.
All Redis operations inside the callback will reuse the same connection, avoiding multiple pool checkouts. Re-entrant: if a connection is already pinned, the callback runs on it without re-pinning.
at line 490
mixed
withoutSerializationOrCompression(callable $callback)
Execute a callback with serialization and compression temporarily disabled.
Pins a pool connection and disables phpredis serialization and compression for the duration of the callback, then restores settings and releases.
This is needed for rate limiter counters which must be stored as raw integers — phpredis serialization (e.g., igbinary) would corrupt them.
at line 511
Subscriber
subscriber()
Create a coroutine-native Redis subscriber.
Returns a Subscriber with its own dedicated socket connection (not from the pool). Use for the channel-based pub/sub API:
$sub = Redis::subscriber();
$sub->subscribe('channel');
while ($message = $sub->channel()->pop()) { ... }
$sub->close();
at line 641
void
listen(Closure $callback)
Register a Redis command listener with the connection.
Boot-only. The listener persists on the singleton event dispatcher for the worker lifetime and runs for every subsequent Redis command event.
at line 656
void
listenForFailures(Closure $callback)
Register a Redis command failure listener with the connection.
Boot-only. The listener persists on the singleton event dispatcher for the worker lifetime and runs for every subsequent Redis failure event.
at line 668
void
subscribe(array|string $channels, Closure $callback)
Subscribe to a set of given channels for messages.
at line 676
void
psubscribe(array|string $channels, Closure $callback)
Subscribe to a set of given channels with wildcards.
at line 684
mixed
command(string $method, array $parameters = [])
Run a command against the Redis database.
at line 692
DurationLimiterBuilder
throttle(string $name)
Throttle a callback for a maximum number of executions over a given duration.
at line 700
ConcurrencyLimiterBuilder
funnel(string $name)
Funnel a callback for a maximum number of simultaneous executions.
at line 723
int
flushByPattern(string $pattern)
Flush (delete) all Redis keys matching a pattern.
Use this for standalone/one-off flush operations. It handles the connection lifecycle automatically (get from pool, flush, release). Uses the default connection, or specify one via Redis::connection($name)->flushByPattern().
If you already have a raw connection from withConnection(..., transform: false), call $connection->flushByPattern() directly to avoid redundant pool operations.
Uses SCAN to iterate keys efficiently and deletes them in batches. Correctly handles OPT_PREFIX to avoid the double-prefixing bug.
at line 36
Redis
discard()
No description