VOOZH about

URL: https://deepwiki.com/hypervel/redis/6.3-redis-proxy

⇱ Redis Proxy | hypervel/redis | DeepWiki


Loading...
Menu

Redis Proxy

Overview

RedisProxy is a minimal subclass of Redis that binds a Redis client instance to a specific named connection pool. While the base Redis class defaults to the 'default' pool, RedisProxy instances are configured with pool names like 'cache', 'session', or any custom pool defined in configuration.

Related Pages:


Class Structure

RedisProxy extends Redis with only a modified constructor. All runtime behavior is inherited from the parent class.


Diagram: RedisProxy Class Hierarchy with Key Methods

The constructor overrides parent::__construct() to accept a $pool parameter, which sets the $poolName property. All other methods (__call(), getConnection(), etc.) are inherited unchanged.

Sources: src/RedisProxy.php9-17 src/Redis.php17-24


Implementation

Constructor

RedisProxy::__construct() accepts two parameters and configures the pool name:

ParameterTypeDescription
$factoryPoolFactoryHyperf's pool factory (passed to parent)
$poolstringPool name to bind this instance to

Execution flow:

  1. Invokes parent::__construct($factory) src/RedisProxy.php13
  2. Assigns $this->poolName = $pool src/RedisProxy.php15

This ensures all subsequent calls to $this->factory->getPool($this->poolName) in the parent's getConnection() method will retrieve connections from the specified pool.


Diagram: RedisProxy Construction Sequence

Sources: src/RedisProxy.php11-16 src/Redis.php19-24


Implementation

Constructor

The RedisProxy constructor accepts two parameters and delegates to the parent Redis constructor:

ParameterTypePurpose
$factoryPoolFactoryHyperf's pool factory for managing connection pools
$poolstringThe specific pool name this proxy will use

Implementation flow:

  1. Calls parent constructor with $factory parameter src/RedisProxy.php13
  2. Sets $this->poolName to the provided pool name src/RedisProxy.php15

This ensures that all subsequent Redis commands executed through this proxy instance will use the specified pool rather than the default pool.


Diagram: RedisProxy Construction Sequence

Sources: src/RedisProxy.php11-16


Pool Name Binding

The $poolName property controls which connection pool is used for all operations:

Class$poolName ValueSet ByImmutability
Redis'default'Parent class property initialization src/Redis.php19Can change after construction
RedisProxyConstructor parameter$this->poolName = $pool src/RedisProxy.php15Fixed at construction

Key Mechanism:

When Redis::__call() executes, it calls getConnection(), which invokes:

$this->factory->getPool($this->poolName)->get()

src/Redis.php116

Since RedisProxy sets $this->poolName in its constructor, all connections retrieved through that instance come from the configured pool.


Inherited Functionality

RedisProxy inherits all parent methods without override:

MethodInherited FromPurpose
__call($name, $arguments)Redis src/Redis.php26-77Intercepts all Redis commands
getConnection($hasContextConnection)Redis src/Redis.php109-123Retrieves connection from pool or context
getContextKey()Redis src/Redis.php128-131Returns context key using $poolName
shouldUseSameConnection($methodName)Redis src/Redis.php97-104Determines if command requires context storage
releaseContextConnection()Redis src/Redis.php82-91Releases connection stored in context
connection($name)Redis src/Redis.php136-141Creates new proxy for different pool

Command Execution Flow:


Diagram: Method Invocation Chain for RedisProxy Commands

The $this->poolName property (set by RedisProxy constructor) determines which pool PoolFactory::getPool() accesses.

Sources: src/RedisProxy.php9-17 src/Redis.php26-141


Creation by RedisFactory

RedisProxy instances are created by RedisFactory during construction. The factory iterates over configured pools and instantiates a proxy for each:


Diagram: RedisFactory Creating RedisProxy Instances

The $proxies array maintains one RedisProxy instance per configured pool. Methods like RedisFactory::get($poolName) and RedisFactory::make($poolName) return the pre-created proxy from this array.

Sources: src/RedisProxy.php9-17


Usage Examples

Obtaining RedisProxy Instances

Applications typically obtain RedisProxy instances through RedisFactory:

Access PatternMethodReturns
Explicit$factory->make('cache')RedisProxy with $poolName = 'cache'
Explicit$factory->get('cache')Same as make()
Magic method$factory->cache()Same as make('cache') via __call()

Example Usage


Comparison: Redis vs RedisProxy

PropertyRedisRedisProxy
$poolName initialization'default' src/Redis.php19Constructor parameter src/RedisProxy.php15
Typical instantiationDirect: new Redis($factory)Via factory: $factory->get('cache')
Pool bindingMutableImmutable after construction
Primary use caseDefault pool operationsNamed pool isolation

Pool Isolation Mechanism

Each RedisProxy instance is permanently bound to one pool, ensuring complete resource isolation:


Diagram: Pool Isolation Through RedisProxy Binding

Isolation Benefits:

AspectMechanismResult
Connection separation$poolName determines pool in getConnection()No cross-pool connection usage
Configuration independenceEach pool has separate config in redis.phpDifferent hosts, databases, limits per pool
Context isolationgetContextKey() uses $poolName src/Redis.php130Separate context keys per pool
Resource limitsPool-specific max_connectionsCache pool exhaustion doesn't affect session pool

Sources: src/RedisProxy.php9-17 src/Redis.php109-131


Technical Considerations

Memory Footprint

Each RedisProxy instance has minimal memory overhead:

  • Extends Redis base class (inherits properties)
  • No additional properties beyond those inherited
  • Pool name stored in parent's $poolName property

Performance Characteristics

RedisProxy introduces no performance overhead:

  • Constructor overhead is negligible (one method call + one assignment)
  • All runtime operations delegate to parent class methods
  • No method overrides that would add execution layers

Thread Safety

In Hyperf's coroutine context:

  • Each RedisProxy instance is safe for concurrent access
  • Connection pool handles concurrency at the pool level
  • Context management (inherited from Redis) ensures transaction isolation

Sources: src/RedisProxy.php1-18


Design Rationale

The RedisProxy class follows the Proxy Pattern with minimal implementation:

Benefits:

  1. Simplicity: Only 18 lines of code with clear purpose
  2. Type safety: Provides distinct instances for each pool
  3. Extensibility: Can be extended for pool-specific customizations
  4. Factory compatibility: Integrates cleanly with RedisFactory pattern

Alternative approaches considered:

  • Method-based pool selection: Would require passing pool name to every command
  • Dynamic pool switching: Would complicate connection lifecycle management
  • Pool-specific classes: Would require maintaining multiple full implementations

The chosen approach balances simplicity with functionality, providing a lightweight mechanism for pool-specific client instances.

Sources: src/RedisProxy.php1-18