eAccelerator加速PHP代碼方法解析

來源:互聯網
上載者:User

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) 來釋放這個鎖或等待程式請求結束時自動釋放這個鎖。 例如:

  1. eaccelerator_lock("count");
  2. eaccelerator_put("count",eaccelerator_get("count")+1));
  3. ?>
複製代碼

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:

  1. eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
  2. echo time();
  3. phpinfo();
  4. ?>
複製代碼

10. eaccelerator_rm_page($key) 刪除由 eaccelerator_cache_page() 執行的緩衝,參數也是 $key 例子,注意在 cli 模式下可能無效。

  1. class test_cache {
  2. var $pro = 'hello';
  3. function test_cache() {
  4. echo "Object Created!
    \n";
  5. }
  6. function func() {
  7. echo ', the world!';
  8. }
  9. function now($t) {
  10. echo date('Y-m-d H:i:s', $t);
  11. }
  12. }
  13. $tt = eaccelerator_get("test_tt");
  14. if (!$tt)
  15. {
  16. $tt = new test_cache;
  17. eaccelerator_put("test_tt", $tt);
  18. echo "no cached!
    \n";
  19. }
  20. else {
  21. echo "cached
    \n";
  22. }
  23. echo $tt->pro;
  24. $tt->func();
  25. $tt->now(time() + 86400);
  26. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.