Home  >  Article  >  Backend Development  >  A brief discussion on one of PHP caching technologies_PHP tutorial

A brief discussion on one of PHP caching technologies_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:43:47647browse

近来做了一阵子程序性能的优化工作,有个比较有意思的想法,想提出来和大家交流一下。

Cache是“以空间换时间”策略的典型应用模式,是提高系统性能的一种重要方法。缓存的使用在大访问量的情况下能够极大的减少对数据库操作的次 数,明显降低系统负荷提高系统性能。相比页面的缓存,结果集是一种“原始数据”不包含格式信息,数据量相对较小,而且可以再进行格式化,所以显得相当灵 活。由于php是“一边编译一边执行”的脚本语言,某种程度上也提供了一种相当方便的结果集缓存使用方法——通过动态include相应的数据定义代码段 的方式使用缓存。如果在“RamDisk”上建缓存的话,效率应该还可以得到进一步的提升。以下是一小段示例代码,供参考。

 

// load data with cache

function load_data($id,$cache_lifetime) {

// the return data

$data = array();

// make cache filename

$cache_filename = ‘cache_‘.$id.‘.php‘;

// check cache file‘s last modify time

$cache_filetime = filemtime($cache_filename);

if (time() - $cache_filetime <= $cache_lifetime) {

//** the cache is not expire

include($cache_filename);

} else {

//** the cache is expired

// load data from database

// ...

while ($dbo->nextRecord()) {

// $data[] = ...

}

// format the data as a php file

$data_cache = "
while (list($key, $val) = each($data)) {

$data_cache .= "$data[‘$key‘]=array(‘";

$data_cache .= "‘NAME‘=>"".qoute($val[‘NAME‘])."","

$data_cache .= "‘VALUE‘=>"".qoute($val[‘VALUE‘])."""

$data_cache .= ";);rn";

}

$data_cache = "?>rn";

// save the data to the cache file

if ($fd = fopen($cache_filename,‘w+‘)) {

fputs($fd,$data_cache);

fclose($fd);

}

}

return $data;

}

?>

适 用情况:

1.数据相对比较稳定,主要是读取操作。

2.文件操作要比数据库操作快。

3.复杂数据访 问,大数据量访问,密集数据访问,系统数据库负载极重。

4.Web/DB分离结构或者多Web单DB结构。

未经证实 的问题:

1.并发访问时对文件的读写是否会引起锁定问题。

2.涉及到的数据文件太多时,性能如何。

扩 展思路:

1.生成javaScript数据定义代码,在客户端调用。

2.还未想到……

望共同探 讨。

缓存

如果你想要让自己庞大的PHP应用有更好的性能表现,采用缓存也是一种很好 的方法。现在已经有许多缓存方案可供选择,其中包括:Zend Cache,APC,和Afterburner Cache。

所有这些 产品都属于“缓存模块”。当第一次出现对.php文件的请求时,它们会在Web服务器内存中保存PHP的中间代码,此后就用“经过编译”的版本响应后继的 请求。这种方法确实能够改善应用的性能,因为它使得磁盘访问量减低到了最少的程度(代码已经读取和解析),代码直接在内存中运行使得服务器响应请求的速度 大大提高。当然,缓存模块还会监视PHP源文件的变化,必要时重新缓存页面,从而防止了用户得到的页面仍旧由过时的PHP代码生成。由于缓存模块能够明显 地降低服务器的负载、提高PHP应用的响应效率,因此它们非常适合于负载较大的网站使用。

如何选择这些缓存产品

Zend Cache是Zend Technologies公司的商业软件,而Zend Technologies就是前面提到的那个为我们提供PHP引擎和免费Zend Optimizer的公司。Zend Cache确实是名不虚传!对于大型的PHP页面,你可以感觉到第一次运行之后速度就会有所提高,而且服务器也会有更多的可用资源。遗憾的是这个产品并不 免费,不过在有些情形下它仍旧是物超所值。

Afterburner Cache是来自Bware Technologies的免费缓存模块,当前这个产品还是Beta版。Afterburner Cache的做法看起来与Zend Cache差不多,但它对性能的改善程度(还)不能与Zend Cache相比,而且它还不能与Zend Optimizer一起工作。

APC 是Alternative PHP Cache的缩写,它是来自Community Connect的又一个免费缓存模块。这个产品已经具有足够的稳定性供正式场合使用,而且它看起来也能在很大程度上提高响应请求的速度。

有 关压缩

The free Apache module mod_gzip from Remote Communications has the ability to compress static web content for browsers that support this type of content encoding. For the vast majority of static web content, mod_gzip works very well. mod_gzip can be easily compiled into Apache and can also be used as a DSO. According to Remote Communications, mod_gzip can also compress dynamic content from mod_php, mod_perl, etc. I tried again and again, but it didn't seem to work. I have read many forums and articles about mod_gzip, and it seems that this problem is expected to be solved in the next version of mod_gzip (probably 1.3.14.6f). Until then, we can use mod_gzip on the static parts of the website.

However, sometimes we really don’t want to compress dynamic content, so we have to find other ways. One way is to use class.gzip_encode.php, which is a PHP class that can be used to compress the content of the page by calling certain functions of the class at the beginning and end of the PHP script. If you want to implement this solution at the website level, you can call these functions from the auto_PRepend and auto_append directives in the php.ini file. Although this method is effective, it undoubtedly brings more overhead to high-load websites. For detailed instructions on how to use this class, see its source code. Its source code description is quite complete, and the author tells you everything you need to know.

PHP 4.0.4 has a new output cache handler ob_gzhandler, which is similar to the previous class, but its usage is different. The content to be added to php.ini when using ob_gzhandler is as follows:

output_handler = ob_gzhandler ;

This line of code causes PHP to activate output caching and compress everything it sends out. If for some reason you don’t want to add this line of code to php.ini, you can also change the default server behavior (no compression) through the .htaccess file in the directory where the PHP source file is located. The syntax is as follows:

php_value output_handler ob_gzhandler

Or call it from PHP code, as shown below: ob_start(“ob_gzhandler”);

The method of using output cache handles is indeed very effective and does not bring any special load to the server. But it must be noted that Netscape Communicator has poor support for compressed graphics, so unless you can ensure that all users use IE browser, you should disable compressed JPEG and GIF graphics. Generally, this compression works for all other files, but it is recommended that you test it separately for each browser, especially if you use special plug-ins or data viewers. This is especially important.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478789.htmlTechArticleI have been doing optimization work on program performance for a while recently, and I have an interesting idea that I would like to share with everyone. Let’s talk. Cache is a typical application mode of the "space for time" strategy,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn