eAccelerator 真是一個好東西(它的前身是truck-mmcache)。 它是一套配合PHP(支援PHP5)運作的緩衝系統,通過共用記憶體或磁碟檔案方式交換資料。 它被廣為使用的是PHP源碼“編碼”(不太貼切的稱為“加密”)和緩衝PHP執行的中間碼以加速。關於 eA 的安裝使用的文章已經很多而且也很詳細了,這次我想推薦的是用它輔助程式設計緩衝,它提供了一組API如下: 是一個非常便捷而又穩定的本機緩衝實現方式,目前這部分設計似乎只支援於共用記憶體,所以只能用於 Unix -Like OS 了,windows的就沒這個福氣了。 1. eaccelerator_put($key, $value, $ttl=0) 將 $value 以 $key 為鍵名存進緩衝(php4下支援對像類型,看源碼好像zend2裡不支援了),$ttl 是這個緩衝的生命週期,單位是秒,省略該參數或指定為 0 表示不限時,直到伺服器重啟清空為止。 2. eaccelerator_get($key) 根據 $key 從緩衝中返回相應的 eaccelerator_put() 存進去的資料,如果這項緩衝已經到期或不存在那麼傳回值是 NULL 3. eaccelerator_rm($key) 根據 $key 移除緩衝 4. eaccelerator_gc() 移除清理所有已到期的 key 5. eaccelerator_lock($key) 為 $key 加上鎖定操作,以保證多進程多線程操作時資料的同步。需要調用 eaccelerator_unlock($key) 來釋放這個鎖或等待程式請求結束時自動釋放這個鎖。 例如:
- eaccelerator_lock("count");
- eaccelerator_put("count",eaccelerator_get("count")+1));
- ?>
-
複製代碼6. eaccelerator_unlock($key) 根據 $key 釋放鎖 7. eaccelerator_cache_output($key, $eval_code, $ttl=0) 將 $eval_code 代碼的輸出緩衝 $ttl 秒,($ttl參數同 eacclerator_put) For Example:
-
複製代碼8. eaccelerator_cache_result($key, $eval_code, $ttl=0) 將 $eval_code 代碼的執行結果緩衝 $ttl 秒,($ttl參數同 eacclerator_put),類似 cache_output For Example:
-
複製代碼 9. eaccelerator_cache_page($key, $ttl=0) 將當前整頁緩衝 $ttl 秒。 For Example:
- eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
- echo time();
- phpinfo();
- ?>
-
複製代碼10. eaccelerator_rm_page($key) 刪除由 eaccelerator_cache_page() 執行的緩衝,參數也是 $key 例子,注意在 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);
- ?>
複製代碼 |