eAccelerator加速php程序
Jun 08, 2016 pm 05:25 PMeaccelerator加速php教程程序
什么是eaccelerator
概念:
eaccelerator 是一個免費開源的php加速、優(yōu)化、編譯和動態(tài)緩存的項目,它可以
通過緩存php代碼編譯后的結(jié)果來提高php腳本的性能,使得一向很復(fù)雜和離我們很
遠的php腳本編譯問題完全得到解決。通過使用eaccelerator,可以優(yōu)化你的php代
碼執(zhí)行速度,降低服務(wù)器負載,可以提高php應(yīng)用執(zhí)行速度最高達10倍。
原理:
eaccelerator 通過把經(jīng)過編譯后的php代碼緩存到共享內(nèi)存中,并在用戶訪問的時
候直接調(diào)用從而起到高效的加速作用。它的效率非常高,從創(chuàng)建共享內(nèi)存到查找編
譯后的代碼都在非常短的時間內(nèi)完成,對于不能緩存到共享內(nèi)存中的文件和代碼,
eaccelerator還可以把他們緩存到系統(tǒng)磁盤上。
eaccelerator 同樣還支持php代碼的編譯和解釋執(zhí)行,你可以通過encoder.php腳本
來對php代碼進行編譯達到保護代碼的目的,經(jīng)過編譯后的代碼必須運行在安裝了
eaccelerator的環(huán)境下。eaccelerator編譯后的代碼不能被反編譯,它不象其他一
些編譯工具那樣可以進行反編譯,這將使得代碼更加安全和高效。
注意:在共享內(nèi)存里面尋找編譯好的php程序時,會在很短的時間內(nèi)產(chǎn)生一些鎖定,
所以一個程序可以被多個進程同時執(zhí)行。不適合放入共享內(nèi)存的文件將被緩存到硬
盤上
eaccelerator安裝配置
1、支持平臺
由于aaccelerator提供了大部分基于共享內(nèi)存的api,所以在*nix的平臺上將得到更
好的支持,雖然也發(fā)布了基于windows平臺的binary版本,但我在這里就只提供基于
*nix平臺的配置和說明,目前可以支持的平臺包括linux, freebsd, openbsd, mac
os x, solaris, aix en hp-ux。
2、系統(tǒng)要求
php4 or php5
autoconf
automake
libtool
m4
eaccelerator 只支持使用 mod_php 或者 fastcgi mode 安裝的php
3、安裝
先去eaccelerator官方下載最新版的源碼包:
#tar -zxvf ./eaccelerator-0.9.5-beta2.tar.bz2
#cd eaccelerator-0.9.5-beta2
#export php_prefix="/usr/local" (把php安裝目錄導(dǎo)入到環(huán)境變量,freebsd默
認是/usr/local)
#$php_prefix/bin/phpize
#./configure --enable-eaccelerator=shared --with-php-
config=$php_prefix/bin/php-config
#make
#make install
4、ini文件配置
安裝完成,下面開始配置php.ini文件,eaccelerator提供了兩種配置和調(diào)用方式,
分別如下。
安裝為 zend extension 模式:
zend_extension="/usr/local/lib/php/20050922/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.log_file = "/var/log/httpd/eaccelerator_log"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
如果你使用了thread safe模式安裝的php,你必須使用 “zend_extension_ts” 替
換第一行的 “zend_extension”.
安裝為 php extension 模式:(這是大部分采用的方式)
extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.log_file = "/var/log/httpd/eaccelerator_log"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
有關(guān)php.ini文件的詳細配置說明,請參照源碼目錄的readme文檔或者訪問官方文檔
:
完成安裝配置后,我們最后要創(chuàng)建緩存目錄
#mkdir /tmp/eaccelerator
#chmod 777 /tmp/eaccelerator
測試
php代碼中使用eaccelerator加速
下面有一個測試的代碼,你可以測試一下eaccelerator強大的威力:(該代碼在
cli 模式下可能無效)
class test_cache {
? var $pro = 'hello';
?
? function test_cache() {
??? echo "object created!
/n";
? }
? function func() {
??? echo ', the world!';
? }
? function now($t) {
??? echo date('y-m-d h:i:s', $t);
? }
}
?
$tt = eaccelerator_get("test_tt");
if (!$tt)
{
? $tt = new test_cache;
? eaccelerator_put("test_tt", $tt);
? echo "no cached!
/n";
}
else {
? echo "cached
/n";
}
?
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses
