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

Redis : array<int, mixed>|false)
pipeline(callable|null $callback = null)

Execute commands in a pipeline.

Redis|RedisCluster : array<int, mixed>|false)
transaction(callable|null $callback = null)

Execute commands in a transaction.

__construct(PoolFactory $factory, string $poolName, RedisSentinelFactory $sentinelFactory)

Create a new Redis proxy instance.

string
getName()

Get the connection name.

bool
isCluster()

Determine if this connection uses Redis Cluster.

void
macro(string $name, callable|object $macro)

Register a custom Redis connection macro.

void
mixin(object $mixin, bool $replace = true)

Mix another object into the Redis connection.

bool
hasMacro(string $name)

Determine if a Redis connection macro is registered.

void
flushMacros()

Flush the registered Redis connection macros.

scan(mixed $cursor, mixed ...$arguments)

Scan keys matching a pattern.

hScan(mixed $key, mixed $cursor, mixed ...$arguments)

Scan hash fields matching a pattern.

zScan(mixed $key, mixed $cursor, mixed ...$arguments)

Scan sorted set members matching a pattern.

sScan(mixed $key, mixed $cursor, mixed ...$arguments)

Scan set members matching a pattern.

__call(mixed $name, mixed $arguments)

Pass dynamic method calls to a pooled Redis connection.

void
releaseContextConnection()

Release the connection stored in coroutine context.

void
discardContextConnection()

Discard the connection stored in coroutine context.

void
handleSubscribe(string $name, array $arguments)

Handle subscribe/psubscribe using the coroutine-native subscriber.

bool
shouldUseSameConnection(string $methodName)

Determine which commands must reuse the same connection.

getConnection(bool $hasContextConnection, bool $transform = true)

Get a connection from coroutine context, or from redis connection pool.

string
getContextKey()

The key to identify the connection object in coroutine context.

mixed
withConnection(callable $callback, bool $transform = true)

Execute callback with a pinned connection from the pool.

mixed
withPinnedConnection(callable $callback)

Pin a pool connection in coroutine context for the duration of a callback.

mixed
withoutSerializationOrCompression(callable $callback)

Execute a callback with serialization and compression temporarily disabled.

subscriber()

Create a coroutine-native Redis subscriber.

void
listen(Closure $callback)

Register a Redis command listener with the connection.

void
listenForFailures(Closure $callback)

Register a Redis command failure listener with the connection.

void
subscribe(array|string $channels, Closure $callback)

Subscribe to a set of given channels for messages.

void
psubscribe(array|string $channels, Closure $callback)

Subscribe to a set of given channels with wildcards.

mixed
command(string $method, array $parameters = [])

Run a command against the Redis database.

throttle(string $name)

Throttle a callback for a maximum number of executions over a given duration.

funnel(string $name)

Funnel a callback for a maximum number of simultaneous executions.

int
flushByPattern(string $pattern)

Flush (delete) all Redis keys matching a pattern.

Redis
discard()

No description

Details

in MultiExec at line 23
Redis : array<int, mixed>|false) pipeline(callable|null $callback = null)

Execute commands in a pipeline.

Parameters

callable|null $callback

Return Value

Redis : array<int, mixed>|false)

in MultiExec at line 33
Redis|RedisCluster : array<int, mixed>|false) transaction(callable|null $callback = null)

Execute commands in a transaction.

Parameters

callable|null $callback

Return Value

Redis|RedisCluster : array<int, mixed>|false)

at line 85
__construct(PoolFactory $factory, string $poolName, RedisSentinelFactory $sentinelFactory)

Create a new Redis proxy instance.

Parameters

PoolFactory $factory
string $poolName
RedisSentinelFactory $sentinelFactory

at line 95
string getName()

Get the connection name.

Return Value

string

at line 103
bool isCluster()

Determine if this connection uses Redis Cluster.

Return Value

bool

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.

Parameters

string $name
callable|object $macro

Return Value

void

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.

Parameters

object $mixin
bool $replace

Return Value

void

at line 133
bool hasMacro(string $name)

Determine if a Redis connection macro is registered.

Parameters

string $name

Return Value

bool

at line 143
void flushMacros()

Flush the registered Redis connection macros.

Boot or tests only. Concurrent coroutines may resolve different methods during a flush.

Return Value

void

at line 154
scan(mixed $cursor, mixed ...$arguments)

Scan keys matching a pattern.

Parameters

mixed $cursor
mixed ...$arguments

at line 166
hScan(mixed $key, mixed $cursor, mixed ...$arguments)

Scan hash fields matching a pattern.

Parameters

mixed $key
mixed $cursor
mixed ...$arguments

at line 178
zScan(mixed $key, mixed $cursor, mixed ...$arguments)

Scan sorted set members matching a pattern.

Parameters

mixed $key
mixed $cursor
mixed ...$arguments

at line 190
sScan(mixed $key, mixed $cursor, mixed ...$arguments)

Scan set members matching a pattern.

Parameters

mixed $key
mixed $cursor
mixed ...$arguments

at line 200
__call(mixed $name, mixed $arguments)

Pass dynamic method calls to a pooled Redis connection.

Parameters

mixed $name
mixed $arguments

at line 310
void releaseContextConnection()

internal  
 

Release the connection stored in coroutine context.

Return Value

void

at line 327
void discardContextConnection()

internal  
 

Discard the connection stored in coroutine context.

Return Value

void

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.

Parameters

string $name
array $arguments

Return Value

void

at line 373
protected bool shouldUseSameConnection(string $methodName)

Determine which commands must reuse the same connection.

Parameters

string $methodName

Return Value

bool

at line 389
protected RedisConnection getConnection(bool $hasContextConnection, bool $transform = true)

Get a connection from coroutine context, or from redis connection pool.

Parameters

bool $hasContextConnection

Whether a connection exists in coroutine context

bool $transform

Whether to enable Laravel-style result transformation

Return Value

RedisConnection

at line 408
protected string getContextKey()

The key to identify the connection object in coroutine context.

Return Value

string

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.

Parameters

callable $callback
bool $transform

Whether to enable Laravel-style result transformation (default: true)

Return Value

mixed

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.

Parameters

callable $callback

Return Value

mixed

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.

Parameters

callable $callback

Return Value

mixed

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();

Return Value

Subscriber

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.

Parameters

Closure $callback

Return Value

void

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.

Parameters

Closure $callback

Return Value

void

at line 668
void subscribe(array|string $channels, Closure $callback)

Subscribe to a set of given channels for messages.

Parameters

array|string $channels
Closure $callback

Return Value

void

at line 676
void psubscribe(array|string $channels, Closure $callback)

Subscribe to a set of given channels with wildcards.

Parameters

array|string $channels
Closure $callback

Return Value

void

at line 684
mixed command(string $method, array $parameters = [])

Run a command against the Redis database.

Parameters

string $method
array $parameters

Return Value

mixed

at line 692
DurationLimiterBuilder throttle(string $name)

Throttle a callback for a maximum number of executions over a given duration.

Parameters

string $name

Return Value

DurationLimiterBuilder

at line 700
ConcurrencyLimiterBuilder funnel(string $name)

Funnel a callback for a maximum number of simultaneous executions.

Parameters

string $name

Return Value

ConcurrencyLimiterBuilder

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.

Parameters

string $pattern

The pattern to match (e.g., "cache:test:*"). Should NOT include OPT_PREFIX - it's handled automatically.

Return Value

int

Number of keys deleted

at line 36
Redis discard()

No description

Return Value

Redis