PHP Класс 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.
Автор: Daniele Alessandri ([email protected])
Наследование: implements ClusterInterface, implements IteratorAggregate, implements Countable
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__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 )

Защищенные методы

Метод Описание
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.

Приватные методы

Метод Описание
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.

Описание методов

__construct() публичный Метод

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 add ( Predis\Connection\NodeConnectionInterface $connection )
$connection Predis\Connection\NodeConnectionInterface

askSlotsMap() публичный Метод

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.
Результат array

buildSlotsMap() публичный Метод

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
Результат array

connect() публичный Метод

public connect ( )

count() публичный Метод

public count ( )

createConnection() защищенный Метод

Creates a new connection instance from the given connection ID.
protected createConnection ( string $connectionID ) : Predis\Connection\NodeConnectionInterface
$connectionID string Identifier for the connection.
Результат Predis\Connection\NodeConnectionInterface

disconnect() публичный Метод

public disconnect ( )

executeCommand() публичный Метод

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

getClusterStrategy() публичный Метод

Returns the underlying command hash strategy used to hash commands by using keys found in their arguments.
public getClusterStrategy ( ) : Predis\Cluster\StrategyInterface
Результат Predis\Cluster\StrategyInterface

getConnection() публичный Метод

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

getConnectionById() публичный Метод

public getConnectionById ( $connectionID )

getConnectionBySlot() публичный Метод

Returns the connection currently associated to a given slot.
public getConnectionBySlot ( integer $slot ) : Predis\Connection\NodeConnectionInterface
$slot integer Slot index.
Результат Predis\Connection\NodeConnectionInterface

getConnectionFactory() публичный Метод

Returns the underlying connection factory used to create new connection instances to Redis nodes indicated by redis-cluster.
public getConnectionFactory ( ) : Predis\Connection\FactoryInterface
Результат Predis\Connection\FactoryInterface

getIterator() публичный Метод

public getIterator ( )

getRandomConnection() защищенный Метод

Returns a random connection from the pool.
protected getRandomConnection ( ) : Predis\Connection\NodeConnectionInterface | null
Результат Predis\Connection\NodeConnectionInterface | null

getSlotsMap() публичный Метод

The order of the returned $slot => $server dictionary is not guaranteed.
public getSlotsMap ( ) : array
Результат array

guessNode() защищенный Метод

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.
Результат string Connection ID.

isConnected() публичный Метод

public isConnected ( )

move() защищенный Метод

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() защищенный Метод

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.
Результат mixed

onErrorResponse() защищенный Метод

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.
Результат mixed

onMovedResponse() защищенный Метод

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.
Результат mixed

readResponse() публичный Метод

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

remove() публичный Метод

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

removeById() публичный Метод

Removes a connection instance by using its identifier.
public removeById ( string $connectionID ) : boolean
$connectionID string Connection identifier.
Результат boolean True if the connection was in the pool.

resetSlotsMap() публичный Метод

Resets the slots map cache.
public resetSlotsMap ( )

setRetryLimit() публичный Метод

-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() публичный Метод

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() публичный Метод

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 writeRequest ( Predis\Command\CommandInterface $command )
$command Predis\Command\CommandInterface