yii2 提速


1.环境准备

使用: latest stable PHP version

使用: APC for PHP 5.4

2.准备framework的生产环境

关闭调试模块:

defined('YII_DEBUG') or define('YII_DEBUG', false);

3.开启opc缓存

PHP opcode cache

4.开启: ActiveRecord database schema caching

前提是您的应用使用active record,这样可以节省数据库的准备时间,原文为:

If the application is using Active Record, we should turn on the schema caching to save the time of parsing database schema. 

方法日下: 'enableSchemaCache' => true,

 'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=mydatabase',
            'username' => 'root',
            'password' => '',
            'enableSchemaCache' => true,

5.最小化的使用assets

这个玩意,是需要加载生成js和css的。

6.使用更好的方式存储session

当有大量请求的时候,最好是放到redis或者memcache中,个人认为redis比较好一些,他支持的类型多,譬如缓存,用redis,都可以存储进去

而memcache支持的类型少,有一些需要放到文件中。

7.更好的使用局部缓存,整页缓存 ,http 缓存等

8.数据库索引等优化,尽量多的使用视图(当有必要)。

9.

查询操作limit限制。

查询结果使用AsArray,这样可以节省内存因为这样返回的是数组,而不是对象,譬如:

 $posts = Post::find()->orderBy('id DESC')->limit(100)->asArray()->all();

10. Composer Atuoloader 优化

使用命令:

php composer.phar dumpautoload -o
进行优化。原文如下:

The autoloader's performance is really important in modern PHP applications. Optimizing Composer's autoloader using php composer.phar dumpautoload -o is the way to have maximum performance. But it is the responsibility of the application developer to apply the optimization.

php composer.phar dumpautoload -o

11.通过脚本处理中间数据

可以通过cron定时任务批量处理数据,譬如产品的特价,产品的过滤等等

这样查询可以通过中间处理脚本的数据老快速的进行。

12.

If nothing helps

If nothing helps, never assume what may fix performance problem. Always profile your code instead before changing anything. The following tools may be helpful:











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值