使用 eAccelerator加速PHP代碼的方法
更新時(shí)間:2007年09月30日 09:46:48 作者:
eAccelerator 真是一個(gè)好東西(它的前身是truck-mmcache)。
簡(jiǎn)單來講它是一套配合PHP(支持PHP5)運(yùn)作的緩存系統(tǒng),通過共享內(nèi)存或磁盤文件方式交換數(shù)據(jù)。
它被廣為使用的是PHP源碼“編碼”(不太貼切的稱為“加密”)和緩存PHP執(zhí)行的中間碼以加速。關(guān)于 eA 的安裝使用的文章已經(jīng)很多而且也很詳細(xì)了,這次我想推薦的是用它輔助程序設(shè)計(jì)緩存,它提供了一組API如下:
是一個(gè)非常便捷而又穩(wěn)定的本機(jī)緩存實(shí)現(xiàn)方式,目前這部分設(shè)計(jì)似乎只支持于共享內(nèi)存,所以只能用于 Unix -Like OS 了,windows的就沒這個(gè)福氣了。
1. eaccelerator_put($key, $value, $ttl=0)
將 $value 以 $key 為鍵名存進(jìn)緩存(php4下支持對(duì)像類型,看源碼好像zend2里不支持了),$ttl 是這個(gè)緩存的生命周期,單位是秒,省略該參數(shù)或指定為 0 表示不限時(shí),直到服務(wù)器重啟清空為止。
2. eaccelerator_get($key)
根據(jù) $key 從緩存中返回相應(yīng)的 eaccelerator_put() 存進(jìn)去的數(shù)據(jù),如果這項(xiàng)緩存已經(jīng)過期或不存在那么返回值是 NULL
3. eaccelerator_rm($key)
根據(jù) $key 移除緩存
4. eaccelerator_gc()
移除清理所有已過期的 key
5. eaccelerator_lock($key)
為 $key 加上鎖定操作,以保證多進(jìn)程多線程操作時(shí)數(shù)據(jù)的同步。需要調(diào)用 eaccelerator_unlock($key) 來釋放這個(gè)鎖或等待程序請(qǐng)求結(jié)束時(shí)自動(dòng)釋放這個(gè)鎖。
例如:
<?php
eaccelerator_lock("count");
eaccelerator_put("count",eaccelerator_get("count")+1));
?>
6. eaccelerator_unlock($key)
根據(jù) $key 釋放鎖
7. eaccelerator_cache_output($key, $eval_code, $ttl=0)
將 $eval_code 代碼的輸出緩存 $ttl 秒,($ttl參數(shù)同 eacclerator_put)
For Example:
<?php eaccelerator_cache_output('test', 'echo time(); phpinfo();', 30); ?>
8. eaccelerator_cache_result($key, $eval_code, $ttl=0)
將 $eval_code 代碼的執(zhí)行結(jié)果緩存 $ttl 秒,($ttl參數(shù)同 eacclerator_put),類似 cache_output
For Example:
<?php eaccelerator_cache_result('test', ' time() . "Hello";', 30); ?>
9. eaccelerator_cache_page($key, $ttl=0)
將當(dāng)前整頁緩存 $ttl 秒。
For Example:
<?php
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echo time();
phpinfo();
?>
10. eaccelerator_rm_page($key)
刪除由 eaccelerator_cache_page() 執(zhí)行的緩存,參數(shù)也是 $key
______________________________________________
(作個(gè)簡(jiǎn)單例子看看它的威力,注意在 cli 模式下可能無效?。?nbsp;
<?phpclass test_cache { var $pro = 'hello'; function test_cache() { echo "Object Created!<br>/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!<br>/n";}else { echo "cached<br>/n";}echo $tt->pro; $tt->func();$tt->now(time() + 86400);?>
簡(jiǎn)單來講它是一套配合PHP(支持PHP5)運(yùn)作的緩存系統(tǒng),通過共享內(nèi)存或磁盤文件方式交換數(shù)據(jù)。
它被廣為使用的是PHP源碼“編碼”(不太貼切的稱為“加密”)和緩存PHP執(zhí)行的中間碼以加速。關(guān)于 eA 的安裝使用的文章已經(jīng)很多而且也很詳細(xì)了,這次我想推薦的是用它輔助程序設(shè)計(jì)緩存,它提供了一組API如下:
是一個(gè)非常便捷而又穩(wěn)定的本機(jī)緩存實(shí)現(xiàn)方式,目前這部分設(shè)計(jì)似乎只支持于共享內(nèi)存,所以只能用于 Unix -Like OS 了,windows的就沒這個(gè)福氣了。
1. eaccelerator_put($key, $value, $ttl=0)
將 $value 以 $key 為鍵名存進(jìn)緩存(php4下支持對(duì)像類型,看源碼好像zend2里不支持了),$ttl 是這個(gè)緩存的生命周期,單位是秒,省略該參數(shù)或指定為 0 表示不限時(shí),直到服務(wù)器重啟清空為止。
2. eaccelerator_get($key)
根據(jù) $key 從緩存中返回相應(yīng)的 eaccelerator_put() 存進(jìn)去的數(shù)據(jù),如果這項(xiàng)緩存已經(jīng)過期或不存在那么返回值是 NULL
3. eaccelerator_rm($key)
根據(jù) $key 移除緩存
4. eaccelerator_gc()
移除清理所有已過期的 key
5. eaccelerator_lock($key)
為 $key 加上鎖定操作,以保證多進(jìn)程多線程操作時(shí)數(shù)據(jù)的同步。需要調(diào)用 eaccelerator_unlock($key) 來釋放這個(gè)鎖或等待程序請(qǐng)求結(jié)束時(shí)自動(dòng)釋放這個(gè)鎖。
例如:
<?php
eaccelerator_lock("count");
eaccelerator_put("count",eaccelerator_get("count")+1));
?>
6. eaccelerator_unlock($key)
根據(jù) $key 釋放鎖
7. eaccelerator_cache_output($key, $eval_code, $ttl=0)
將 $eval_code 代碼的輸出緩存 $ttl 秒,($ttl參數(shù)同 eacclerator_put)
For Example:
<?php eaccelerator_cache_output('test', 'echo time(); phpinfo();', 30); ?>
8. eaccelerator_cache_result($key, $eval_code, $ttl=0)
將 $eval_code 代碼的執(zhí)行結(jié)果緩存 $ttl 秒,($ttl參數(shù)同 eacclerator_put),類似 cache_output
For Example:
<?php eaccelerator_cache_result('test', ' time() . "Hello";', 30); ?>
9. eaccelerator_cache_page($key, $ttl=0)
將當(dāng)前整頁緩存 $ttl 秒。
For Example:
<?php
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echo time();
phpinfo();
?>
10. eaccelerator_rm_page($key)
刪除由 eaccelerator_cache_page() 執(zhí)行的緩存,參數(shù)也是 $key
______________________________________________
(作個(gè)簡(jiǎn)單例子看看它的威力,注意在 cli 模式下可能無效?。?nbsp;
<?phpclass test_cache { var $pro = 'hello'; function test_cache() { echo "Object Created!<br>/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!<br>/n";}else { echo "cached<br>/n";}echo $tt->pro; $tt->func();$tt->now(time() + 86400);?>
您可能感興趣的文章:
- php 提速工具eAccelerator 配置參數(shù)詳解
- 在Windows下編譯適用于PHP 5.2.12及5.2.13的eAccelerator.dll(附下載)
- PHP加速 eAccelerator配置和使用指南
- 使用eAccelerator加密PHP程序
- 實(shí)現(xiàn)php加速的eAccelerator dll支持文件打包下載
- 用windows下編譯過的eAccelerator for PHP 5.1.6實(shí)現(xiàn)php加速的使用方法
- 使用 eAccelerator加速PHP代碼的目的
- win2003服務(wù)器之用Zend和eAccelerator在IIS6下同時(shí)加速
相關(guān)文章
thinkphp路由規(guī)則使用示例詳解和偽靜態(tài)功能實(shí)現(xiàn)(apache重寫)
這篇文章主要介紹了thinkphp路由規(guī)則使用示例詳解和偽靜態(tài)功能實(shí)現(xiàn)(apache重寫),需要的朋友可以參考下2014-02-02Yii實(shí)現(xiàn)微信公眾號(hào)場(chǎng)景二維碼的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Yii實(shí)現(xiàn)微信公眾號(hào)場(chǎng)景二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08PHP中使用substr()截取字符串出現(xiàn)中文亂碼問題該怎么辦
本文給大家介紹使用php substr()截取字符串出現(xiàn)亂碼問題該怎么辦,涉及到php substr()方法的一些知識(shí)點(diǎn),感興趣的朋友一起學(xué)習(xí)下吧2015-10-10CodeIgniter自定義控制器MY_Controller用法分析
這篇文章主要介紹了CodeIgniter自定義控制器MY_Controller用法,結(jié)合實(shí)例形式分析了CodeIgniter自定義控制器MY_Controller的定義與使用技巧,需要的朋友可以參考下2016-01-01Yii+MYSQL鎖表防止并發(fā)情況下重復(fù)數(shù)據(jù)的方法
這篇文章主要介紹了Yii+MYSQL鎖表防止并發(fā)情況下重復(fù)數(shù)據(jù)的方法,分析了MySQL讀鎖定、寫鎖定及Yii實(shí)現(xiàn)MySQL讀寫鎖定的相關(guān)技巧,需要的朋友可以參考下2016-07-07使用PHP如何實(shí)現(xiàn)高效安全的ftp服務(wù)器(一)
FTP服務(wù)器(File Transfer Protocol Server)是在互聯(lián)網(wǎng)上提供文件存儲(chǔ)和訪問服務(wù)的計(jì)算機(jī),它們依照FTP協(xié)議提供服務(wù),本文給大家介紹使用PHP如何實(shí)現(xiàn)高效安全的ftp服務(wù)器(一),對(duì)php ftp服務(wù)器相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12