探討PHP使用eAccelerator的API開發詳解_php技巧

來源:互聯網
上載者:User
1、API和文檔說明:
eAccelerator提供了便捷便捷而又穩定的本機緩衝實現方式,由於大部分代碼實現基於共用記憶體,所以只能在*nix平台中使用,Windows平台Michael就暫時不知道何時有這方面的支援了。
eAccelerator提供如下的API介面和檔案:(下述檔案均在源碼包的doc/php/目錄下)
檔案清單:
複製代碼 代碼如下:

    cache.php
    dasm.php
    encoder.php
    info.php
    loader.php
    session.php
    shared_memory.php

介面列表:
複製代碼 代碼如下:

    array eaccelerator_cached_scripts ()
    void eaccelerator_cache_output (string $key, string $eval_code, [int $ttl = 0])
    void eaccelerator_cache_page (string $key, [int $ttl = 0])
    void eaccelerator_cache_result (string $key, string $code, [int $ttl = 0])
    void eaccelerator_caching (boolean $flag)
    void eaccelerator_clean ()
    void eaccelerator_clear ()
    array eaccelerator_dasm_file (mixed $filename)
    mixed eaccelerator_encode (mixed $src, [mixed $prefix = ''], [string $pre_content = ''], [string $post_content = ''])
    void eaccelerator_gc ()
    mixed eaccelerator_get (string $key)
    array eaccelerator_info ()
    array eaccelerator_list_keys ()
    void eaccelerator_load ()
    boolean eaccelerator_lock (string $key)
    void eaccelerator_optimizer (boolean $flag)
    void eaccelerator_purge ()
    boolean eaccelerator_put (string $key, mixed $value, [int $ttl = 0])
    array eaccelerator_removed_scripts ()
    boolean eaccelerator_rm (string $key)
    void eaccelerator_rm_page (string $key)
    boolean eaccelerator_set_session_handlers ()
    boolean eaccelerator_unlock (string $key)

下面有部分網友翻譯後的介面說明:
複製代碼 代碼如下:

eaccelerator_put($key, $value, $ttl=0)
  將 $value 以 $key 為鍵名存進緩衝(php4下支援對像類型,看源碼好像zend2裡不支援了),$ttl 是這個緩衝的生命週期,單位是秒,省略該參數或指定為 0 表示不限時,直到伺服器重啟清空為止。

eaccelerator_get($key)
  根據 $key 從緩衝中返回相應的 eaccelerator_put() 存進去的資料,如果這項緩衝已經到期或不存在那麼傳回值是 NULL

eaccelerator_rm($key)
  根據 $key 移除緩衝

eaccelerator_gc()
  移除清理所有已到期的 key

eaccelerator_lock($key)
  為 $key 加上鎖定操作,以保證多進程多線程操作時資料的同步。需要調用 eaccelerator_unlock($key) 來釋放這個鎖或等待程式請求結束時自動釋放這個鎖。
  例如:
  <?php
    eaccelerator_lock(“count”);
    eaccelerator_put(“count”,eaccelerator_get(“count”)+1));
  ?>

eaccelerator_unlock($key)
  根據 $key 釋放鎖

eaccelerator_cache_output($key, $eval_code, $ttl=0)
  將 $eval_code 代碼的輸出緩衝 $ttl 秒,($ttl參數同 eacclerator_put)
  例如:
  <?php eaccelerator_cache_output(‘test', ‘echo time(); phpinfo();', 30); ?>

eaccelerator_cache_result($key, $eval_code, $ttl=0)
  將 $eval_code 代碼的執行結果緩衝 $ttl 秒,($ttl參數同 eacclerator_put),類似 cache_output
  例如:
  <?php eaccelerator_cache_result(‘test', ‘ time() . “Hello”;', 30); ?>

eaccelerator_cache_page($key, $ttl=0)
  將當前整頁緩衝 $ttl 秒。
  例如:
  <?php
    eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
    echo time();
    phpinfo();
  ?>

eaccelerator_rm_page($key)
  刪除由  eaccelerator_cache_page() 執行的緩衝,參數也是 $key

2、PHP代碼中使用eAccelerator加速
另外,在PHPCMS裡面已經整合了對eAccelerator的支援,下面是一段來自PHPCMS裡面的代碼
複製代碼 代碼如下:

class cache
{
    function __construct()
    {
    }

    function cache()
    {
        $this->__construct();
    }

    function get($name)
    {
        return eaccelerator_get($name);
    }

    function set($name, $value, $ttl = 0)
    {
        eaccelerator_lock($name);
        return eaccelerator_put($name, $value, $ttl);
    }

    function rm($name)
    {
        return eaccelerator_rm($name);
    }

    function clear()
    {
        return eaccelerator_gc();
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.