barbery/laravel-phpredis

laravel 的 phpredis 库

dev-master 2016-04-08 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 06:45:44 UTC


README

这个库为 Laravel 框架提供了 phpredis 支持,显然它也支持 Redis 集群。

特性

  1. 这个库实现了 Cache、Session 和 Queue 中的 Redis 驱动,您可以轻松使用它。
  2. 实现了类似于 Laravel 文档中介绍的 Redis 管道
  3. 您可以自定义序列化和反序列化函数
  4. 您可以自定义缓存过期时间的默认单位

要求

  • PHP >= 5.6.x
  • PhpRedis
  • Laravel >= 5.2.x

安装

您可以通过 composer 安装它,只需执行以下命令

对于 Laravel >= 5.4

自 Laravel 5.4 以来,Laravel 已经支持 phpredis 驱动。因此,您不需要使用这个库。更多信息请参阅 这里

对于 Laravel >= 5.3

composer require barbery/laravel-phpredis:dev-master

对于 Laravel >= 5.2

composer require barbery/laravel-phpredis:v5.2.x-dev

配置

如果您想完全使用 phpredis 而不是 predis,您应该在 config/app.php 文件中添加以下配置

'providers' => [
        // ...
        // YOUR OTHER PROVIDERS SETTING
        // ...
        // And you should commend those system's provider as below
        // Illuminate\Redis\RedisServiceProvider::class,

        // add this to your providers
        Barbery\Providers\RedisServiceProvider::class,
]


aliases => [
        // you must rename the Redis Key name, because it's conflict with the \Redis class provide by phpredis
        // may be you can rename it to MyRedis, So, you can use it like that:
        // MyRedis::get('key'); MyRedis::set('key', 'value'); MyRedis::pipeline(function($pipe){YOUR_CODE});
        'MyRedis' => Illuminate\Support\Facades\Redis::class,
]
// add config to `config/database.php`
'redis' => [
        // if this true, will enable redis cluster mode
        'cluster' => env('REDIS_CLUSTER', false),

        // defualt config is for single redis mode not cluster
        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
            // can take one of the values:
            // Redis::SERIALIZER_NONE, 'none',
            // Redis::SERIALIZER_PHP, 'php',
            // Redis::SERIALIZER_IGBINARY, 'igbinary'
            'serializer' => env('REDIS_SERIALIZER', Redis::SERIALIZER_PHP),
        ],

        // this is for redis cluster mode
        'clusterConfig' => [
            // cluster options
            'options' => [
                'failover' => env('REDIS_CLUSTER_FAILOVER', RedisCluster::FAILOVER_ERROR),
                'read_timeout' => env('REDIS_CLUSTER_READ_TIMEOUT', 3),
                'timeout' => env('REDIS_CLUSTER_TIMEOUT', 3),
                'persistent' => env('REDIS_CLUSTER_PERSISTENT', false),
            ],
            // put your cluster master node here
            [
                'host' => env('REDIS_HOST_1'),
                'port' => env('REDIS_PORT_1'),
            ],
            [
                'host' => env('REDIS_HOST_2'),
                'port' => env('REDIS_PORT_2'),
            ],
            [
                'host' => env('REDIS_HOST_3'),
                'port' => env('REDIS_PORT_3'),
            ],
        ],
    ],

高级配置

Laravel 中的默认单位是分钟,如果您想更改它,您可以在 app/cache.php 中添加配置

'stores' => [
        // YOUR OTHER SETTING
        // ...

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            // add this config to change the default unit, it can be 'second','minute','hour'
            'defaultUnit' => 'second',
            // if you don't set encodeFunc and decodeFunc,
            // default to use php serialize and unserialize to encode/decode value
            // below setting show you how to change it to use json_encode/json_decode to encode/decode value
            'encodeFunc' => function($value){return json_encode($value);},
            'decodeFunc' => function($value){return json_decode($value, true);},
        ],
    ],

问题

由于 Laravel 5.3.x 使用 Lua 脚本来迁移队列作业,而 Redis 集群不支持。因此,如果您使用 Laravel 5.3.x 与 Redis 集群模式,则不要使用 RedisQueue。