cheprasov/php-redis-client

PHP Redis 客户端。这是一个快速、功能齐全且用户友好的 Redis 客户端,针对性能进行了优化。RedisClient 支持从 2.6 到 6.0 的最新 Redis 版本。

资助包维护!
cheprasov

安装次数: 1,001,734

依赖者: 21

建议者: 1

安全: 0

星标: 126

关注者: 17

分支: 41

开放问题: 8

1.10.0 2020-06-20 13:05 UTC

README

MIT license Latest Stable Version Total Downloads

RedisClient v1.10.0 for PHP >= 5.5

关于

RedisClient 是一个快速、功能齐全且用户友好的 Redis 客户端,针对性能进行了优化。RedisClient 支持从 2.66.0 的最新 Redis 版本。

主要功能

  • 支持从 2.6.x6.0.x 的 Redis 版本。
  • 支持 TCP/IPUNIX 套接字。
  • 支持 PubSubMonitor 功能。
  • 支持 PipelineTransactions
  • 支持 Redis Cluster
  • 支持作为数组 ['SET', 'foo', 'bar']RAW 命令。
  • 客户端在第一次命令时才会建立与 Redis 的连接。
  • 易于与 IDE 一起使用,客户端为所有支持的版本提供 PHPDocs。
  • 默认情况下,客户端使用 Redis 的最新稳定版本(6.0)。
  • 客户端已在以下 Redis 的最新版本上进行了测试:6.0.55.0.54.0.143.2.83.0.72.8.242.6.17
  • 此外,客户端还在以下 PHP 版本上进行了测试:7.47.37.27.17.05.65.5HHVM

Redis 命令

请在此处查看命令列表:./COMMANDS.md

使用方法

配置

$config = [
    // Optional. Default = '127.0.0.1:6379'. You can use 'unix:///tmp/redis.sock'
    'server' => '127.0.0.1:6379',

    // Optional. Default = 1
    // The timeout for reading/writing data over the socket
    'timeout' => 2,

    // Optional. Default = null
    // See more here: https://php.ac.cn/manual/en/function.stream-socket-client.php
    'connection' => [
        // Optional. Default = ini_get("default_socket_timeout")
        // The timeout only applies while making connecting the socket
        'timeout' => 2,

        // Optional. Default = STREAM_CLIENT_CONNECT
        // Bitmask field which may be set to any combination of connection flags.
        // Currently the select of connection flags is limited to STREAM_CLIENT_CONNECT (default),
        // STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT.
        'flags' => STREAM_CLIENT_CONNECT
    ],

    // Optional. Specify version to avoid some unexpected errors.
    'version' => '4.0.10',

    // Optional. Use it only if Redis server requires password (AUTH)
    'password' => 'some-password',

    // Optional. Use it, if you want to select not default db (db != 0) on connect
    'database' => 1,

    // Optional. Array with configs for RedisCluster support
    'cluster' => [
        'enabled' => false,

        // Optional. Default = []. Map of cluster slots and servers
        // array(max_slot => server [, ...])
        // Examples for Cluster with 3 Nodes:
        'clusters' => [
            5460  => '127.0.0.1:7001', // slots from 0 to 5460
            10922 => '127.0.0.1:7002', // slots from 5461 to 10922
            16383 => '127.0.0.1:7003', // slots from 10923 to 16383
        ],

        // Optional. Default = false.
        // Use the param to update cluster slot map below on init RedisClient.
        // RedisClient will execute command CLUSTER SLOTS to get map.
        'init_on_start' => false,

        // Optional. Default = false.
        // If Redis returns error -MOVED then RedisClient will execute
        // command CLUSTER SLOTS to update cluster slot map
        'init_on_error_moved' => true,

        // Optional. Defatult = 0.05 sec. It is timeout before next attempt on TRYAGAIN error.
        'timeout_on_error_tryagain' => 0.25, // sec
    ]
];

创建 RedisClient 的新实例

<?php
namespace Examples;

require (dirname(__DIR__).'/vendor/autoload.php');
// or require (dirname(__DIR__).'/src/autoloader.php');

use RedisClient\RedisClient;
use RedisClient\Client\Version\RedisClient2x6;
use RedisClient\ClientFactory;

// Example 1. Create new Instance for Redis version 6.0.x with config via factory
$Redis = ClientFactory::create([
    'server' => '127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
    'timeout' => 2,
    'version' => '6.0'
]);

echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.8


// Example 2. Create new Instance without config. Client will use default config.
$Redis = new RedisClient();
// By default, the client works with the latest stable version of Redis.
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 3.2
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; // Redis: 3.0.3


// Example 3. Create new Instance with config
// By default, the client works with the latest stable version of Redis.
$Redis = new RedisClient([
    'server' => '127.0.0.1:6387', // or 'unix:///tmp/redis.sock'
    'timeout' => 2
]);

echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 3.2
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; // Redis: 3.2.0


// Example 4. Create new Instance for Redis version 2.6.x with config
$Redis = new RedisClient2x6([
    'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
    'timeout' => 2
]);

echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.6

示例

请在此处查看示例:https://github.com/cheprasov/php-redis-client/tree/master/examples

安装

Composer

下载 composer

wget -nc https://getcomposer.org.cn/composer.phar

并将依赖项添加到您的项目中

php composer.phar require cheprasov/php-redis-client

运行测试

  1. 运行带有 Redis 的 Docker 容器进行测试 https://hub.docker.com/r/cheprasov/redis-for-tests/

  2. 运行带有 Redis 集群的 Docker 容器进行测试 https://hub.docker.com/r/cheprasov/redis-cluster-for-tests/

  3. 在控制台中输入以下命令以运行测试

    ./vendor/bin/phpunit

出现问题

请随意 fork 项目,修复错误,最后请求拉取请求