天天看點

php+apc 安裝與優化

php 代碼緩存APC 

    PHP語言是一個功能強大的嵌入式HTML腳本語言,很多程式員都在使用這一語言來進行網站的建立。PHP APC,全稱是Alternative PHP Cache,官方翻譯叫”可選PHP緩存”,但我個人覺得應該叫”另一個PHP緩存”。

  PHP APC提供兩種緩存功能,即緩存Opcode(目标檔案),我們稱之為apc_compiler_cache。同時它還提供一些接口用于PHP開發人員将使用者資料駐留在記憶體中,我們稱之為apc_user_cache。

PHP APC的安裝:

  下載下傳php_apc.dll

打開php.ini.搜尋;extension=php_zip.dll 在這一行下面加上

extension=php_apc.dll

apc.enabled = 1

apc.shm_segments = 1

apc.shm_size = 64

apc.optimization = 0

apc.num_files_hint = 1000

apc.ttl = 0

apc.gc_ttl = 3600

apc.cache_by_default = On

apc.slam_defense = 0

apc.file_update_protection = 2

apc.enable_cli = 0

apc.stat=0

apc.rfc1867 = on

儲存php.ini,重新啟動你的IIS或者Apache.就安裝成功了..

打開phpinfo.php 如果看到 apc那麼就安裝成功了.

PHP APC的使用:

  PHP APC的使用:

APC的使用其實倒說不上.APC是個優化器,自安裝之日起,就默默地在背景為您的PHP應用服務了.您的所有PHP代碼會被緩存起來. 另外,APC可提供一定的記憶體緩存功能.但是這個功能并不是十分完美,有報告說如果頻繁使用APC緩存的寫入功能,會導緻不可預料的錯誤.如果想使用這個功能,可以看看apc_fetch,apc_store等幾個與apc緩存相關的函數. APC引入了一個小甜餅,解決了困擾大家已久的大檔案上傳的進度條問題.

 PHP APC的進階使用

1.緩存期限: APC的緩存分兩部分:系統緩存和使用者資料緩存.

系統緩存 是自動使用的,是指APC把PHP檔案源碼的編譯結果緩存起來,然後在再次調用時先對比時間标記。如果未過期,則使用緩存代碼運作。預設緩存 3600s(一小時).但是這樣仍會浪費大量CPU時間.是以可以在php.ini中設定system緩存為永不過期(apc.ttl=0).不過如果這樣設定,運作php代碼後需要restart一下您的web伺服器(比如apache…).目前對APC的性能測試一般指的是這一層cache;

使用者資料緩存 由使用者在編寫php代碼時用apc_store和apc_fetch函數操作讀取、寫入的.如果量不大的話我建議可以使用一下.如果量大,我建議使用memcache會更好.

2.狀态控制和分析: PHP APC的源碼包自帶了一個apc.php;您可以将這個檔案上傳到web伺服器的某個目錄下,用浏覽器通路,這會顯示目前的狀态.我們可以從這裡的表格分析目前的緩存狀況,作出進一步優化. apc-info-clublocalhost2.png 這是某test站點的狀态.您可以慢慢分析,這個工具會提供很多有用的工具.比如您可以看到哪些檔案經常被包含(通路),您緩存的哪個變量經常被讀取,或經常被更新等. 最後順便提一句,有獨立報告說,PHP APC的代碼緩存、優化效果要高出zend優化器.

注意:在不久的将來, 此加速器可能變得更好,據說PHP6.X版本中要整合此加速器.

pc的用法比較簡單,隻有幾個函數,列舉如下

apc_clear_cache() 清除apc緩存内容預設(無參數)時,隻清除系統緩存,要清除使用者緩存,需用’user’參數

apc_define_constants ( string key, array constants [, bool case_sensitive] )将數組constants以常量加入緩存

apc_load_constants (string Key)取出常量緩存

apc_store ( string key, mixed var [, int ttl] )在緩存中儲存資料

apc_fetch ( string key )獲得apc_store儲存的緩存内容

apc_delete ( string key )删除apc_store儲存的内容

以常量加入緩存 并取出緩存

<?php

  $constants = array('APC_FILE' => 'apc.php', 'AUTHOR' => 'tim');

  apc_define_constants('numbers', $constants);

  apc_load_constants('numbers');

  echo 'APC_FILE='.APC_FILE;

  echo 'AUTHOR='.AUTHOR;

?>

添加和取出緩存

<?php

if(!apc_fetch('time1')){  //判斷是否存儲緩存  如果fei 的話那麼就添加緩存

    apc_store('time1', time());

}

if(!apc_fetch('time2')) {

    apc_store('time2', time(),2); //添加過期時間

}

//取出

echo 'time1:'.apc_fetch('time1');

echo 'time2:'.apc_fetch('time2');

?>

緩存對象并取出,取出後可以通路對象的屬性和方法

<?php

class a{

function b(){

    return '你通路了a類的b方法';

    }

}

header('content-type: text/html;charset=utf-8');

apc_store('obj',new a());

$a = apc_fetch('obj');

echo $a->b();

?>

數組緩存

<?php

$arr = array('a'=>'i am a','b'=>'i am b');

apc_store('arr',$arr);

$apc_arr = apc_fetch('arr');

print_r($apc_arr);

?>

提示:如果設定了緩存時間 那麼可以重新整理一下,看ttl設定是否生效

     在 PHP Cache 層,Facebook 采用了 APC。

     與PHP中其他的機制不同,使用 apc_store() 存儲的變量 在不同的請求之間一直持久存在(直到從緩存系統中移除)。

其他常用apc 函數

apc_exists — 檢查APC中是否存在某個或者某些key

<?php 

$fruit  = 'apple';

apc_store('foo', $fruit);

if (apc_exists('foo')) {

    echo "Foo exists: ";

    echo apc_fetch('foo');

} else {

    echo "Foo does not exist";

}

?>

apc_clear_cache();  自動清除系統緩存

apc_clear_cache('user');  自動使用者緩存

class apcInit { 

    public function set_cache($key, $value, $time = 0) {  

        if ($time == 0) $time = null; //null情況下永久緩存 

        return apc_store($key, $value, $time);; 

    } 

    public function get_cache($key) { 

        return apc_fetch($key); 

    } 

    public function clear($key) { 

        return apc_delete($key); 

    } 

    public function clear_all() { 

        apc_clear_cache('user'); //清除使用者緩存 

        return apc_clear_cache(); //清楚緩存 

    } 

    public function exists($key) { 

        return apc_exists($key); 

    } 

    public function inc($key, $step) { 

        return apc_inc($key, (int) $step); 

    } 

    public function dec($key, $step) { 

        return apc_dec($key, (int) $step); 

    } 

    public function info() { 

        return apc_cache_info(); 

    }