PredisConnector.php
TLDR
This file, PredisConnector.php
, is part of the Illuminate\Redis\Connectors
namespace in the Demo Projects project. It contains a class called PredisConnector
that implements the Connector
interface. The class has two methods: connect
and connectToCluster
.
Methods
connect
This method creates a new Redis connection using the Predis client library. It takes in an array of connection configuration and an array of connection options. It merges the options with the configuration and sets certain values, such as the connection timeout and the connection prefix. It then creates a new instance of PredisConnection
using the Client
class from Predis.
connectToCluster
This method creates a new clustered Redis connection using the Predis client library. Similar to the connect
method, it takes in an array of connection configuration, an array of cluster options, and an array of connection options. It merges the cluster options with the configuration and sets the connection prefix. It then creates a new instance of PredisClusterConnection
using the Client
class from Predis.
<?php
namespace Illuminate\Redis\Connectors;
use Illuminate\Contracts\Redis\Connector;
use Illuminate\Redis\Connections\PredisClusterConnection;
use Illuminate\Redis\Connections\PredisConnection;
use Illuminate\Support\Arr;
use Predis\Client;
class PredisConnector implements Connector
{
/**
* Create a new connection.
*
* @param array $config
* @param array $options
* @return \Illuminate\Redis\Connections\PredisConnection
*/
public function connect(array $config, array $options)
{
$formattedOptions = array_merge(
['timeout' => 10.0], $options, Arr::pull($config, 'options', [])
);
if (isset($config['prefix'])) {
$formattedOptions['prefix'] = $config['prefix'];
}
return new PredisConnection(new Client($config, $formattedOptions));
}
/**
* Create a new clustered Predis connection.
*
* @param array $config
* @param array $clusterOptions
* @param array $options
* @return \Illuminate\Redis\Connections\PredisClusterConnection
*/
public function connectToCluster(array $config, array $clusterOptions, array $options)
{
$clusterSpecificOptions = Arr::pull($config, 'options', []);
if (isset($config['prefix'])) {
$clusterSpecificOptions['prefix'] = $config['prefix'];
}
return new PredisClusterConnection(new Client(array_values($config), array_merge(
$options, $clusterOptions, $clusterSpecificOptions
)));
}
}