Redis

webman的redis组件默认使用的是illuminate/redis,也就是laravel的redis库,用法与laravel相同。

使用illuminate/redis之前必须先给php-cli安装redis扩展。

使用命令php -m | grep redis查看php-cli是否装了redis扩展。注意:即使你在php-fpm安装了redis扩展,不代表你在php-cli可以使用它,因为php-cliphp-fpm是不同的应用程序,可能使用的是不同的php.ini配置。使用命令php --ini来查看你的php-cli使用的是哪个php.ini配置文件。

安装

  1. composer require psr/container ^1.1.1 illuminate/redis illuminate/events

配置

redis配置文件在config/redis.php

  1. return [
  2. 'default' => [
  3. 'host' => '127.0.0.1',
  4. 'password' => null,
  5. 'port' => 6379,
  6. 'database' => 0,
  7. ]
  8. ];

示例

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. use support\Redis;
  5. class User
  6. {
  7. public function db(Request $request)
  8. {
  9. $key = 'test_key';
  10. Redis::set($key, rand());
  11. return response(Redis::get($key));
  12. }
  13. }

Redis接口

  1. Redis::append($key, $value)
  2. Redis::bitCount($key)
  3. Redis::decr($key, $value)
  4. Redis::decrBy($key, $value)
  5. Redis::get($key)
  6. Redis::getBit($key, $offset)
  7. Redis::getRange($key, $start, $end)
  8. Redis::getSet($key, $value)
  9. Redis::incr($key, $value)
  10. Redis::incrBy($key, $value)
  11. Redis::incrByFloat($key, $value)
  12. Redis::mGet(array $keys)
  13. Redis::getMultiple(array $keys)
  14. Redis::mSet($pairs)
  15. Redis::mSetNx($pairs)
  16. Redis::set($key, $value, $expireResolution = null, $expireTTL = null, $flag = null)
  17. Redis::setBit($key, $offset, $value)
  18. Redis::setEx($key, $ttl, $value)
  19. Redis::pSetEx($key, $ttl, $value)
  20. Redis::setNx($key, $value)
  21. Redis::setRange($key, $offset, $value)
  22. Redis::strLen($key)
  23. Redis::del(...$keys)
  24. Redis::exists(...$keys)
  25. Redis::expire($key, $ttl)
  26. Redis::expireAt($key, $timestamp)
  27. Redis::select($dbIndex)

等价于

  1. $redis = Redis::connection('default');
  2. $redis->append($key, $value)
  3. $redis->bitCount($key)
  4. $redis->decr($key, $value)
  5. $redis->decrBy($key, $value)
  6. $redis->get($key)
  7. $redis->getBit($key, $offset)
  8. ...

注意 慎用Redis::select($db)接口,由于webman是常驻内存的框架,如果某一个请求使用Redis::select($db)切换数据库后将会影响后续其他请求。多数据库建议将不同的$db配置成不同的Redis连接配置。

使用多个 Redis 连接

例如配置文件config/redis.php

  1. return [
  2. 'default' => [
  3. 'host' => '127.0.0.1',
  4. 'password' => null,
  5. 'port' => 6379,
  6. 'database' => 0,
  7. ],
  8. 'cache' => [
  9. 'host' => '127.0.0.1',
  10. 'password' => null,
  11. 'port' => 6379,
  12. 'database' => 1,
  13. ],
  14. ]

默认使用的是default下配置的连接,你可以用Redis::connection()方法选择使用哪个redis连接。

  1. $redis = Redis::connection('cache');
  2. $redis->get('test_key');

集群配置

如果你的应用使用 Redis 服务器集群,你应该在 Redis 配置文件中使用 clusters 键来定义这些集群:

  1. return [
  2. 'clusters' => [
  3. 'default' => [
  4. [
  5. 'host' => 'localhost',
  6. 'password' => null,
  7. 'port' => 6379,
  8. 'database' => 0,
  9. ],
  10. ],
  11. ],
  12. ];

默认情况下,集群可以在节点上实现客户端分片,允许你实现节点池以及创建大量可用内存。这里要注意,客户端共享不会处理失败的情况;因此,这个功能主要适用于从另一个主数据库获取的缓存数据。如果要使用 Redis 原生集群,需要在配置文件下的 options 键中做出如下指定:

  1. return[
  2. 'options' => [
  3. 'cluster' => 'redis',
  4. ],
  5. 'clusters' => [
  6. // ...
  7. ],
  8. ];

管道命令

当你需要在一个操作中给服务器发送很多命令时,推荐你使用管道命令。 pipeline 方法接受一个 Redis 实例的 闭包 。你可以将所有的命令发送给 Redis 实例,它们都会在一个操作中执行完成:

  1. Redis::pipeline(function ($pipe) {
  2. for ($i = 0; $i < 1000; $i++) {
  3. $pipe->set("key:$i", $i);
  4. }
  5. });