PhpiredisSocketConnection
extends AbstractConnection
in package
This class provides the implementation of a Predis connection that uses the PHP socket extension for network communication and wraps the phpiredis C extension (PHP bindings for hiredis) to parse the Redis protocol.
This class is intended to provide an optional low-overhead alternative for processing responses from Redis compared to the standard pure-PHP classes. Differences in speed when dealing with short inline responses are practically nonexistent, the actual speed boost is for big multibulk responses when this protocol processor can parse and return responses very fast.
For instructions on how to build and install the phpiredis extension, please consult the repository of the project.
The connection parameters supported by this class are:
- scheme: it can be either 'redis', 'tcp' or 'unix'.
- host: hostname or IP address of the server.
- port: TCP port of the server.
- path: path of a UNIX domain socket when scheme is 'unix'.
- timeout: timeout to perform the connection (default is 5 seconds).
- read_write_timeout: timeout of read / write operations.
Tags
Table of Contents
- $initCommands : mixed
- $parameters : mixed
- $cachedId : mixed
- $reader : mixed
- $resource : mixed
- __construct() : mixed
- __destruct() : mixed
- Disconnects from the server and destroys the underlying resource and the protocol reader resource when PHP's garbage collector kicks in.
- __sleep() : mixed
- {@inheritdoc}
- __toString() : string
- Returns a string representation of the connection.
- __wakeup() : mixed
- {@inheritdoc}
- addConnectCommand() : mixed
- Pushes the given command into a queue of commands executed when establishing the actual connection to Redis.
- connect() : mixed
- {@inheritdoc}
- disconnect() : mixed
- {@inheritdoc}
- executeCommand() : mixed
- {@inheritdoc}
- getParameters() : ParametersInterface
- Returns the parameters used to initialize the connection.
- getResource() : mixed
- Returns the underlying resource used to communicate with Redis.
- isConnected() : mixed
- {@inheritdoc}
- read() : mixed
- {@inheritdoc}
- readResponse() : mixed
- {@inheritdoc}
- writeRequest() : mixed
- {@inheritdoc}
- assertExtensions() : mixed
- Checks if the socket and phpiredis extensions are loaded in PHP.
- assertParameters() : ParametersInterface
- Checks some of the parameters used to initialize the connection.
- createResource() : mixed
- Creates the underlying resource used to communicate with Redis.
- getAddress() : string
- Gets the address of an host from connection parameters.
- getErrorHandler() : Closure
- Returns the handler used by the protocol reader for error responses.
- getIdentifier() : string
- Gets an identifier for the connection.
- getReader() : resource
- Returns the underlying protocol reader resource.
- getStatusHandler() : Closure
- Returns the handler used by the protocol reader for inline responses.
- onConnectionError() : mixed
- Helper method to handle connection errors.
- onProtocolError() : mixed
- Helper method to handle protocol errors.
- write() : mixed
- {@inheritdoc}
- connectWithTimeout() : string
- Opens the actual connection to the server with a timeout.
- createExceptionMessage() : string
- Helper method that returns an exception message augmented with useful details from the connection parameters.
- createReader() : resource
- Creates a new instance of the protocol reader resource.
- emitSocketError() : mixed
- Helper method used to throw exceptions on socket errors.
- setSocketOptions() : mixed
- Sets options on the socket resource from the connection parameters.
Properties
$initCommands
protected
mixed
$initCommands
= array()
$parameters
protected
mixed
$parameters
$cachedId
private
mixed
$cachedId
$reader
private
mixed
$reader
$resource
private
mixed
$resource
Methods
__construct()
public
__construct(ParametersInterface $parameters) : mixed
Parameters
- $parameters : ParametersInterface
-
Initialization parameters for the connection.
Return values
mixed —__destruct()
Disconnects from the server and destroys the underlying resource and the protocol reader resource when PHP's garbage collector kicks in.
public
__destruct() : mixed
Return values
mixed —__sleep()
{@inheritdoc}
public
__sleep() : mixed
Return values
mixed —__toString()
Returns a string representation of the connection.
public
__toString() : string
Return values
string —__wakeup()
{@inheritdoc}
public
__wakeup() : mixed
Return values
mixed —addConnectCommand()
Pushes the given command into a queue of commands executed when establishing the actual connection to Redis.
public
addConnectCommand(CommandInterface $command) : mixed
Parameters
- $command : CommandInterface
-
Instance of a Redis command.
Return values
mixed —connect()
{@inheritdoc}
public
connect() : mixed
Return values
mixed —disconnect()
{@inheritdoc}
public
disconnect() : mixed
Return values
mixed —executeCommand()
{@inheritdoc}
public
executeCommand(CommandInterface $command) : mixed
Parameters
- $command : CommandInterface
Return values
mixed —getParameters()
Returns the parameters used to initialize the connection.
public
getParameters() : ParametersInterface
Return values
ParametersInterface —getResource()
Returns the underlying resource used to communicate with Redis.
public
getResource() : mixed
Return values
mixed —isConnected()
{@inheritdoc}
public
isConnected() : mixed
Return values
mixed —read()
{@inheritdoc}
public
read() : mixed
Return values
mixed —readResponse()
{@inheritdoc}
public
readResponse(CommandInterface $command) : mixed
Parameters
- $command : CommandInterface
Return values
mixed —writeRequest()
{@inheritdoc}
public
writeRequest(CommandInterface $command) : mixed
Parameters
- $command : CommandInterface
Return values
mixed —assertExtensions()
Checks if the socket and phpiredis extensions are loaded in PHP.
protected
assertExtensions() : mixed
Return values
mixed —assertParameters()
Checks some of the parameters used to initialize the connection.
protected
assertParameters(ParametersInterface $parameters) : ParametersInterface
Parameters
- $parameters : ParametersInterface
-
Initialization parameters for the connection.
Return values
ParametersInterface —createResource()
Creates the underlying resource used to communicate with Redis.
protected
createResource() : mixed
Return values
mixed —getAddress()
Gets the address of an host from connection parameters.
protected
static getAddress(ParametersInterface $parameters) : string
Parameters
- $parameters : ParametersInterface
-
Parameters used to initialize the connection.
Return values
string —getErrorHandler()
Returns the handler used by the protocol reader for error responses.
protected
getErrorHandler() : Closure
Return values
Closure —getIdentifier()
Gets an identifier for the connection.
protected
getIdentifier() : string
Return values
string —getReader()
Returns the underlying protocol reader resource.
protected
getReader() : resource
Return values
resource —getStatusHandler()
Returns the handler used by the protocol reader for inline responses.
protected
getStatusHandler() : Closure
Return values
Closure —onConnectionError()
Helper method to handle connection errors.
protected
onConnectionError(string $message[, int $code = null ]) : mixed
Parameters
- $message : string
-
Error message.
- $code : int = null
-
Error code.
Return values
mixed —onProtocolError()
Helper method to handle protocol errors.
protected
onProtocolError(string $message) : mixed
Parameters
- $message : string
-
Error message.
Return values
mixed —write()
{@inheritdoc}
protected
write(mixed $buffer) : mixed
Parameters
- $buffer : mixed
Return values
mixed —connectWithTimeout()
Opens the actual connection to the server with a timeout.
private
connectWithTimeout(resource $socket, string $address, ParametersInterface $parameters) : string
Parameters
- $socket : resource
-
Socket resource.
- $address : string
-
IP address (DNS-resolved from hostname)
- $parameters : ParametersInterface
-
Parameters used to initialize the connection.
Return values
string —createExceptionMessage()
Helper method that returns an exception message augmented with useful details from the connection parameters.
private
createExceptionMessage(string $message) : string
Parameters
- $message : string
-
Error message.
Return values
string —createReader()
Creates a new instance of the protocol reader resource.
private
createReader() : resource
Return values
resource —emitSocketError()
Helper method used to throw exceptions on socket errors.
private
emitSocketError() : mixed
Return values
mixed —setSocketOptions()
Sets options on the socket resource from the connection parameters.
private
setSocketOptions(resource $socket, ParametersInterface $parameters) : mixed
Parameters
- $socket : resource
-
Socket resource.
- $parameters : ParametersInterface
-
Parameters used to initialize the connection.