Skip to content

Client Options

Till Krüss edited this page Mar 2, 2023 · 5 revisions

Certain aspects of a Predis\Client instance can be configured on a client-basis passing a named array to the second parameter of Predis\Client::__construct():

$options = array(
    'profile' => '2.4',
    'prefix'  => 'ns:',
);

$client = new Predis\Client('tcp://127.0.0.1', $options);

Supported client options

  • profile [string, callable object or instance of Predis\Profiles\IServerProfile - default: default]

    The value must match the version of Redis that is being used and specifies which commands or features are available to the client. Supported server profiles are 1.2, 2.0, 2.2, 2.4 and 2.6. Other accepted values are default (the latest version of Redis that was available at release time) and dev (the unstable branch of Redis).

    This value can also be passed directly as the second argument of Predis\Client::__construct() for the sake of brevity since it is a common option:

    $client = new Predis\Client('tcp://127.0.0.1', '2.4');

    When a callable object is used, an instance of Predis\Profiles\IServerProfile must be returned.

    $options = array(
        'profile' => function($options) {
            $profile = Predis\Profiles\ServerProfile::getDefault();
            $profile->defineCommand('set', 'Predis\Commands\StringSet');
    
            return $profile;
        },
    );
  • prefix [string - default: not set]

    Specifies a string used to automatically prefix all the keys contained in a command issued to Redis.

  • throw_errors [boolean - default: true]

    When set to true server errors generated by Redis are translated to PHP exceptions, otherwise they are returned as normal PHP objects.

  • connections [array or instance of Predis\IConnectionFactory - default: instance of Predis\ConnectionFactory]

    This option can be used to register custom classes that will be used by the client to handle the specified connection schemes. The default mapping of scheme => class is:

    $options = array(
        'connections' => array(
            'tcp'  => 'Predis\Network\StreamConnection',
            'unix' => 'Predis\Network\SocketConnection',
        ),
    );

    Alternatively this option can accept an instance of Predis\IConnectionFactory:

    $options = array(
        'connections' => new Predis\ConnectionFactory(),
    );
  • cluster [string or callable object - default: Predis\Network\PredisCluster]

    This option can be used to specify a connection class used to handle a cluster of Redis servers. The accepted values are a string representing the fully qualified name of class that extends the Predis\Network\IConnectionCluster interface or a callable object that returns an instance of Predis\Network\IConnectionCluster.

    $options = array(
        'cluster' => function($options) {
            $distribution = new Predis\Distribution\HashRing();
            $connection = new Predis\Network\PredisCluster($distribution);
    
            return $connection;
        },
    );
  • replication [string or callable object - default: Predis\Network\MasterSlaveReplication]

    This option can be used to specify a connection class used to handle replication with multiple Redis servers. The accepted values are a string representing the fully qualified name of class that extends the Predis\Network\IConnectionReplication interface or a callable object that returns an instance of Predis\Network\IConnectionReplication.

    $options = array(
        'replication' => function($options) {
            $replication = new MasterSlaveReplication();
    
            return $replication;
        },
    );

Custom client options

Client options passed by users and not directly supported by Predis are available for later use. In the following example, commands is a custom option:

$options = array(
    'commands' => array(
        'set' => 'Predis\Commands\StringSet',
        'get' => 'Predis\Commands\StringGet',
    ),
    'profile' => function($options) {
        $profile = Predis\Profiles\ServerProfile::getDefault();
        foreach ($options->commands as $command => $class) {
            $profile->defineCommand($command, $class);
        }

        return $profile;
    },
);