时间:2019-08-17 10:42:52 浏览: 字体: 放大 缩小

This library can be found on Packagist for an easier management of projects dependencies using Composer or on our own PEAR channel for a more traditional installation using PEAR. Ultimately, compressed archives of each release are available on GitHub.

可以在Packagist上找到此库,以便使用Composer更轻松地管理项目依赖项,或者在我们自己的PEAR通道上使用PEAR进行更传统的安装。 最终,GitHub上提供了每个版本的压缩档案。

Loading the library(加载库)

Predis relies on the autoloading features of PHP to load its files when needed and complies with the PSR-4 standard. Autoloading is handled automatically when dependencies are managed through Composer, but it is also possible to leverage its own autoloader in projects or scripts lacking any autoload facility:

Predis依靠PHP的自动加载功能在需要时加载其文件并符合PSR-4标准。 通过Composer管理依赖项时自动处理自动加载,但也可以在缺少任何自动加载功能的项目或脚本中利用自己的自动加载器:

// Prepend a base path if Predis is not available in your "include_path".
require 'Predis/Autoloader.php';

It is also possible to create a phar archive directly from the repository by launching the bin/create-phar script. The generated phar already contains a stub defining its own autoloader, so you just need to require() it to start using the library.

也可以通过启动bin / create-phar脚本直接从存储库创建phar存档。 生成的phar已经包含一个定义自己的自动加载器的存根,因此您只需要require()它就可以开始使用该库。

Connecting to Redis(连接到Redis)

When creating a client instance without passing any connection parameter, Predis assumes and 6379 as default host and port. The default timeout for the connect() operation is 5 seconds:

在不传递任何连接参数的情况下创建客户端实例时,Predis将127.0.0.1和6379视为默认主机和端口。 connect()操作的默认超时为5秒:

$client = new Predis\Client();
$client->set('foo', 'bar');
$value = $client->get('foo');

Connection parameters can be supplied either in the form of URI strings or named arrays. The latter is the preferred way to supply parameters, but URI strings can be useful when parameters are read from non-structured or partially-structured sources:

可以以URI字符串或命名数组的形式提供连接参数。 后者是提供参数的首选方法,但是当从非结构化或部分结构化的源读取参数时,URI字符串可能很有用:

// Parameters passed using a named array:
$client = new Predis\Client([
    'scheme' => 'tcp',
    'host'   => '',
    'port'   => 6379,
// Same set of parameters, passed using an URI string:
$client = new Predis\Client('tcp://');

It is also possible to connect to local instances of Redis using UNIX domain sockets, in this case the parameters must use the unix scheme and specify a path for the socket file.


$client = new Predis\Client(['scheme' => 'unix', 'path' => '/path/to/redis.sock']);
$client = new Predis\Client('unix:/path/to/redis.sock');

The client can leverage TLS/SSL encryption to connect to secured remote Redis instances without the need to configure an SSL proxy like stunnel. This can be useful when connecting to nodes running on various cloud hosting providers. Encryption can be enabled with using the tls scheme and an array of suitable options passed via the ssl parameter.

客户端可以利用TLS / SSL加密连接到安全的远程Redis实例,而无需配置像stunnel这样的SSL代理。 当连接到在各种云托管提供商上运行的节点时,这可能很有用。 可以使用tls方案和通过ssl参数传递的合适选项数组来启用加密。

// Named array of connection parameters:
$client = new Predis\Client([
  'scheme' => 'tls',
  'ssl'    => ['cafile' => 'private.pem', 'verify_peer' => true],
// Same set of parameters, but using an URI string:
$client = new Predis\Client('tls://[cafile]=private.pem&ssl[verify_peer]=1');

The connection schemes redis (alias of tcp) and rediss (alias of tls) are also supported, with the difference that URI strings containing these schemes are parsed following the rules described on their respective IANA provisional registration documents.


The actual list of supported connection parameters can vary depending on each connection backend so it is recommended to refer to their specific documentation or implementation for details.


When an array of connection parameters is provided, Predis automatically works in cluster mode using client-side sharding. Both named arrays and URI strings can be mixed when providing configurations for each node.

提供连接参数数组时,Predis将使用客户端分片自动在群集模式下工作。 在为每个节点提供配置时,可以混合命名数组和URI字符串。

$client = new Predis\Client([
    ['host' => '', 'alias' => 'second-node'],

Connections to Redis are lazy meaning that the client connects to a server only if and when needed. While it is recommended to let the client do its own stuff under the hood, there may be times when it is still desired to have control of when the connection is opened or closed: this can easily be achieved by invoking $client->connect() and $client->disconnect(). Please note that the effect of these methods on aggregate connections may differ depending on each specific implementation.

与Redis的连接是惰性的,这意味着客户端仅在需要时才连接到服务器。 虽然建议让客户端自己做一些事情,但有时候仍然希望能够控制何时打开或关闭连接:这可以通过调用$client->connect()和$client->disconnect()就能轻松实现。 请注意,这些方法对聚合连接的影响可能因具体实现而异。

标签: 安装phpPredis