1. 程式人生 > >APC(Alternative PHP Cache)

APC(Alternative PHP Cache)

APC是一種php的快取解決方案,目前以pecl方式釋出,有訊息說將會出現在php6版本的核心. 一.安裝方法
1)從http://pecl.php.net/package/apc下載相應版本
2)解壓
3)進入原始碼目錄
4)執行php安裝目錄下的bin/phpize
5)./configure --enable-apc --enable-apc-mmap --with-apxs=path-to-apache/bin/apxs --with-php-config=path-to-php/bin/php-config
6)make && make install
7)將生成的apc.so載入到php.ini(extesion=apc.so,注意extension_dir的設定) 一般地,編譯生成的.so會在php安裝路徑的lib/php/extensions下 8)重啟,apache 寫一個phpinfo看看
注:windows下,只要到http://snaps.php.net/的相應分支下下載php_apc.dll,再在php.ini中載入即可 二.用法 apc的用法比較簡單,只有幾個函式,列舉如下 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
//apc test
//constants
$constants = array('APC_FILE'   => 'apc.php', 'AUTHOR'   => 'tim'
);
apc_define_constants('numbers', $constants
);
apc_load_constants('numbers'
);
echo
'APC_FILE='.APC_FILE
.'<br>'
;
echo
'AUTHOR='.AUTHOR.'<br>'
;

//variable
if(!apc_fetch('time1')) apc_store('time1', time
());
if(!
apc_fetch('time2')) apc_store('time2', time(),2);
//set ttl
echo 'time1:'.apc_fetch('time1').'<br>'
;
echo
'time2:'.apc_fetch('time2').'<br>'
;

//object
class a
{
     function
b(){return 'i am b in class a'
;}
}
apc_store('obj',new a
());
$a = apc_fetch('obj'
);
echo
$a->b
();
echo
'<br>'
;
//array
$arr = array('a'=>'i am a','b'=>'i am b'
);
apc_store('arr',$arr
);
$apc_arr = apc_fetch('arr'
);
print_r($apc_arr
); ?>
提示:你可以重新整理一下,看ttl設定是否生效