<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Charlie Jade on Medium]]></title>
        <description><![CDATA[Stories by Charlie Jade on Medium]]></description>
        <link>https://medium.com/@Summer_Charlie?source=rss-53f5e2939bc7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cksniyYgb4eIYM4r.jpeg</url>
            <title>Stories by Charlie Jade on Medium</title>
            <link>https://medium.com/@Summer_Charlie?source=rss-53f5e2939bc7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:10:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Summer_Charlie/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[十个 Laravel 5 程序优化技巧]]></title>
            <link>https://medium.com/@Summer_Charlie/%E5%8D%81%E4%B8%AA-laravel-5-%E7%A8%8B%E5%BA%8F%E4%BC%98%E5%8C%96%E6%8A%80%E5%B7%A7-a5e3a34e196f?source=rss-53f5e2939bc7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5e3a34e196f</guid>
            <dc:creator><![CDATA[Charlie Jade]]></dc:creator>
            <pubDate>Thu, 09 Jan 2020 21:42:32 GMT</pubDate>
            <atom:updated>2020-01-09T22:07:50.948Z</atom:updated>
            <content:encoded><![CDATA[<h3>说明</h3><p>性能一直是 Laravel 框架为人聊病的一个点，所以调优 Laravel 程序算是一个必学的技能。</p><p>接下来分享一些开发的最佳实践，还有调优技巧，大家有别的建议也欢迎留言讨论。</p><p>看到这里，我想聪明的你不难明白，原来我们装的插件就是 github 上开源的 go 插件，而 sqs 是用户，goreturns 是项目名称。我们为了尊重插件的开发者，直接就按照 github 的页面编排的目录，直接把我们的项目克隆到 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\sqs\goreturns 文件夹之中。所以我们就在 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\ 目录下，新建一个文件夹为 sqs 我想用过类 Unix 系统的用户都应该明白了，我们相当于建了一个 sqs 用户的个人文件夹，里面可以放他个人的项目，而这个用户本来是 <a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a> 的用户。打开我们的控制台，在 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\sqs\ 路径下，执行 git clone git@github.com:sqs/goreturns.git 命令，将项目克隆到我们本地。看到这里，我想聪明的你不难明白，原来我们装的插件就是 github 上开源的 go 插件，而 sqs 是用户，goreturns 是项目名称。我们为了尊重插件的开发者，直接就按照 github 的页面编排的目录，直接把我们的项目克隆到 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\sqs\goreturns 文件夹之中。所以我们就在 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\ 目录下，新建一个文件夹为 sqs 我想用过类 Unix 系统的用户都应该明白了，我们相当于建了一个 sqs 用户的个人文件夹，里面可以放他个人的项目，而这个用户本来是 <a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a> 的用户。打开我们的控制台，在 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\sqs\ 路径下，执行 git clone git@github.com:sqs/goreturns.git 命令，将项目克隆到我们本地。看到这里，我想聪明的你不难明白，原来我们装的插件就是 github 上开源的 go 插件，而 sqs 是用户，goreturns 是项目名称。我们为了尊重插件的开发者，直接就按照 github 的页面编排的目录，直接把我们的项目克隆到 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\sqs\goreturns 文件夹之中。所以我们就在 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\ 目录下，新建一个文件夹为 sqs 我想用过类 Unix 系统的用户都应该明白了，我们相当于建了一个 sqs 用户的个人文件夹，里面可以放他个人的项目，而这个用户本来是 <a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a> 的用户。打开我们的控制台，在 $GOPATH\src\<a href="https://link.zhihu.com/?target=http%3A//github.com">http://github.com</a>\sqs\ 路径下，执行 git clone git@github.com:sqs/goreturns.git 命令，将项目克隆到我们本地。</p><p>以下是在我电脑上执行的过程，可以看出来下载网速较慢，因此花了很长时间：</p><p>这里是简单的列表：</p><ol><li>配置信息缓存 artisan config:cache</li><li>路由缓存 artisan route:cache</li><li>类映射加载优化 artisan optimize</li><li>自动加载优化 composer dumpautoload</li><li>使用 Memcached 来存储会话 config/session.php</li><li>使用专业缓存驱动器 config/cache.php</li><li>数据库请求优化</li><li>为数据集书写缓存逻辑</li><li>使用即时编译器（JIT），如：HHVM</li><li>开启 OpCache</li><li>前端资源合并 Elixir</li></ol><h3>1. 配置信息缓存</h3><p>使用以下 Artisan 自带，把 config 文件夹里所有配置信息合并到一个文件里，减少运行时文件的载入数量：</p><pre>php artisan config:cache</pre><p>上面命令会生成文件 bootstrap/cache/config.php，可以使用以下命令来取消配置信息缓存：</p><pre>php artisan config:clear</pre><p>此命令做的事情就是把 bootstrap/cache/config.php 文件删除。</p><p>水电费</p><p>水电费</p><blockquote><em>注意：配置信息缓存不会随着更新而自动重载，所以，开发时候建议关闭配置信息缓存，一般在生产环境中使用，可以配合 </em><a href="https://learnku.com/docs/laravel/5.1/envoy"><em>Envoy 任务运行器</em></a><em> 一起使用。</em></blockquote><p>阿斯顿发斯蒂芬</p><h3>2. 路由缓存</h3><p>路由缓存可以有效的提高路由器的程序中效果越加明显</p><pre>php artisan route:cache</pre><p>bootstrap/cache/routes.php 文件，需要注意的是，路由缓存不支持路由匿名函数编写逻辑，详见：<a href="https://learnku.com/docs/laravel/5.1/controllers#%E8%B7%AF%E7%94%B1%E7%BC%93%E5%AD%98">文档 - 路由缓存</a>。</p><p>可以使用下面命令清除路由缓存：</p><pre>php artisan route:clear</pre><p>此命令做的事情就是把 bootstrap/cache/routes.php 文件删除。</p><blockquote><em>注意：路由缓存不会随着更新而自动重载，所以，开发时候建议关闭路由缓存，一般在生产环境中使用，可以配合 </em><a href="https://learnku.com/docs/laravel/5.1/envoy"><em>Envoy 任务运行器</em></a><em> 一起使用。</em></blockquote><h3>3. 类映射加载优化</h3><p>optimize 命令把常用加载的类合并到一个文件里，通过减少文件的加载，来提高运行效率：</p><pre>php artisan optimize --force</pre><p>会生成 bootstrap/cache/compiled.php 和 bootstrap/cache/services.json 两个文件。</p><p>你可以可以通过修改 config/compile.php 文件来添加要合并的类。</p><p>在 production 环境中，参数 --force 不需要指定，文件就会自动生成。</p><p>要清除类映射加载优化，请运行以下命令：</p><pre>php artisan clear-compiled</pre><p>此命令会删除上面 optimize 生成的两个文件。</p><blockquote><em>注意：此命令要运行在 </em><em>php artisan config:cache 后，因为 </em><em>optimize 命令是根据配置信息（如：</em><em>config/app.php 文件的 </em><em>providers 数组）来生成文件的。</em></blockquote><h3>4. 自动加载优化</h3><p>此命令不止针对于 Laravel 程序，适用于所有使用 composer 来构建的程序。此命令会把 PSR-0 和 PSR-4 转换为一个类映射表，来提高类的加载速度。</p><pre>composer dumpautoload -o</pre><blockquote><em>注意：</em><em>php artisan optimize --force 命令里已经做了这个操作。</em></blockquote><h3>5. 使用 Memcached 来存储会话</h3><p>每一个 Laravel 的请求，都会产生会话，修改会话的存储方式能有效提高程序效率，会话的配置信息是 config/session.php，建议修改为 Memcached 或者 Redis 等专业的缓存软件：</p><pre>&#39;driver&#39; =&gt; &#39;memcached&#39;,</pre><h3>6. 使用专业缓存驱动器</h3><p>「缓存」是提高应用程序运行效率的法宝之一，默认缓存驱动是 file 文件缓存，建议切换到专业的缓存系统，如 Redis 或者 Memcached，不建议使用数据库缓存。</p><pre>&#39;default&#39; =&gt; &#39;redis&#39;,</pre><h3>7. 数据库请求优化</h3><p>数据库请求优化</p><ul><li>数据关联模型读取时使用 <a href="https://learnku.com/docs/laravel/5.1/eloquent-relationships#%E9%A2%84%E5%8A%A0%E8%BD%BD">延迟预加载</a> 和 <a href="https://learnku.com/docs/laravel/5.1/eloquent-relationships#%E9%A2%84%E5%8A%A0%E8%BD%BD">预加载</a> ；</li><li>使用 <a href="https://github.com/barryvdh/laravel-debugbar">Laravel Debugbar</a> 或者 <a href="https://learnku.com/laravel/t/23">Clockwork</a> 留意每一个页面的总数据库请求数量；</li></ul><p>这里的篇幅只写到与 Laravel 相关的，其他关于数据优化的内容，请自行查阅其他资料。</p><h3>8. 为数据集书写缓存逻辑</h3><p>合理的使用 Laravel 提供的缓存层操作，把从数据库里面拿出来的数据集合进行缓存，减少数据库的压力，运行在内存上的专业缓存软件对数据的读取也远远快于数据库。</p><pre>$posts = Cache::remember(&#39;index.posts&#39;, $minutes = 30, function()<br>{<br>    return Post::with(&#39;comments&#39;, &#39;tags&#39;, &#39;author&#39;, &#39;seo&#39;)-&gt;whereHidden(0)-&gt;get();<br>});</pre><p>remember 甚至连数据关联模型也都一并缓存了，多么方便呀。</p><h3>9. 使用即时编译器（JIT）</h3><p>HHVM 是目前比较好的 JIT 实现，能轻轻松松的让你的应用程序在不用做任何修改的情况下，直接提高 50% 或者更高的性能。</p><h3>10. 开启 OpCache</h3><p>PHPhub 之前做个一个实验，具体请见：<a href="https://learnku.com/laravel/t/301">使用 OpCache 提升 PHP 5.5+ 程序性能</a>。</p><h3>11. 前端资源合并</h3><p>作为优化的标准，一个页面只应该加载一个 CSS 和 一个 JS 文件，并且文件要能方便走 CDN，需要文件名随着修改而变化。</p><p>Laravel Elixir 提供了一套简便实用的方案，详细请见文档：<a href="https://learnku.com/docs/laravel/5.1/elixir">Laravel Elixir 文档</a>。</p><blockquote><em>本作品采用</em><a href="https://learnku.com/docs/guide/cc4.0/6589"><em>《CC 协议》</em></a><em>，转载必须注明作者和本文链接</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5e3a34e196f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>