<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Redis - [簡介](#introduction) - [配置](#Configuration) - [基本用法](#Interacting With Redis) - [管道化命令](#Pipelining Commands) - [發布與訂閱](#Pub/Sub) <a name="introduction"></a> # 簡介 Redis 是一款開源且先進的鍵值對數據庫。由于它可用的鍵包含了[字符串](http://redis.io/topics/data-types#strings)、[哈希](http://redis.io/topics/data-types#hashes)、[列表](http://redis.io/topics/data-types#lists)、[集合](http://redis.io/topics/data-types#sets) 和 [有序集合](http://redis.io/topics/data-types#sorted-sets),因此常被稱作數據結構服務器。在使用 Redis 之前,你必須通過 Composer 安裝 `predis/predis` 擴展包(~1.0)。 ```php composer require predis/predis ``` <a name="Configuration"></a> ## 配置 應用程序的 Redis 設置都在 `config/database.php` 配置文件中。在這個文件里,你可以看到 `redis` 數組里面包含了應用程序使用的 Redis 服務器: ```php 'redis' => [ 'cluster' => false, 'default' => [ 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ], ], ``` 默認的服務器配置對于開發來說應該足夠了。然而,你也可以根據使用的環境來隨意更改數組。只需給每個 Redis 指定名稱以及在服務器中使用的 host 和 port 即可 > 譯者注: 關于 Redis 多連接的配置,請參閱 - [Laravel 下配置 Redis 讓緩存、Session 各自使用不同的 Redis 數據庫](https://laravel-china.org/topics/2466)。 `cluster` 選項會讓 Laravel 的 Redis 客戶端在所有 Redis 節點間運行客戶端分片(client-side sharding)來創建節點池,并因此擁有大量的可用內存。但是請注意,客戶端分片的節點不能運行容錯轉移。因此,此選項主要適用于可從另一臺主要數據存儲庫獲取到的緩存數據。 此外,你可以在你的 Redis 連接中定義一個 options 數組值,讓你指定一套 Predis [客戶端選項](https://github.com/nrk/predis/wiki/Client-Options)。 如果你的 Redis 服務器需要認證,你可以在 Redis 服務器的設置數組里加入 `password` 設置作為提供的密碼。 > **注意**:如果你是通過 PECL 安裝 Redis PHP 擴展,則需要重命名 `config/app.php` 文件里的 Redis 別名。 <a name="Interacting With Redis"></a> # 基本用法 你可以通過調用 `Redis` [facade](https://laravel.com/docs/5.3/facades) 的各種方法與 `Redis` 進行交互。`Redis` facade 支持動態方法,意思就是指你可以在該 facade 調用任何 [Redis 命令](http://redis.io/commands),該命令會直接傳遞給 Redis。在本例中,我們會通過 `Redis` facade 的 `get` 方法來調用 Redis 的 `GET` 命令: ```php <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Redis; use App\Http\Controllers\Controller; 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` facade 調用任何的 Redis 命令。Laravel 使用魔術方法來傳遞命令至 Redis 服務器,所以可以簡單的傳遞 Redis 命令所需要的參數: ```php Redis::set('name', 'Taylor'); $values = Redis::lrange('names', 5, 10); ``` 另外,你也可以通過 `command` 方法傳遞命令至服務器,它接收命令的名稱作為第一個參數,第二個參數則為值的數組: ```php $values = Redis::command('lrange', ['name', 5, 10]); ``` ### 使用多個 Redis 連接 你可以通過 `Redis::connection` 方法來得到 Redis 實例: ```php $redis = Redis::connection(); ``` 你會得到一個 Redis 默認服務器的實例。如果你沒有使用服務器集群,則可以在 `connection` 方法傳入定義在 Redis 配置文件的服務器名稱,以獲取特定服務器: ```php $redis = Redis::connection('other'); ``` <a name="Pipelining Commands"></a> ## 管道化命令 當你想要在單次操作中發送多個命令至服務器時則可以使用管道化命令。 `pipeline` 方法接收一個參數:帶有 Redis 實例的 `閉包` 。你可以發送所有的命令至此 Redis 實例,它們都會在單次操作中運行: ```php Redis::pipeline(function ($pipe) { for ($i = 0; $i < 1000; $i++) { $pipe->set("key:$i", $i); } }); ``` <a name="Pub/Sub"></a> # 發布與訂閱 Laravel 也對 Redis 的 `publish` 及 `subscribe` 提供了方便的接口。這些 Redis 命令讓你可以監聽指定「頻道」的消息。你可以從另一個應用程序發布消息至頻道,甚至使用另一種編程語言,讓應用程序或進程之間容易溝通。 首先,讓我們通過 `Redis` 來使用 `subscribe` 方法在一個頻道設置偵聽器。我們會將方法調用放置于一個 [Artisan 命令](https://laravel.com/docs/5.3/artisan) 中,因為調用 `subscribe` 方法會啟動一個長時間運行的進程: ```php <?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` 方法發布消息至該頻道: ```php Route::get('publish', function () { // 路由邏輯... Redis::publish('test-channel', json_encode(['foo' => 'bar'])); }); ``` ### 通配符訂閱 你可以使用 `psubscribe` 方法訂閱一個通配符頻道,這在對所有頻道獲取所有消息時相當有用。 `$channel` 名稱會被傳遞至該方法提供的回調 `閉包` 的第二個參數: ```php Redis::psubscribe(['*'], function($message, $channel) { echo $message; }); Redis::psubscribe(['users.*'], function($message, $channel) { echo $message; }); ``` ## 譯者署名 | 用戶名 | 頭像 | 職能 | 簽名 | |---|---|---|---| | [@buer](https://github.com/buer0) | <img class="avatar-66 rm-style" src="https://avatars3.githubusercontent.com/u/22141008?v=3&u=f14a9d540240e1d39079dc1319eb146a91aabfa8&s=140"> | 翻譯 | [你今天吃藥了嗎?](http://www.cxdog.com) | | [@silvercell](https://github.com/silvercell) | <img class="avatar-66 rm-style" src="https://avatars2.githubusercontent.com/u/20363459?v=3&u=2234d736aa27209a2e986d4d789f95c6d110aa0c&s=140"> | 翻譯 | [已放棄治療!](http://www.cxdog.com) |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看