cakephp好吗

It seems that whenever I mention CakePHP to the developer community, those who have not used it think of it as a slow framework. Indeed it isn’t the fastest according to the results of many benchmarks – out of the box that is – but what it might lack in performance it certainly makes up for in Rapid Application Development.

看来,每当我向开发人员社区提及CakePHP时,那些没有使用过CakePHP的人都会将其视为缓慢的框架。 确实,根据许多基准测试的结果来看,它并不是最快的-实际上,但是它可能在快速应用程序开发中所弥补的性能不足。

By applying a few simple modifications, and even some more complex enhancements, CakePHP can be sped up quite a bit. By the time you work your way through even half of these changes, the performance of your your CakePHP site will be comparable to many other popular PHP frameworks, with the advantage that your development speed will never falter!

通过应用一些简单的修改,甚至进行一些更复杂的增强,CakePHP可以大大加速。 当您完成这些更改的一半时,您的CakePHP站点的性能将可与许多其他流行PHP框架相提并论,其优点是您的开发速度将永无止境!

There are two types of modifications that I will be describing in the following article. The first is code changes, e.g. these will work for anyone even if you are running on a shared hosting environment. The second type is geared towards users who have their own dedicated or virtual server that they can add and remove software as required.

在下一篇文章中,我将描述两种类型的修改。 首先是代码更改,例如,即使您在共享主机环境上运行,这些更改也适用于任何人。 第二种类型适用于拥有自己专用或虚拟服务器的用户,他们可以根据需要添加和删除软件。

Do not fear though, if you can only follow the first set you will not be disappointed.

但是不要担心,如果您只能遵循第一个设置,您将不会失望。

升级CakePHP版本 (Upgrade CakePHP Versions)

It’s important to note that people’s misconceptions around CakePHP’s speed probably date all the way back to versions 1.1 or 1.2. In version 1.3, the core development team performed a serious overhaul of CakePHP’s underlying architecture. In fact, I performed several benchmark tests comparing CakePHP versions 1.2 against 1.3 against 2.0. The results were astonishing. By simply updating from 1.2 to 1.3, I saw an immediate decrease by over 70% in the average time it took to load a page.

重要的是要注意,人们对CakePHP速度的误解可能可以追溯到1.1或1.2版。 在1.3版中,核心开发团队对CakePHP的基础体系结构进行了全面的检查。 实际上,我进行了一些基准测试,将CakePHP 1.2版与1.3版对2.0版进行了比较。 结果是惊人的。 通过简单地从1.2更新到1.3,我发现加载页面的平均时间立即减少了70%以上。

If you’ve been delaying upgrading your version of CakePHP and you are still using version 1.2 or less, the first thing you should do is upgrade to at least version 1.3, but preferably version 2.3.x would be even better.

如果您一直在延迟升级CakePHP的版本,但仍在使用1.2或更低版本,那么您应该做的第一件事就是至少升级到1.3版,但最好是2.3.x版甚至更好。

The CakePHP Cookbook provides some very detailed migration guides:

CakePHP Cookbook提供了一些非常详细的迁移指南:

  • Migrating from CakePHP 1.2 to 1.3

    从CakePHP 1.2迁移到1.3

  • 2.0 Migration Guide

    2.0迁移指南

  • 2.1 Migration Guide

    2.1迁移指南

  • 2.2 Migration Guide

    2.2迁移指南

  • 2.3 Migration Guide

    2.3迁移指南

禁用调试模式 (Disable Debug Mode)

Don’t laugh! This might seem obvious, but in a mad scramble to move code into production it can be very easy to forget to turn off debugging. A typical setup that I follow is to create multiple app/Config/core.php files: one for my local environment, one for dev, one for staging, and one for production. These files contain the different settings based on the target environment.

别笑 这看起来似乎很明显,但是在疯狂地将代码移入生产环境时,很容易忘记关闭调试。 我遵循的典型设置是创建多个app / Config / core.php文件:一个用于我的本地环境,一个用于开发人员,一个用于暂存,以及一个用于生产。 这些文件包含基于目标环境的不同设置。

The key statement to change is Configure::write('debug', 2) to Configure::write('debug', 0).

要更改的关键语句是Configure::write('debug', 2)Configure::write('debug', 0)

The change hides all error messages and no longer refreshes the model caches. This is extremely important because, by default, each page load causes all of your models to be dynamically generated instead of cached from the first page load.

所做的更改将隐藏所有错误消息,并且不再刷新模型缓存。 这是非常重要的,因为默认情况下,每个页面加载都会导致动态生成所有模型,而不是从第一个页面加载中进行缓存。

禁用递归查找语句 (Disable Recursive Find Statements)

When you perform a query using the find() method, recursion is set to 0 by default. This indicates that CakePHP should attempt to join any first level related models. For example, if you have a user model that has many user comments, each time you perform a query on the users table it will join the comments table as well.

使用find()方法执行查询时,默认情况下递归设置为0。 这表明CakePHP应该尝试加入任何与第一级相关的模型。 例如,如果您的用户模型包含许多用户注释,则每次在用户表上执行查询时,它也会加入注释表。

The processing time on performing, returning, and creating an associative array with this data can be significant, and I’ve actually seen CakePHP sites crash in production because of this!

使用此数据执行,返回和创建关联数组的处理时间可能很长,实际上,我已经看到CakePHP网站因此在生产中崩溃!

My preferred approach to making sure that the default recursion is none is to override the setting in app/Model/AppModel.php by adding the following code:

确保默认递归为none的首选方法是通过添加以下代码来覆盖app/Model/AppModel.php的设置:

<?php
class AppModel extends Model {
public $recursive = -1;
}

缓存查询结果 (Cache Query Results)

This is truly my favorite optimization. I’d like to think I uncovered it myself, but I’m sure that would be debated as I’ve seen other articles discuss a similar solution.

这确实是我最喜欢的优化。 我想我自己发现了它,但是我相信这会受到争论,因为我已经看到其他文章讨论了类似的解决方案。

In many web applications, there are probably a lot of queries going to the database that do not necessarily need to. By overriding the default find() function inside app/Model/AppModel.php, I’ve made it easy to cache the full associative array results of queries. This means that not only do I avoid hitting the database, I even avoid the processing time of CakePHP converting the results into an array. The code required is as follows:

在许多Web应用程序中,可能有很多查询不一定要去数据库。 通过覆盖app/Model/AppModel.php的默认find()函数,我可以轻松地缓存查询的完整关联数组结果。 这意味着不仅可以避免访问数据库,还可以避免CakePHP将结果转换为数组的处理时间。 所需的代码如下:

<?php
class AppModel extends Model
{
public $recursive = -1;
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
$doQuery = true;
// check if we want the cache
if (!empty($fields['cache'])) {
$cacheConfig = null;
// check if we have specified a custom config
if (!empty($fields['cacheConfig'])) {
$cacheConfig = $fields['cacheConfig'];
}
$cacheName = $this->name . '-' . $fields['cache'];
// if so, check if the cache exists
$data = Cache::read($cacheName, $cacheConfig);
if ($data == false) {
$data = parent::find($conditions, $fields,
$order, $recursive);
Cache::write($cacheName, $data, $cacheConfig);
}
$doQuery = false;
}
if ($doQuery) {
$data = parent::find($conditions, $fields, $order,
$recursive);
}
return $data;
}
}

Subtle changes need to be made to the queries we wish to cache. A basic query which looks like this:

需要对我们希望缓存的查询进行细微更改。 基本查询如下所示:

<?php
$this->User->find('list');

requires updating to include caching information:

需要更新以包括缓存信息:

<?php
$this->User->find('list',
array('cache' => 'userList', 'cacheConfig' => 'short')
);

Two additional values are added: the cache name and the cache config that should be used.

添加了两个附加值:应使用的缓存名称和缓存配置。

Two final changes must be made to app/Config/core.php. Caching must be turned on and the cacheConfig value that is used must be defined. First, uncomment the following line:

必须对app/Config/core.php进行两个最终更改。 必须打开缓存,并且必须定义所使用的cacheConfig值。 首先,取消注释以下行:

<?php
Configure::write('Cache.check', true);

And then, add a new cache config as follows (updating the parameters as required for name and expiry):

然后,按如下所示添加新的缓存配置(根据名称和有效期的要求更新参数):

<?php
Cache::config('short', array(
'engine' => 'File',
'duration'=> '+5 minutes',
'probability'=> 100,
'path' => CACHE,
'prefix' => 'cache_short_'
));

All of the options above can be updated to extend the duration, change the name, or even define where the cache should be stored.

上面的所有选项都可以更新,以延长持续时间,更改名称,甚至定义应将缓存存储在何处。

For a more detailed explanation on how to add, update, and purge the cache, continue reading about the specific caching optimization on my blog.

有关如何添加,更新和清除缓存的更详细说明,请继续阅读我的Blog上有关特定缓存优化的内容 。

Don’t consider this the end of your CakePHP caching, though. You can cache controller actions, views, and even helper functions, too. Explore the Cache Component in the CakePHP book for more information.

但是,不要认为这是CakePHP缓存的结尾。 您也可以缓存控制器动作,视图,甚至辅助功能。 在CakePHP书籍中浏览Cache Component以获得更多信息。

安装基于内存的缓存 (Install Memory Based Caching)

By default, PHP sessions are stored on disk (typically in a temp folder). This means that each time you access the session, PHP needs to open the session’s file and decode the information it contains. The problem is disk I/O can be quite expensive. Accessing items from memory opposed to disk I/O is immensely faster.

默认情况下,PHP会话存储在磁盘上(通常在temp文件夹中)。 这意味着每次访问会话时,PHP需要打开会话的文件并对包含的信息进行解码。 问题是磁盘I / O可能非常昂贵。 从与磁盘I / O相对的内存访问项目非常快。

There are two nice approaches to session-related disk I/O. The first one is to configure a RAM disk on your server. Once configured, the drive will be mounted like any other drive and the session.save_path value in php.ini would be updated to point to the new RAM disk. The second way to is by installing software like Memcached, an open source caching system that allows objects to be stored in memory.

与会话相关的磁盘I / O有两种不错的方法。 第一个是在服务器上配置RAM磁盘。 配置完成后,该驱动器将像其他任何驱动器一样挂载,并且php.inisession.save_path value将被更新以指向新的RAM磁盘。 第二种方法是安装像Memcached这样的软件,这是一个开放源代码缓存系统,该系统允许将对象存储在内存中。

If you’re wondering which approach is best for you, the way to decide this is by answering the following question: Will more than one server be required to access this memory simultaneously? If yes, you’ll want to choose Memcached since it can be installed on a separate system allowing other servers to access it. Whereas, if you are just looking to speed up your single web server, choosing the RAM disk solution is nice and quick and requires no additional software.

如果您想知道哪种方法最适合您,可以通过回答以下问题来决定采用哪种方法:是否需要多个服务器同时访问此内存? 如果是,那么您将要选择Memcached,因为它可以安装在单独的系统上,并允许其他服务器访问它。 而如果您只是想提高单个Web服务器的速度,则选择RAM磁盘解决方案既方便又快捷,并且不需要其他软件。

Depending on your operating system, installing Memcached can be as simple as typing sudo aptitude install memcached. Once installed, you can configure PHP to store sessions in memory opposed to on disk by updating your php.ini:

根据您的操作系统,安装Memcached就像输入sudo aptitude install memcached一样简单。 安装完成后,您可以通过更新php.ini将PHP配置为将会话存储在内存中而不是磁盘上:

session.save_handler = memcache
session.save_path = 'tcp://127.0.0.1:11211'

If Memcached is installed on a different port or host, then modify your entries accordingly.

如果Memcached安装在其他端口或主机上,请相应地修改您的条目。

After you have finished installing it on your server, you will also need to install the PHP memcache module. Once again depending on your operating system, one of these commands will work for you:

在服务器上完成安装后,还需要安装PHP memcache模块。 再次取决于您的操作系统,以下命令之一将对您起作用:

pecl install memcache

or:

要么:

sudo aptitude install php5-memcache

删除Apache并安装Nginx (Removing Apache and Installing Nginx)

Apache is still the favorite according to recent statistics, but Ngnix adoption is picking up a lot of steam when it comes to the most-heavily trafficked websites on the Internet today. In fact, Nginx is becoming an extremely popular replacement for Apache.

根据最近的统计数据 ,Apache仍然是最受欢迎的,但是当涉及到当今Internet上最繁忙的网站时,Ngnix的采用正在加速发展。 实际上,Nginx正在成为Apache的非常受欢迎的替代品。

Apache is like Microsoft Word, it has a million options but you only need six. Nginx does those six things, and it does five of them 50 times faster than Apache. — Chris Lea

Apache就像Microsoft Word一样,它有上百万个选项,但您只需要六个。 Nginx可以完成这六件事,并且其中五件事要比Apache快50倍。 — 克里斯·莉亚

Nginx differs from Apache because it is a process based server, whereas Nginx is event driven. As your web server’s load grows, Apache quickly begins to be a memory hog. To properly handle new requests, Apache’s worker processes spin up new threads causing increase memory and wait-time creating new threads. Meanwhile, Nginx runs asynchronously and uses one or very few light-weight threads.

Nginx与Apache不同,因为它是基于进程的服务器,而Nginx是事件驱动的。 随着Web服务器负载的增加,Apache很快开始成为内存消耗大户。 为了正确处理新请求,Apache的工作进程启动了新线程,从而增加了内存并增加了创建新线程的等待时间。 同时,Nginx异步运行,并使用一个或很少的轻量级线程。

Nginx also is extremely fast at serving static files, so if you are not using a content delivery network then you’ll definitely want to consider using Nginx for this as well.

Nginx在提供静态文件方面也非常快,因此,如果您不使用内容交付网络,那么您肯定会考虑使用Nginx。

In the end, if you are short on memory Nginx will consume as little as 20-30M where Apache might be consuming upwards to 200M for the same load. Memory might be cheap for your PC, but not when it comes to paying for servers in the cloud!

最后,如果您的内存不足,Nginx将消耗20-30M的内存,而对于相同的负载,Apache可能消耗高达200M的内存。 内存对于您的PC可能很便宜,但在云中为服务器付费时却不便宜!

For a more in-depth breakdown between Apache and Nginx visit the Apache vs Nginx WikiVS page.

有关Apache和Nginx之间更深入的细分,请访问Apache vs Nginx WikiVS页面 。

HowToForge has an excellent article for configuring Nginx on your server. I suggest following the step-by-step guide to install and configuring Nginx with php-fpm.

HowToForge上有一篇很棒的文章,介绍如何在服务器上配置Nginx。 我建议按照分步指南使用php-fpm安装和配置Nginx。

配置Nginx以使用Memcached (Configure Nginx to use Memcached)

Once you’ve installed Nginx and Memcached, making them leverage each other will even further extend your performance. Even though CakePHP applications are dynamic, it’s likely the 80-90% is still relatively static – meaning that it only changes at specific intervals.

一旦安装了Nginx和Memcached,使它们相互利用将进一步提高您的性能。 尽管CakePHP应用程序是动态的,但80-90%可能仍然相对静态-这意味着它仅在特定的时间间隔内发生变化。

By making the following edits to your Nginx config file, Memcached will begin serving your Nginx requests that have already been processed and stored in memory. This means that only a few requests will actually invoke PHP which significantly increases your website’s speed.

通过对Nginx配置文件进行以下编辑,Memcached将开始处理已处理并存储在内存中的Nginx请求。 这意味着实际上只有很少的请求会调用PHP,这大大提高了您网站的速度。

server {
listen 80;
server_name endyourif.com www.endyourif.com;
access_log /var/log/nginx/endyourif-access.log;
error_log /var/log/nginx/endyourif-error.log;
root /www/endyourif.com/;
index index.php index.html index.htm;
# serve static files
location / {
# this serves static files that exists without
# running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}
# this sends all-non-existing file or directory requests
# to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location ~ .php$ {
set $memcached_key '$request_uri';
memcached_pass 127.0.0.1:11211;
default_type       text/html;
error_page 404 405 502 = @no_cache;
}
location @no_cache {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

The above configuration specifies that static files should be served immediately. PHP files should be directed to Memcached to serve the page if it’s been cached. If it doesn’t exist in the cache, the @nocache location is reached which acts like your previous PHP setup to serve the page via php-fpm.

上面的配置指定应立即提供静态文件。 如果已缓存页面,则应将PHP文件定向到Memcached来提供页面。 如果缓存中不存在该文件, @nocache到达@nocache位置,其作用类似于您以前PHP设置,以通过php-fpm服务该页面。

Don’t forget to update your app/Config/core.php to feed the memcache cache. The following:

不要忘了更新您的app/Config/core.php以提供Memcache缓存。 下列:

<?php
$engine = 'File';
if (extension_loaded('apc') && function_exists('apc_dec') && (php_sapi_name() !== 'cli' || ini_get('apc.enable_cli'))) {
$engine = 'Apc';
}

becomes:

变成:

<?php
$engine = 'Memcache';

Our code for caching queries from earlier also requires updating:

我们用于缓存较早版本的查询的代码还需要更新:

<?php
Cache::config('short', array(
'engine' => 'Memcache',
'duration'=> '+5 minutes',
'probability'=> 100,
'path' => CACHE,
'prefix' => 'cache_short_'
));

删除MySQL并安装Percona (Remove MySQL and Install Percona)

The final server modification I recommend is to install Percona‘s build of MySQL. The Percona team has spent many years understanding and fine-tuning the database’s architecture for optimal performance. It is best to follow the installation instructions from Percona as it describes the several different installation options.

我建议对服务器进行的最终修改是安装PerconaMySQL版本。 Percona团队花了很多年的时间了解和微调数据库的体系结构以获得最佳性能。 最好遵循Percona的安装说明,因为它描述了几种不同的安装选项。

摘要 (Summary)

There are quite a lot of techniques to ensure CakePHP will run lightning fast. Some changes may be more difficult to make than others, but do not become discouraged. No matter what your framework or language, optimization comes at a cost, and CakePHP’s ability to let you rapidly develop extremely complex web applications will strongly outweigh the effort involved in optimizing things once your app is complete.

有很多技术可以确保CakePHP快速运行。 某些更改可能比其他更改更难执行,但不要灰心。 无论您使用哪种框架或语言,优化都是要付出代价的,而CakePHP能够让您快速开发极其复杂的Web应用程序的能力将大大超过完成应用程序后进行优化的工作量。

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/speeding-up-your-cakephp-websites/

cakephp好吗

cakephp好吗_更好的性能-加速CakePHP网站相关推荐

  1. 性能测试接口请求超时问题_如何获得更好的性能:超时的情况

    性能测试接口请求超时问题 by Alex Nadalin 通过亚历克斯·纳达林 如何获得更好的性能:超时的情况 (How to get better performance: the case for ...

  2. steam数据流过滤_如何通过Steam家用流媒体获得更好的性能

    steam数据流过滤 Steam's In-Home Streaming is an awesome way to get the top-tier PC graphics you love, wit ...

  3. 更好的Java虚拟机Zing: 更好的性能,无停顿,更快的启动

    Zing虚拟机文档Understanding Java Garbage Collection(了解Java垃圾收集) 首先说明这个Zing是收费的,但是他也是优秀的,我觉得我们可以研究下他的一些思想对 ...

  4. IntelliJ IDEA 2019.3 发布,启动更快,性能更好(新特性解读)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 原文地址:https://www.jetbrains.com/id ...

  5. 超越Swin Transformer!谷歌提出了收敛更快、鲁棒性更强、性能更强的NesT

    [导读]谷歌&罗格斯大学的研究员对ViT领域的分层结构设计进行了反思与探索,提出了一种简单的结构NesT,方法凭借68M参数取得了超越Swin Transformer的性能. 文章链接:htt ...

  6. 扩有mysql的磁盘_为提高MySQL性能而在磁盘IO方面的设置

    提起MySQL数据库在硬件方面的优化无非是CPU.内存和IO.下面我们着重梳理一下关于磁盘I/O方面的优化. 1.磁盘冗余阵列RAID RAID(Redundant Array of Inexpens ...

  7. verilog 浮点转定点_定点数优化:性能成倍提升

    定点数这玩意儿并不是什么新东西,早年 CPU 浮点性能不够,定点数技巧大量活跃于各类图形图像处理的热点路径中.今天 CPU 浮点上来了,但很多情况下整数仍然快于浮点,因此比如:libcario (gn ...

  8. Java 启动和停止界面_IntelliJ IDEA 2019.3 发布,启动更快,性能更好(新特性解读)...

    点击上方"小哈学Java",选择"星标" 回复"资源",领取全网最火的Java核心知识总结~ 2019.3 11 月 28 IntelliJ ...

  9. java jpa性能_[Java Performance] 数据库性能最佳实践 - JPA和读写优化

    数据库性能最佳实践 当应用须要连接数据库时.那么应用的性能就可能收到数据库性能的影响. 比方当数据库的I/O能力存在限制,或者因缺失了索引而导致运行的SQL语句须要对整张表进行遍历.对于这些问题.只相 ...

最新文章

  1. 讨论:Service层需要接口吗?
  2. poj 2373(单调队列优化dp)
  3. VisualSVN Server Manager创建版本库以及TortoiseSVN的使用
  4. flume 中的 hdfs sink round 和roll
  5. [POJ 1742] Coins 【DP】
  6. 2019 live tex 发行版_TeX Live 2019安装指南
  7. LINUX创建桌面运行快捷方式
  8. win7 Embedded EWF与HORM特性(实战验证)
  9. office2016风格后台管理系统html模板下载-uimaker设计
  10. zabbix安装教程
  11. NC6 转库单、采购入库单、库存委托入库单签字后自动生成调拨订单
  12. 西门子g120变频器接线图_西门子1500PLC通过工艺对象对G120变频器组态和调试
  13. win7旗舰版恢复出厂设置没有修复计算机,教你win7旗舰版怎么恢复出厂设置
  14. 【Liunx_QT触摸屏不管用】
  15. JS转换为数字的方法Number()、parseInt()和 parseFloat()
  16. C语言简介之进制转换,原码、反码、补码,位运算符,函数
  17. qt listwidget 关键字颜色_seo关键字优化工具如何收费
  18. linux管理口连交换机灯不亮,交换机端口指示灯不亮了怎么办?
  19. Propensity score简介
  20. RPCA 稳健主成分分析/鲁棒主成分分析

热门文章

  1. 刚学会开车时的一些基本注意点,注意事项
  2. BLE蓝牙开发 ANCS服务开发
  3. CSS2中display:table属性的用法详解
  4. 伦敦银最新价格走势图与买卖点
  5. linux shell 用cp -f 还是有覆盖提醒
  6. win7系统ie11浏览器为什么开发者工具一片空白
  7. 2021年全球合成石墨收入大约8775.9百万美元,预计2028年达到11120百万美元
  8. JUC系列(六) 线程池
  9. 一文详解 | 低代码发展的 “背后推手”
  10. C++标准里 string和wstring