企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # Redis ## 简介 ``` // 可以通过 Composer 安装`predis/predis`扩展包 composer require predis/predis ``` 也可以通过 PECL 安装[PhpRedis](https://github.com/phpredis/phpredis)PHP 扩展。 ### 配置 ``` // 配置文件 config/database.php 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_CACHE_DB', 1), ], ], ``` ### 集群配置 ``` // 使用 clusters 键定义集群 'redis' => [ 'client' => 'predis', 'clusters' => [ 'default' => [ [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], ], ], ], // 使用 Redis 原生集群,需要在配置文件下的 options 键中设置 'redis' => [ 'client' => 'predis', 'options' => [ 'cluster' => 'redis', ], 'clusters' => [ // ... ], ], ``` ### Predis ``` // Predis 还支持为每个 Redis 服务器定义其它的链接参数 connection parameters 。 // https://github.com/nrk/predis/wiki/Connection-Parameters 'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, 'read_write_timeout' => 60, ], ``` ### PhpRedis ``` // 要使用 PhpRedis 扩展,需要将配置文件 config/database.php 中 Redis 配置的 client 选项修改为 phpredis 'redis' => [ 'client' => 'phpredis', // 其余的Redis配置... ], // PhpRedis 还支持几个额外的连接参数: persistent, prefix, read_timeout 和 timeout 。 'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, 'read_timeout' => 60, ], ``` ## Redis 交互 ``` // 通过调用 Redis facade 上的各种方法来与 Redis 交互。 <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Redis; class UserController extends Controller { /** * 显示给定用户的配置文件。 * * @param int $id * @return Response */ public function showProfile($id) { $user = Redis::get('user:profile:'.$id); return view('user.profile', ['user' => $user]); } } Redis::set('name', 'Taylor'); $values = Redis::lrange('names', 5, 10); // 使用 command 方法将命令传递给服务器,它接受命令的名称作为其第一个参数,并将值的数组作为其第二个参数 $values = Redis::command('lrange', ['name', 5, 10]); ``` ### 使用多个 Redis 连接 ``` $redis = Redis::connection(); // 传递连接或者集群名称给 connection 方法来获取在 Redis 配置中特定服务或集群 $redis = Redis::connection('my-connection'); ``` ### 管道命令 当你需要在一个操作中给服务器发送很多命令时,推荐你使用管道命令。`pipeline`方法接受一个 Redis 实例的`闭包`。你可以将所有的命令发送给 Redis 实例,它们都会在一个操作中执行完成: ``` // 当你需要在一个操作中给服务器发送很多命令时,推荐你使用管道命令。 Redis::pipeline(function ($pipe) { for ($i = 0; $i < 1000; $i++) { $pipe->set("key:$i", $i); } }); ``` ## 发布与订阅 ``` // 使用 subscribe 方法设置频道监听器,在 Artisan 命令调用 <?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Redis; class RedisSubscribe extends Command { /** * 控制台命令的名称和签名。 * * @var string */ protected $signature = 'redis:subscribe'; /** * 控制台命令说明。 * * @var string */ protected $description = 'Subscribe to a Redis channel'; /** * 执行控制台命令。 * * @return mixed */ public function handle() { Redis::subscribe(['test-channel'], function ($message) { echo $message; }); } } // 使用 publish 方法将消息发布到频道 Route::get('publish', function () { // 路由... Redis::publish('test-channel', json_encode(['foo' => 'bar'])); }); ``` ### 通配符订阅 ``` Redis::psubscribe(['*'], function ($message, $channel) { echo $message; }); Redis::psubscribe(['users.*'], function ($message, $channel) { echo $message; }); ```