企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<article><h1>Laravel 的 Redis 使用指南</h1><ul><li><a href="#introduction">简介</a><ul><li><a href="#configuration">配置</a></li><li><a href="#predis">Predis</a></li><li><a href="#phpredis">PhpRedis</a></li></ul></li><li><a href="#interacting-with-redis">基本用法</a><ul><li><a href="#pipelining-commands">管道化命令</a></li></ul></li><li><a href="#pubsub">发布与订阅</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p><a href="http://redis.io">Redis</a> 是一款开源且先进的键值对数据库。由于它的键指向的数据包含了 <a href="http://redis.io/topics/data-types#strings">字符串</a>、<a href="http://redis.io/topics/data-types#hashes">哈希</a>、<a href="http://redis.io/topics/data-types#lists">列表</a>、<a href="http://redis.io/topics/data-types#sets">集合</a> 和 <a href="http://redis.io/topics/data-types#sorted-sets">有序集合</a> 这些数据类型,因此常被用作数据结构服务器。</p><p>在使用 Redis 之前,你需要通过 Composer 安装 <code class=" language-php">predis<span class="token operator">/</span>predis</code> 扩展包。</p><pre class=" language-php"><code class=" language-php">composer <span class="token keyword">require</span> predis<span class="token operator">/</span>predis</code></pre><p>还有一种选择,你可以通过 PECL 安装 <a href="https://github.com/phpredis/phpredis">PhpRedis</a> PHP 扩展。这个扩展安装起来更复杂,但是你可以在你的程序重度使用 redis 时获得一定的性能提升。</p><p><a name="configuration"></a></p><h3>配置</h3><p>应用程序的 Redis 配置都在 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 配置文件中。在这个文件里,你可以看到 <code class=" language-php">redis</code> 数组里面包含了应用程序使用的 Redis 服务器:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'redis'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'client'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'predis'</span><span class="token punctuation">,</span> <span class="token string">'default'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PASSWORD'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'port'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'database'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p>默认的服务器配置对于开发来说应该足够了。当然,你也可以根据使用的环境来随意更改数组。只需给每个 Redis 服务器指定名称、host 和 port 即可。</p><blockquote><p>译者注: 关于 Redis 多连接的配置,请参阅 - <a href="https://laravel-china.org/topics/2466">Laravel 下配置 Redis 让缓存、Session 各自使用不同的 Redis 数据库</a>。</p></blockquote><h4>redis 集群配置</h4><p>如果你的程序使用 redis 服务器集群,你应该在 redis 配置文件中使用 <code class=" language-php">clusters</code> 键来定义:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'redis'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'client'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'predis'</span><span class="token punctuation">,</span> <span class="token string">'clusters'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'default'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token punctuation">[</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PASSWORD'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'port'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'database'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p>默认情况下,集群可以实现跨节点间客户端共享,允许你实现节点池以及创建大量可用内存。然而,注意客户端共享并没有处理失败情况;因此,主要适用于从另一个主要的数据源来建立缓存数据。如果你喜欢使用 redis 原生集群,你需要在配置文件中配置 <code class=" language-php">options</code> 键:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'redis'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'client'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'predis'</span><span class="token punctuation">,</span> <span class="token string">'options'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'cluster'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'redis'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'clusters'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token comment" spellcheck="true"> // ... </span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="predis"></a></p><h3>Predis</h3><p>除了默认的 <code class=" language-php">Host</code>,<code class=" language-php">port</code>,<code class=" language-php">database</code> 和 <code class=" language-php">password</code> 服务配置项之外,Predis 还可以为每个 redis 定义其他的 <a href="https://github.com/nrk/predis/wiki/Connection-Parameters">连接参数</a>。要使用这些额外的配置选项,只需将它们添加到你的 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 配置文件的 Redis 服务器配置项中即可:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'default'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PASSWORD'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'port'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'database'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'read_write_timeout'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="phpredis"></a></p><h3>PhpRedis</h3><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 如果你是通过 PECL 安装 Redis PHP 扩展,则需要重命名 <code class=" language-php">config<span class="token operator">/</span>app<span class="token punctuation">.</span>php</code> 文件里的 Redis 别名。</p></blockquote><p>要使用 Phpredis 扩展,你需要将 <code class=" language-php">client</code> 选项配置为 <code class=" language-php">phpredis</code>。这个选项可以在 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 配置文件中找到:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'redis'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'client'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'phpredis'</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true"> // Rest of Redis configuration... </span><span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p>除了默认的 <code class=" language-php">Host</code>,<code class=" language-php">port</code>,<code class=" language-php">database</code> 和 <code class=" language-php">password</code> 服务配置项之外,Phpredis 还支持下列额外连接配置:<code class=" language-php">persistent</code>,<code class=" language-php">prefix</code>,<code class=" language-php">read_timeout</code> 和 <code class=" language-php">timeout</code>。你可以将这些选项加到 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 配置文件中 redis 服务器配置项下:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'default'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_HOST'</span><span class="token punctuation">,</span> <span class="token string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PASSWORD'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'port'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'REDIS_PORT'</span><span class="token punctuation">,</span> <span class="token number">6379</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'database'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'read_timeout'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="interacting-with-redis"></a></p><h2><a href="#interacting-with-redis">基本用法</a></h2><p>你可以通过调用 <code class=" language-php">Redis</code> <a href="/docs/5.4/facades">facade</a> 的各种方法与 <code class=" language-php">Redis</code> 进行交互。<code class=" language-php">Redis</code> facade 支持动态方法,意思就是指你可以在该 facade 调用任何 <a href="http://redis.io/commands">Redis 命令</a>,该命令会直接传递给 Redis。在本例中,我们会通过 <code class=" language-php">Redis</code> facade 的 <code class=" language-php">get</code> 方法来调用 Redis 的 <code class=" language-php"><span class="token constant">GET</span></code> 命令:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * Show the profile for the given user. * * @param int $id * @return Response */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">showProfile<span class="token punctuation">(</span></span><span class="token variable">$id</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'user:profile:'</span><span class="token punctuation">.</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'user.profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'user'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>如上所述,你可以在 <code class=" language-php">Redis</code> facade 调用任何的 Redis 命令。Laravel 使用魔术方法来传递命令至 Redis 服务器,所以可以简单的传递 Redis 命令所需要的参数:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">set<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$values</span> <span class="token operator">=</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">lrange<span class="token punctuation">(</span></span><span class="token string">'names'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>另外,你也可以通过 <code class=" language-php">command</code> 方法传递命令至服务器,它接收命令的名称作为第一个参数,第二个参数则为值的数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$values</span> <span class="token operator">=</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">command<span class="token punctuation">(</span></span><span class="token string">'lrange'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>使用多个 Redis 连接</h4><p>你可以通过 <code class=" language-php"><span class="token scope">Redis<span class="token punctuation">::</span></span>connection</code> 方法来得到 Redis 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$redis</span> <span class="token operator">=</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">connection<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>这会返回配置项中的默认的 redis 服务器。你也可以传递连接或者集群的名字给 <code class=" language-php">connection</code> 方法,来获取在 Redis 配置文件中配置的特定的服务器或者集群:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$redis</span> <span class="token operator">=</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">connection<span class="token punctuation">(</span></span><span class="token string">'my-connection'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="pipelining-commands"></a></p><h3>管道化命令</h3><p>当你想要在单次操作中发送多个命令至服务器时则可以使用管道化命令。 <code class=" language-php">pipeline</code> 方法接收一个参数:带有 Redis 实例的 <code class=" language-php">闭包</code> 。你可以发送所有的命令至此 Redis 实例,它们都会在单次操作中运行:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">pipeline<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$pipe</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$i</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token variable">$i</span> <span class="token operator">&lt;</span> <span class="token number">1000</span><span class="token punctuation">;</span> <span class="token variable">$i</span><span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$pipe</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">set<span class="token punctuation">(</span></span><span class="token string">"key:$i"</span><span class="token punctuation">,</span> <span class="token variable">$i</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="pubsub"></a></p><h2><a href="#pubsub">发布与订阅</a></h2><p>Laravel 也对 Redis 的 <code class=" language-php">publish</code> 及 <code class=" language-php">subscribe</code> 提供了方便的接口。这些 Redis 命令让你可以监听指定「频道」的消息。你可以从另一个应用程序发布消息至频道,甚至使用另一种编程语言,让应用程序或进程之间容易沟通。</p><p>首先,让我们通过 <code class=" language-php">Redis</code> 来使用 <code class=" language-php">subscribe</code> 方法在一个频道设置侦听器。我们会将方法调用放置于一个 <a href="/docs/5.4/artisan">Artisan 命令</a> 中,因为调用 <code class=" language-php">subscribe</code> 方法会启动一个长时间运行的进程:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Command</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">RedisSubscribe</span> <span class="token keyword">extends</span> <span class="token class-name">Command</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * The name and signature of the console command. * * @var string */</span> <span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string">'redis:subscribe'</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * The console command description. * * @var string */</span> <span class="token keyword">protected</span> <span class="token variable">$description</span> <span class="token operator">=</span> <span class="token string">'Subscribe to a Redis channel'</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * Execute the console command. * * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">handle<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">subscribe<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'test-channel'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">echo</span> <span class="token variable">$message</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>现在,我们可以通过 <code class=" language-php">publish</code> 方法发布消息至该频道:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'publish'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // Route logic... </span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">publish<span class="token punctuation">(</span></span><span class="token string">'test-channel'</span><span class="token punctuation">,</span> <span class="token function">json_encode<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>通配符订阅</h4><p>你可以使用 <code class=" language-php">psubscribe</code> 方法订阅一个通配符频道,这在对所有频道获取所有消息时相当有用。 <code class=" language-php"><span class="token variable">$channel</span></code> 名称会被传递至该方法提供的回调 <code class=" language-php">闭包</code> 的第二个参数:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">psubscribe<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$channel</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">echo</span> <span class="token variable">$message</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token scope">Redis<span class="token punctuation">::</span></span><span class="token function">psubscribe<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'users.*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$channel</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">echo</span> <span class="token variable">$message</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://github.com/daydaygo">@daydaygo</a></td><td><img class="avatar-66 rm-style" src="http://qiniu.daydaygo.top/lol-timo-panda.png"></td><td>翻译</td><td><a href="http://blog.daydaygo.top">Coder at Work</a></td></tr></tbody></table></article>