<article><h1>Laravel 的缓存系统</h1><ul><li><a href="#configuration">配置信息</a><ul><li><a href="#driver-prerequisites">驱动前提条件</a></li></ul></li><li><a href="#cache-usage">缓存的使用</a><ul><li><a href="#obtaining-a-cache-instance">获取一个缓存实例</a></li><li><a href="#retrieving-items-from-the-cache">从缓存中获取项目</a></li><li><a href="#storing-items-in-the-cache">存放项目到缓存中</a></li><li><a href="#removing-items-from-the-cache">删除缓存中的项目</a></li><li><a href="#the-cache-helper">Cache 帮助函数</a></li></ul></li><li><a href="#cache-tags">缓存标签</a><ul><li><a href="#storing-tagged-cache-items">写入被标记的缓存项</a></li><li><a href="#accessing-tagged-cache-items">访问被标记的缓存项</a></li><li><a href="#removing-tagged-cache-items">移除被标记的缓存项</a></li></ul></li><li><a href="#adding-custom-cache-drivers">增加自定义的缓存驱动</a><ul><li><a href="#writing-the-driver">写驱动</a></li><li><a href="#registering-the-driver">注册驱动</a></li></ul></li><li><a href="#events">缓存事件</a></li></ul><p><a name="configuration"></a></p><h2><a href="#configuration">配置信息</a></h2><p>Laravel 给多种缓存系统提供丰富而统一的 API,缓存配置信息位于 <code class=" language-php">config<span class="token operator">/</span>cache<span class="token punctuation">.</span>php</code>,在这个文件中你可以为你的应用程序指定默认的缓存驱动,Laravel 支持当前流行的缓存系统,如非常棒的 <a href="http://memcached.org">Memcached</a> 和 <a href="http://redis.io">Redis</a> 。</p><p>缓存配置信息文件中也包括很多其他选项,你可以在文件中找到这些选项,请确保你看过这些选项说明。Laravel 默认使用将序列化缓存对象保存在文件系统中的 <code class=" language-php">file</code> 缓存驱动,对于大型应用程序而言,推荐你使用如 Memcached 或者 Redis 这样更强大的缓存驱动。你甚至可以为一个驱动配置多个缓存配置信息。</p><p><a name="driver-prerequisites"></a></p><h3>驱动前提条件</h3><h4>数据库</h4><p>当使用 <code class=" language-php">database</code> 缓存驱动时,你需要配置一个用来存放缓存项的数据库表,下面是一个 <code class=" language-php">Schema</code> 数据表结构声明的示例:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Schema<span class="token punctuation">::</span></span><span class="token function">create<span class="token punctuation">(</span></span><span class="token string">'cache'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">string<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">text<span class="token punctuation">(</span></span><span class="token string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$table</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">integer<span class="token punctuation">(</span></span><span class="token string">'expiration'</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><blockquote class="has-icon tip"><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="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 你也可以使用 <code class=" language-php">php artisan cache<span class="token punctuation">:</span>table</code> 这个 Artisan 命令生成一个有合适数据表结构的 migration 。</p></blockquote><h4>Memcached</h4><p>使用 Memcached 驱动需要安装 <a href="https://pecl.php.net/package/memcached">Memcached PECL 扩展包</a> 。你可以把所有 Memcached 服务器都列在 <code class=" language-php">config<span class="token operator">/</span>cache<span class="token punctuation">.</span>php</code> 这个配置信息文件中:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'memcached'</span> <span class="token operator">=</span><span class="token operator">></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">></span> <span class="token string">'127.0.0.1'</span><span class="token punctuation">,</span>
<span class="token string">'port'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">11211</span><span class="token punctuation">,</span>
<span class="token string">'weight'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</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">host</code> 选项配置到 UNIX 的 socket 路径中。如果你这样配置了,<code class=" language-php">port</code> 选项应该设置为 <code class=" language-php"><span class="token number">0</span></code> :</p><pre class=" language-php"><code class=" language-php"><span class="token string">'memcached'</span> <span class="token operator">=</span><span class="token operator">></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">></span> <span class="token string">'/var/run/memcached/memcached.sock'</span><span class="token punctuation">,</span>
<span class="token string">'port'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token string">'weight'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><h4>Redis</h4><p>在使用 Redis 作为 Laravel 的缓存驱动前,你需要通过 Composer 安装 <code class=" language-php">predis<span class="token operator">/</span>predis</code> 扩展包 (~1.0) 或者使用 PECL 安装 PhpRedis PHP 拓展。</p><p>关于配置 Redis 的更多信息,请参考 <a href="/docs/5.4/redis#configuration">Laravel 文档页面</a> 。</p><p><a name="cache-usage"></a></p><h2><a href="#cache-usage">缓存的使用</a></h2><p><a name="obtaining-a-cache-instance"></a></p><h3>获取一个缓存实例</h3><p><code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Cache<span class="token punctuation">\</span>Factory</span></code> 和 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Cache<span class="token punctuation">\</span>Repository</span></code> <a href="/docs/5.4/contracts">contracts</a> 提供了访问 Laravel 缓存服务的机制。<code class=" language-php">Factory</code> contract 则为你的应用程序定义了访问所有缓存驱动的机制。<code class=" language-php">Repository</code> contract 是典型的用 <code class=" language-php">cache</code> 配置信息文件指定你的应用程序默认缓存驱动的实现。</p><p>然而,你也可以使用 <code class=" language-php">Cache</code> facade,我们将在文档的后续中介绍。<code class=" language-php">Cache</code> facade 提供了方便又简洁的方法访问缓存实例:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?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>Cache</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 a list of all users of the application.
*
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">index<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><h4>访问多个缓存仓库</h4><p>使用<code class=" language-php">Cache</code> facade,可通过 <code class=" language-php">store</code> 方法来访问缓存仓库,传入 <code class=" language-php">store</code> 方法的键应该对应一个缓存配置信息文件中的 <code class=" language-php">stores</code> 配置信息数组中列出的配置值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">store<span class="token punctuation">(</span></span><span class="token string">'file'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">store<span class="token punctuation">(</span></span><span class="token string">'redis'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'bar'</span><span class="token punctuation">,</span> <span class="token string">'baz'</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><a name="retrieving-items-from-the-cache"></a></p><h3>从缓存中获取项目</h3><p><code class=" language-php">Cache</code> facade 中的 <code class=" language-php">get</code> 方法用来从缓存中获取缓存项,如果缓存中不存在该缓存项,返回 <code class=" language-php"><span class="token keyword">null</span></code> 。你也可以向 <code class=" language-php">get</code> 方法传递第二个参数,用来指定缓存项不存在时返回的默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你甚至可以将 <code class=" language-php">闭包</code> 作为默认值传递。如果指定的缓存项在缓存中不存在,<code class=" language-php">闭包</code> 的结果将被返回。传递一个闭包允许你延迟从数据库或外部服务中取出默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</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 keyword">return</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></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">has</code> 方法可以用来检查一个项目是否存在于缓存中:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">has<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>递增与递减值</h4><p><code class=" language-php">递增</code> 和 <code class=" language-php">递减</code> 方法可以用来调整缓存中整数项目值。这两个方法都可以传入一个可选的第二个参数,用来指示要递增或递减多少值:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">increment<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">increment<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$amount</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">decrement<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">decrement<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$amount</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>获取和更新</h4><p>有时你可能会想从缓存中取出一个项目,但也想在取出的项目不存在时存入一个默认值,例如,你可能会想从缓存中取出所有用户,或者当用户不存在时,从数据库中将这些用户取出并放入缓存中,你可以使用 Cache::remember 方法实现:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">remember<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</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 keyword">return</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></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">remember</code> 方法的闭包将会被运行,而且闭包的运行结果将会被存放在缓存中。</p><h4>获取和删除</h4><p>如果你需要从缓存中获取一个缓存项然后删除它,你可以使用 <code class=" language-php">pull</code> 方法。像 <code class=" language-php">get</code> 方法一样,如果缓存项在缓存中不存在,<code class=" language-php"><span class="token keyword">null</span></code> 将被返回:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">pull<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="storing-items-in-the-cache"></a></p><h3>存储项目到缓存中</h3><p>你可以使用 <code class=" language-php">Cache</code> facade 的 <code class=" language-php">put</code> 方法来存放缓存项到缓存中,当你在缓存中存放缓存项时,你需要使用第三个参数来设定缓存的存放时间:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果要指定一个缓存项过期的分钟数,你也可以传递一个 <code class=" language-php">DateTime</code> 实例来表示该缓存项过期的时间点:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$expiresAt</span> <span class="token operator">=</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">addMinutes<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$expiresAt</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>写入目前不存在的项目</h4><p><code class=" language-php">add</code> 方法只会把暂时不存在于缓存中的缓存项放入缓存,如果存放成功将返回 <code class=" language-php"><span class="token boolean">true</span></code> ,否则返回 <code class=" language-php"><span class="token boolean">false</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">add<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>永久写入项目</h4><p><code class=" language-php">forever</code> 方法可以用来将缓存项永久存入缓存中,因为这些缓存项不会过期,所以必须通过 <code class=" language-php">forget</code> 方法手动删除:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">forever<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><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="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 如果你在使用 Memcached 驱动,那么当缓存达到大小限制时,那些「永久」保存的缓存项可能被移除。</p></blockquote><p><a name="removing-items-from-the-cache"></a></p><h3>从缓存中移除项目</h3><p>你可以使用 <code class=" language-php">forget</code> 方法从缓存中移除一个项目:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">forget<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>也可以使用 <code class=" language-php">flush</code> 方法清空所有缓存:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">flush<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><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> 清空缓存并不会遵从缓存的前缀,并且会将缓存中所有的缓存项删除。在清除与其它应用程序共享的缓存时应谨慎考虑这一点。</p></blockquote><p><a name="the-cache-helper"></a></p><h3>Cache 帮助函数</h3><p>除了可以使用 <code class=" language-php">Cache</code> facade 或者 <a href="/docs/5.4/contracts">cache contract</a>之外,你也可以使用全局帮助函数 <code class=" language-php">cache</code> 来获取和保存缓存数据。当 <code class=" language-php">cache</code> 只接收一个字符串参数的时候,它将会返回给定键对应的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你传给函数一个键值对数组和过期时间,它将会把值和过期时间保存在缓存中:</p><pre class=" language-php"><code class=" language-php"><span class="token function">cache<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">cache<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">addSeconds<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><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="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 如果在测试中使用全局函数 <code class=" language-php">cache</code>,你应该使用 <code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span>shouldReceive</code> 方法,就好像你在<a href="/docs/5.4/mocking#mocking-facades">测试 facade</a>一样。</p></blockquote><p><a name="cache-tags"></a></p><h2><a href="#cache-tags">缓存标签</a></h2><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> 缓存标签并不支持使用 <code class=" language-php">file</code> 或 <code class=" language-php">dababase</code> 的缓存驱动。此外,当在缓存使用多个标签并「永久」写入时,类似 <code class=" language-php">memcached</code> 的驱动性能会是最佳的,且会自动清除旧的纪录。</p></blockquote><p><a name="storing-tagged-cache-items"></a></p><h3>写入被标记的缓存项</h3><p>缓存标签允许你在缓存中标记关联的项目,并清空所有已分配指定标签的缓存值。你可以通过传入一组标签名称的有序数组,以访问被标记的缓存。举例来说,让我们访问一个被标记的缓存并 <code class=" language-php">put</code> 值给它:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'people'</span><span class="token punctuation">,</span> <span class="token string">'artists'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'John'</span><span class="token punctuation">,</span> <span class="token variable">$john</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'people'</span><span class="token punctuation">,</span> <span class="token string">'authors'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'Anne'</span><span class="token punctuation">,</span> <span class="token variable">$anne</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="accessing-tagged-cache-items"></a></p><h3>访问被标记的缓存项</h3><p>若要获取一个被标记的缓存项,只要传递一样的有序标签列表至 <code class=" language-php">tags</code> 方法,然后通过你希望获取的值对应的键来调用 <code class=" language-php">get</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$john</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'people'</span><span class="token punctuation">,</span> <span class="token string">'artists'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'John'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$anne</span> <span class="token operator">=</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'people'</span><span class="token punctuation">,</span> <span class="token string">'authors'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'Anne'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="removing-tagged-cache-items"></a></p><h3>移除被标记的缓存项</h3><p>你可以清空已分配的单个标签或是一组标签列表中的所有缓存项。例如,下方的语句会把被标记为 <code class=" language-php">people</code>、<code class=" language-php">authors</code>,或两者都标记了的缓存都移除。所以,<code class=" language-php">Anne</code> 与 <code class=" language-php">John</code> 都会被从缓存中移除:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'people'</span><span class="token punctuation">,</span> <span class="token string">'authors'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flush<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>相反的,下方的语句只会删除被标示为 <code class=" language-php">authors</code> 的缓存,所以 <code class=" language-php">Anne</code> 会被移除,但 <code class=" language-php">John</code> 不会:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">tags<span class="token punctuation">(</span></span><span class="token string">'authors'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flush<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="adding-custom-cache-drivers"></a></p><h2><a href="#adding-custom-cache-drivers">增加自定义的缓存驱动</a></h2><p><a name="writing-the-driver"></a></p><h3>写驱动</h3><p>为了创建自定义的缓存驱动,首先我们需要部署 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Cache<span class="token punctuation">\</span>Store</span></code> <a href="/docs/5.4/contracts">contract</a> 。所以 MongoDB 缓存实现看起来会像这样:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Extensions</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Cache<span class="token punctuation">\</span>Store</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">MongoStore</span> <span class="token keyword">implements</span> <span class="token class-name">Store</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">get<span class="token punctuation">(</span></span><span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">many<span class="token punctuation">(</span></span><span class="token keyword">array</span> <span class="token variable">$keys</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">put<span class="token punctuation">(</span></span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">putMany<span class="token punctuation">(</span></span><span class="token keyword">array</span> <span class="token variable">$values</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">increment<span class="token punctuation">(</span></span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">decrement<span class="token punctuation">(</span></span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">forever<span class="token punctuation">(</span></span><span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">forget<span class="token punctuation">(</span></span><span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">flush<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getPrefix<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>我们只需要通过一个 MongoDB 的连接来实现这些方法。关于如何实现这些方法,可以查看框架源代码中的 <code class=" language-php">Illuminate\<span class="token package">Cache<span class="token punctuation">\</span>MemcachedStore</span></code> 。一旦我们的部署完成,我们就可以完成自定义驱动的注册了。</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">extend<span class="token punctuation">(</span></span><span class="token string">'mongo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">repository<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">MongoStore</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><blockquote class="has-icon tip"><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="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 如果你想知道把自定义的缓存驱动代码放置在哪里,你可以在 <code class=" language-php">app</code> 目录下创建一个 <code class=" language-php">Extensions</code> 命名空间。Laravel 没有硬性规定应用程序的结构,你可以依照你的喜好任意组织你的应用程序。</p></blockquote><p><a name="registering-the-driver"></a></p><h3>注册驱动</h3><p>通过 Laravel 注册自定义缓存驱动,我们将用到 <code class=" language-php">Cache</code> facade 的 <code class=" language-php">extend</code> 方法。<code class=" language-php"><span class="token scope">Cache<span class="token punctuation">::</span></span>extend</code> 的调用会在最新的 Laravel 应用程序默认的 <code class=" language-php">App\<span class="token package">Providers<span class="token punctuation">\</span>AppServiceProvider</span></code> 的 <code class=" language-php">boot</code> 方法中完成。或者你可以创建你自己的服务提供者来放置这些扩展 - 不要忘记在 <code class=" language-php">config<span class="token operator">/</span>app<span class="token punctuation">.</span>php</code> 提供者数组中注册提供者:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Extensions<span class="token punctuation">\</span>MongoStore</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>Cache</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>ServiceProvider</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">CacheServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* Perform post-registration booting of services.
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">extend<span class="token punctuation">(</span></span><span class="token string">'mongo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token scope">Cache<span class="token punctuation">::</span></span><span class="token function">repository<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">MongoStore</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 comment" spellcheck="true">/**
* Register bindings in the container.
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>传递给 <code class=" language-php">extend</code> 方法的第一个参数是驱动名称。这取决于你的 <code class=" language-php">config<span class="token operator">/</span>cache<span class="token punctuation">.</span>php</code> 配置信息文件的 <code class=" language-php">driver</code> 选项。第二个参数为一个应该返回 <code class=" language-php">Illuminate\<span class="token package">Cache<span class="token punctuation">\</span>Repository</span></code> 实例的闭包。这个闭包将传递一个 <a href="/docs/5.4/container">service container</a> 的 <code class=" language-php"><span class="token variable">$app</span></code> 实例。</p><p>一旦你的扩展被注册,就可以轻松的更新 <code class=" language-php">config<span class="token operator">/</span>cache<span class="token punctuation">.</span>php</code> 配置信息文件的 <code class=" language-php">driver</code> 选项为你的扩展名称。</p><p><a name="events"></a></p><h2><a href="#events">缓存事件</a></h2><p>为了在每一次缓存操作时执行代码,你可以监听缓存触发的事件 <a href="/docs/5.4/events">事件</a> 。一般来说,你必须将这些事件监听器放置在 <code class=" language-php">EventServiceProvider</code> :</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* The event listener mappings for the application.
*
* @var array
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token string">'Illuminate\Cache\Events\CacheHit'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'App\Listeners\LogCacheHit'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'Illuminate\Cache\Events\CacheMissed'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'App\Listeners\LogCacheMissed'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'Illuminate\Cache\Events\KeyForgotten'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'App\Listeners\LogKeyForgotten'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'Illuminate\Cache\Events\KeyWritten'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'App\Listeners\LogKeyWritten'</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></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度