[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;
});
```
- 入門指南
- 安裝
- 部署
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- URL
- Session
- 表單驗證
- 錯誤
- 日志
- 前端開發
- Blade 模板
- 本地化
- 腳手架
- 編譯資源 Mix
- 安全相關
- 用戶認證
- API 認證
- 綜合話題
- 命令行
- 廣播
- 緩存
- 集合
- 事件
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 速查表
- Artisan
- Auth
- Blade
- Cache
- Collection
- Composer
- Config
- Container
- Cookie
- DB
- Environment
- Event
- File
- Helper
- Input
- Lang
- Log
- Model
- Pagination
- Queue
- Redirect
- Request
- Response
- Route
- SSH
- Schema
- Security
- Session
- Storage
- String
- URL
- UnitTest
- Validation
- View