亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 PHP教程.應(yīng)用實(shí)例11

PHP教程.應(yīng)用實(shí)例11

Jun 21, 2016 am 09:14 AM
cache gzip php zend

教程|應(yīng)用實(shí)例

PHP應(yīng)用提速面面觀
PHP最大的優(yōu)點(diǎn)之一顯然在于它的快速度。一般情況下PHP總是具有足夠的速度支持Web內(nèi)容動(dòng)態(tài)生成,許多時(shí)候你甚至無法找出比它更快的方法。然而,當(dāng)你不得不面對(duì)龐大的訪問量、高負(fù)荷的應(yīng)用、有限的帶寬以及其他各種帶來性能瓶頸的因素時(shí),你可能會(huì)問問自己是否可以做點(diǎn)什么讓網(wǎng)站運(yùn)行得更好?;蛟S只要加上一個(gè)很不起眼的免費(fèi)模塊,你的PHP應(yīng)用性能以及Web服務(wù)器響應(yīng)速度就會(huì)有顯著的改善。本文討論的就是如何進(jìn)一步提高php應(yīng)用的性能,給用戶以更美妙的瀏覽感受。本文分三個(gè)方面(代碼優(yōu)化、緩存、內(nèi)容壓縮)闡述提高PHP應(yīng)用性能的各種技術(shù),并介紹各個(gè)領(lǐng)域的知名產(chǎn)品。

代碼優(yōu)化
首先我們來看看代碼優(yōu)化。注意,這里的代碼優(yōu)化可不是指把代碼寫得更加美觀漂亮,因?yàn)檫@恐怕已經(jīng)是眾所周知沒有必要繼續(xù)討論了;另外,如果你已經(jīng)考慮到了速度問題,很可能你早就對(duì)PHP的源代碼作了一些優(yōu)化。不過,有些工具卻能夠自動(dòng)地幫助我們完成這些繁雜的工作,如Zend Optimizer就是這樣一個(gè)工具。Zend Optimizer可以從Zend Technologies免費(fèi)得到,但你必須同意它的許可約定,注意它不是以GPL方式發(fā)行。Zend Optimizer獲取由Zend Engine運(yùn)行時(shí)編譯生成的中間代碼,并對(duì)它進(jìn)行優(yōu)化,從而使得中間代碼具有更快的執(zhí)行效率。

Zend Optimizer的安裝方法非常簡單,你只需下載為自己所用平臺(tái)提供的預(yù)編譯版本,把下面兩行代碼加入到php.ini,然后重新啟動(dòng)Web服務(wù)器即可:

zend_optimizer.optimization_level=15
zend_extension="/path/to/ZendOptimizer.so"
zend_loader.enable=Off

這里額外增加的第三行代碼是可選的。禁止zend_loader似乎能夠讓Zend Optimizer的速度更快一點(diǎn),所以在php.ini中加上這行代碼是值得的。注意:只有當(dāng)你不使用Zend Encoder Runtime時(shí),你才可以禁用zend_loader。

緩存
如果你想要讓自己龐大的PHP應(yīng)用有更好的性能表現(xiàn),采用緩存也是一種很好的方法?,F(xiàn)在已經(jīng)有許多緩存方案可供選擇,其中包括:Zend Cache,APC,和Afterburner Cache。

所有這些產(chǎn)品都屬于“緩存模塊”。當(dāng)?shù)谝淮纬霈F(xiàn)對(duì).php文件的請求時(shí),它們會(huì)在Web服務(wù)器內(nèi)存中保存PHP的中間代碼,此后就用“經(jīng)過編譯”的版本響應(yīng)后繼的請求。這種方法確實(shí)能夠改善應(yīng)用的性能,因?yàn)樗沟么疟P訪問量減低到了最少的程度(代碼已經(jīng)讀取和解析),代碼直接在內(nèi)存中運(yùn)行使得服務(wù)器響應(yīng)請求的速度大大提高。當(dāng)然,緩存模塊還會(huì)監(jiān)視PHP源文件的變化,必要時(shí)重新緩存頁面,從而防止了用戶得到的頁面仍舊由過時(shí)的PHP代碼生成。由于緩存模塊能夠明顯地降低服務(wù)器的負(fù)載、提高PHP應(yīng)用的響應(yīng)效率,因此它們非常適合于負(fù)載較大的網(wǎng)站使用。

如何選擇這些緩存產(chǎn)品
Zend Cache是Zend Technologies公司的商業(yè)軟件,而Zend Technologies就是前面提到的那個(gè)為我們提供PHP引擎和免費(fèi)Zend Optimizer的公司。Zend Cache確實(shí)是名不虛傳!對(duì)于大型的PHP頁面,你可以感覺到第一次運(yùn)行之后速度就會(huì)有所提高,而且服務(wù)器也會(huì)有更多的可用資源。遺憾的是這個(gè)產(chǎn)品并不免費(fèi),不過在有些情形下它仍舊是物超所值。

Afterburner Cache是來自Bware Technologies的免費(fèi)緩存模塊,當(dāng)前這個(gè)產(chǎn)品還是Beta版。Afterburner Cache的做法看起來與Zend Cache差不多,但它對(duì)性能的改善程度(還)不能與Zend Cache相比,而且它還不能與Zend Optimizer一起工作。

APC是Alternative PHP Cache的縮寫,它是來自Community Connect的又一個(gè)免費(fèi)緩存模塊。這個(gè)產(chǎn)品已經(jīng)具有足夠的穩(wěn)定性供正式場合使用,而且它看起來也能在很大程度上提高響應(yīng)請求的速度。

內(nèi)容壓縮
前面我們討論了幾種提高PHP應(yīng)用性能的方法,下面來看看使得瀏覽者感到網(wǎng)站速度太慢的另外一個(gè)重要因素:下載速度。如果PHP應(yīng)用在內(nèi)部Intranet上運(yùn)行,而且每一臺(tái)客戶機(jī)都以100 MB/s的速度連接到服務(wù)器,那么下載速度應(yīng)該不是什么問題。然而,如果服務(wù)器還要為慢騰騰的Modem用戶提供服務(wù),那么值得考慮內(nèi)容壓縮。大多數(shù)瀏覽器都根據(jù)IETF標(biāo)準(zhǔn)支持用gzip進(jìn)行內(nèi)容壓縮。這意味著你可以用gzip壓縮內(nèi)容然而發(fā)送給瀏覽器,由瀏覽器解壓縮數(shù)據(jù)之后再顯示頁面,這整個(gè)過程對(duì)用戶來說完全透明。至于服務(wù)器端的內(nèi)容壓縮,現(xiàn)在已經(jīng)有許多不同的方法可供使用。

例如,來自Remote Communications的免費(fèi)Apache模塊mod_gzip就具有為支持這類內(nèi)容編碼的瀏覽器壓縮靜態(tài)Web內(nèi)容的能力。對(duì)于絕大多數(shù)靜態(tài)Web內(nèi)容,mod_gzip都非常有效。mod_gzip可以方便地編譯到Apache里面,也可以作為DSO使用。據(jù)Remote communications公司說,mod_gzip也能夠壓縮來自mod_php、mod_perl等的動(dòng)態(tài)內(nèi)容。我試了一次又一次,但看來還是不行。我看了許多關(guān)于mod_gzip的論壇和文章,看來到了mod_gzip的下一個(gè)版本(可能是1.3.14.6f)這個(gè)問題有望得到解決。在此之前,我們可以在網(wǎng)站的靜態(tài)部分使用mod_gzip。

然而有時(shí)我們確實(shí)需要壓縮動(dòng)態(tài)內(nèi)容,所以必須找找其他辦法。有一種辦法是使用class.gzip_encode.php,這是一個(gè)可以用來壓縮頁面內(nèi)容的PHP類,具體方法是在PHP腳本的開頭和末尾調(diào)用該類的某些函數(shù)。如果要在網(wǎng)站級(jí)實(shí)現(xiàn)這個(gè)方案,可以從php.ini文件的auto_prepend以及auto_append指令調(diào)用這些函數(shù)。這種方法雖然有效,但它無疑為高負(fù)載的網(wǎng)站帶來了更多的開銷。關(guān)于如何使用這個(gè)類的詳細(xì)說明,請參見它的源代碼。它的源代碼說明相當(dāng)完善,作者告訴了你所有你必須知道的事情。

PHP 4.0.4有一個(gè)新的輸出緩存句柄ob_gzhandler,它與前面的類相似,但用法不同。使用ob_gzhandler時(shí)要在php.ini中加入的內(nèi)容如下:

output_handler = ob_gzhandler ;


這行代碼使得PHP激活輸出緩存,并壓縮它發(fā)送出去的所有內(nèi)容。如果由于某種原因你不想在php.ini中加上這行代碼,你還可以通過PHP源文件所在目錄的.htaccess文件改變默認(rèn)的服務(wù)器行為(不壓縮),語法如下:

php_value output_handler ob_gzhandler


或者是從PHP代碼調(diào)用,如下所示:

ob_start("ob_gzhandler");


采用輸出緩存句柄的方法確實(shí)非常有效,而且不會(huì)給服務(wù)器帶來什么特殊的負(fù)荷。但必須注意的是,Netscape Communicator對(duì)壓縮圖形的支持不佳,因此除非你能夠保證所有用戶都使用IE瀏覽器,否則你應(yīng)該禁止壓縮JPEG和GIF圖形。一般地,對(duì)于所有其他文件,這種壓縮都有效,但建議你針對(duì)各種瀏覽器都分別進(jìn)行測試,特別是當(dāng)你使用了特殊的插件或者數(shù)據(jù)查看器時(shí)這一點(diǎn)尤其重要。



Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles