PHP Class Predis\Connection\Aggregate\RedisCluster

This connection backend offers smart support for redis-cluster by handling automatic slots map (re)generation upon -MOVED or -ASK responses returned by Redis when redirecting a client to a different node. The cluster can be pre-initialized using only a subset of the actual nodes in the cluster, Predis will do the rest by adjusting the slots map and creating the missing underlying connection instances on the fly. It is possible to pre-associate connections to a slots range with the "slots" parameter in the form "$first-$last". This can greatly reduce runtime node guessing and redirections. It is also possible to ask for the full and updated slots map directly to one of the nodes and optionally enable such a behaviour upon -MOVED redirections. Asking for the cluster configuration to Redis is actually done by issuing a CLUSTER SLOTS command to a random node in the pool.
Author: Daniele Alessandri ([email protected])
Inheritance: implements ClusterInterface, implements IteratorAggregate, implements Countable
Datei anzeigen Open project: tillkruss/redis-object-cache Class Usage Examples

Public Methods

Method Description
__construct ( Predis\Connection\FactoryInterface $connections, Predis\Cluster\StrategyInterface $strategy = null )
add ( Predis\Connection\NodeConnectionInterface $connection )
askSlotsMap ( Predis\Connection\NodeConnectionInterface $connection = null ) : array Generates an updated slots map fetching the cluster configuration using the CLUSTER SLOTS command against the specified node or a random one from the pool.
buildSlotsMap ( ) : array Generates the current slots map by guessing the cluster configuration out of the connection parameters of the connections in the pool.
connect ( )
count ( )
disconnect ( )
executeCommand ( Predis\Command\CommandInterface $command )
getClusterStrategy ( ) : Predis\Cluster\StrategyInterface Returns the underlying command hash strategy used to hash commands by using keys found in their arguments.
getConnection ( Predis\Command\CommandInterface $command )
getConnectionById ( $connectionID )
getConnectionBySlot ( integer $slot ) : Predis\Connection\NodeConnectionInterface Returns the connection currently associated to a given slot.
getConnectionFactory ( ) : Predis\Connection\FactoryInterface Returns the underlying connection factory used to create new connection instances to Redis nodes indicated by redis-cluster.
getIterator ( )
getSlotsMap ( ) : array Returns the current slots map for the cluster.
isConnected ( )
readResponse ( Predis\Command\CommandInterface $command )
remove ( Predis\Connection\NodeConnectionInterface $connection )
removeById ( string $connectionID ) : boolean Removes a connection instance by using its identifier.
resetSlotsMap ( ) Resets the slots map cache.
setRetryLimit ( integer $retry ) Sets the maximum number of retries for commands upon server failure.
setSlots ( integer $first, integer $last, Predis\Connection\NodeConnectionInterface | string $connection ) Pre-associates a connection to a slots range to avoid runtime guessing.
useClusterSlots ( boolean $value ) Enables automatic fetching of the current slots map from one of the nodes using the CLUSTER SLOTS command. This option is enabled by default as asking the current slots map to Redis upon -MOVED responses may reduce overhead by eliminating the trial-and-error nature of the node guessing procedure, mostly when targeting many keys that would end up in a lot of redirections.
writeRequest ( Predis\Command\CommandInterface $command )

Protected Methods

Method Description
createConnection ( string $connectionID ) : Predis\Connection\NodeConnectionInterface Creates a new connection instance from the given connection ID.
getRandomConnection ( ) : Predis\Connection\NodeConnectionInterface | null Returns a random connection from the pool.
guessNode ( integer $slot ) : string Guesses the correct node associated to a given slot using a precalculated slots map, falling back to the same logic used by Redis to initialize a cluster (best-effort).
move ( Predis\Connection\NodeConnectionInterface $connection, integer $slot ) Permanently associates the connection instance to a new slot.
onAskResponse ( Predis\Command\CommandInterface $command, string $details ) : mixed Handles -ASK responses by executing again the command against the node indicated by the Redis response.
onErrorResponse ( Predis\Command\CommandInterface $command, ErrorResponseInterface $error ) : mixed Handles -ERR responses returned by Redis.
onMovedResponse ( Predis\Command\CommandInterface $command, string $details ) : mixed Handles -MOVED responses by executing again the command against the node indicated by the Redis response.

Private Methods

Method Description
queryClusterNodeForSlotsMap ( Predis\Connection\NodeConnectionInterface $connection ) : mixed Queries the specified node of the cluster to fetch the updated slots map.
retryCommandOnFailure ( Predis\Command\CommandInterface $command, string $method ) : mixed Ensures that a command is executed one more time on connection failure.

Method Details

__construct() public method

public __construct ( Predis\Connection\FactoryInterface $connections, Predis\Cluster\StrategyInterface $strategy = null )
$connections Predis\Connection\FactoryInterface Optional connection factory.
$strategy Predis\Cluster\StrategyInterface Optional cluster strategy.

add() public method

public add ( Predis\Connection\NodeConnectionInterface $connection )
$connection Predis\Connection\NodeConnectionInterface

askSlotsMap() public method

Generates an updated slots map fetching the cluster configuration using the CLUSTER SLOTS command against the specified node or a random one from the pool.
public askSlotsMap ( Predis\Connection\NodeConnectionInterface $connection = null ) : array
$connection Predis\Connection\NodeConnectionInterface Optional connection instance.
return array

buildSlotsMap() public method

Generation is based on the same algorithm used by Redis to generate the cluster, so it is most effective when all of the connections supplied on initialization have the "slots" parameter properly set accordingly to the current cluster configuration.
public buildSlotsMap ( ) : array
return array

connect() public method

public connect ( )

count() public method

public count ( )

createConnection() protected method

Creates a new connection instance from the given connection ID.
protected createConnection ( string $connectionID ) : Predis\Connection\NodeConnectionInterface
$connectionID string Identifier for the connection.
return Predis\Connection\NodeConnectionInterface

disconnect() public method

public disconnect ( )

executeCommand() public method

public executeCommand ( Predis\Command\CommandInterface $command )
$command Predis\Command\CommandInterface

getClusterStrategy() public method

Returns the underlying command hash strategy used to hash commands by using keys found in their arguments.
public getClusterStrategy ( ) : Predis\Cluster\StrategyInterface
return Predis\Cluster\StrategyInterface

getConnection() public method

public getConnection ( Predis\Command\CommandInterface $command )
$command Predis\Command\CommandInterface

getConnectionById() public method

public getConnectionById ( $connectionID )

getConnectionBySlot() public method

Returns the connection currently associated to a given slot.
public getConnectionBySlot ( integer $slot ) : Predis\Connection\NodeConnectionInterface
$slot integer Slot index.
return Predis\Connection\NodeConnectionInterface

getConnectionFactory() public method

Returns the underlying connection factory used to create new connection instances to Redis nodes indicated by redis-cluster.
public getConnectionFactory ( ) : Predis\Connection\FactoryInterface
return Predis\Connection\FactoryInterface

getIterator() public method

public getIterator ( )

getRandomConnection() protected method

Returns a random connection from the pool.
protected getRandomConnection ( ) : Predis\Connection\NodeConnectionInterface | null
return Predis\Connection\NodeConnectionInterface | null

getSlotsMap() public method

The order of the returned $slot => $server dictionary is not guaranteed.
public getSlotsMap ( ) : array
return array

guessNode() protected method

Guesses the correct node associated to a given slot using a precalculated slots map, falling back to the same logic used by Redis to initialize a cluster (best-effort).
protected guessNode ( integer $slot ) : string
$slot integer Slot index.
return string Connection ID.

isConnected() public method

public isConnected ( )

move() protected method

The connection is added to the connections pool if not yet included.
protected move ( Predis\Connection\NodeConnectionInterface $connection, integer $slot )
$connection Predis\Connection\NodeConnectionInterface Connection instance.
$slot integer Target slot index.

onAskResponse() protected method

Handles -ASK responses by executing again the command against the node indicated by the Redis response.
protected onAskResponse ( Predis\Command\CommandInterface $command, string $details ) : mixed
$command Predis\Command\CommandInterface Command that generated the -ASK response.
$details string Parameters of the -ASK response.
return mixed

onErrorResponse() protected method

Handles -ERR responses returned by Redis.
protected onErrorResponse ( Predis\Command\CommandInterface $command, ErrorResponseInterface $error ) : mixed
$command Predis\Command\CommandInterface Command that generated the -ERR response.
$error ErrorResponseInterface Redis error response object.
return mixed

onMovedResponse() protected method

Handles -MOVED responses by executing again the command against the node indicated by the Redis response.
protected onMovedResponse ( Predis\Command\CommandInterface $command, string $details ) : mixed
$command Predis\Command\CommandInterface Command that generated the -MOVED response.
$details string Parameters of the -MOVED response.
return mixed

readResponse() public method

public readResponse ( Predis\Command\CommandInterface $command )
$command Predis\Command\CommandInterface

remove() public method

public remove ( Predis\Connection\NodeConnectionInterface $connection )
$connection Predis\Connection\NodeConnectionInterface

removeById() public method

Removes a connection instance by using its identifier.
public removeById ( string $connectionID ) : boolean
$connectionID string Connection identifier.
return boolean True if the connection was in the pool.

resetSlotsMap() public method

Resets the slots map cache.
public resetSlotsMap ( )

setRetryLimit() public method

-1 = unlimited retry attempts 0 = no retry attempts (fails immediatly) n = fail only after n retry attempts
public setRetryLimit ( integer $retry )
$retry integer Number of retry attempts.

setSlots() public method

Pre-associates a connection to a slots range to avoid runtime guessing.
public setSlots ( integer $first, integer $last, Predis\Connection\NodeConnectionInterface | string $connection )
$first integer Initial slot of the range.
$last integer Last slot of the range.
$connection Predis\Connection\NodeConnectionInterface | string ID or connection instance.

useClusterSlots() public method

The slots map can still be manually fetched using the askSlotsMap() method whether or not this option is enabled.
public useClusterSlots ( boolean $value )
$value boolean Enable or disable the use of CLUSTER SLOTS.

writeRequest() public method

public writeRequest ( Predis\Command\CommandInterface $command )
$command Predis\Command\CommandInterface