PHP之Opcode緩衝 AND Memcache緩衝使用引導篇
PHP生命週期
請求--->.php--->詞典掃描--->解析--->建立Opcode--->處理Opcode--->響應
即使該PHP指令碼的內容沒有任何變化,Zend引擎也必須重新建立該檔案的Opcode.
Opcode緩衝提高PHP效能
--->有緩衝--->讀取已緩衝的Opcode--->處理Opcode--->響應
請求--->.php
--->無緩衝--->詞典掃描--->解析--->建立Opcode--->處理Opcode--->響應
Opcode緩衝工具
APC
具體可參考PHP之APC緩衝詳細介紹(學習整理)
XCache
具體可參看PHP之XCache緩衝使用
eACCelerator
記憶體緩衝Memcache
簡介
Memcache函數庫是在PECL(PHP Extension Community Library)中,主要作用是搭建大容量的記憶體資料的臨時存放地區
memcache也提供用於通訊對話(session_handler)的處理。
更多Memcache 模組相關資訊可以到 http://www.danga.com/memcached/ 查閱。
安裝Memcache
可參看:http://blog.csdn.net/initphp/article/details/8039917 liunx下的Memcache安裝和使用
Memcache Functions 函數列表
列表:
參考http://www.php.net/manual/zh/function.Memcache-add.php Memcache::add - 添加一個值,如果已經存在,則返回false Memcache::addServer - 添加一個可供使用的伺服器位址 Memcache::close - 關閉一個Memcache對象 Memcache::connect - 建立一個Memcache對象 memcache_debug - 控制調試功能 Memcache::decrement - 對儲存的某個key中的值進行減法操作 Memcache::delete - 刪除一個key值 Memcache::flush - 清除所有緩衝的資料 Memcache::get - 擷取一個key值 Memcache::getExtendedStats - 擷取進程池中所有進程的運行系統統計 Memcache::getServerStatus - 擷取運行伺服器的參數 Memcache::getStats - 返回伺服器的一些運行統計資訊 Memcache::getVersion - 返回啟動並執行Memcache的版本資訊 Memcache::increment - 對儲存的某個key中的值進行加法操作 Memcache::pconnect - 建立一個Memcache的持久連線物件 Memcache::replace -對一個已有的key進行覆寫操作 Memcache::set - 添加一個值,如果已經存在,則覆寫 Memcache::setCompressThreshold - 對大於某一大小的資料進行壓縮 Memcache::setServerParams - 在運行時修改伺服器的參數 |
1、向對象添加一個伺服器(註:addServer沒有串連到伺服器的動作,所以在memcache進程沒有啟動的時候,執行addServer成功也會返回true)
//參數[email protected] string $host 伺服器網域名稱或IP[email protected] int $port 連接埠號碼,預設值11211[email protected] bool $persistent 是否使用常連結,預設TURE[email protected] int $weight 權重,在多個伺服器設定中占的比重[email protected] int $timeout 串連伺服器失效的秒數,修改預設值 1 時要三思,有可能失去所有緩衝方面的優勢導致串連變得很慢[email protected] int $retry_interval 伺服器串連失敗時的重試頻率,預設是 15 秒一次,如果設定為 -1 將禁止自動重試,當擴充中載入了 dynamically via dl() 時,無論本參數還是常串連設定參數都會失效。 每一個失敗的伺服器在失效前都有獨自的生存期,選擇後端請求時會被跳過而不服務於請求。一個到期的串連將成功的重新串連或者被標記為失敗的串連等待下一次 重試。這種效果就是說每一個 web server 的子進程在服務於頁面時的重試串連都跟他們自己的重試頻率有關。[email protected] bool $status 控制伺服器是否被標記為 online,設定這個參數為 FALSE 並設定 retry_interval 為 -1 可以使串連失敗的伺服器被放到一個描述不響應請求的伺服器集區子中,對這個伺服器的請求將失敗,接受設定為失敗伺服器的設定,預設參數為 TRUE,代表該伺服器可以被定義為 online。[email protected] callback $failure_callback 失敗時的回呼函數,函數的兩個參數為失敗伺服器的 hostname 和 portbool Memcache::addServer ( string $host [, int $port [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]]]] )
2、串連memcache伺服器
//參數[email protected] string $host 伺服器網域名稱或ip[email protected] int $post 伺服器tcp連接埠號碼,預設值是11211[email protected] $timeout 串連memcache進程的失效時間,在修改它的預設值1的時候要三思,以免失去所有memcache緩衝的優勢導致串連變得很慢[email protected] boolbool Memcache::connect ( string $host [, int $port [, int $timeout ]] )
案例:
/* procedural API */$memcache_obj = memcache_connect(‘memcache_host‘, 11211);/* OO API */$memcache = new Memcache;$memcache->connect(‘memcache_host‘, 11211);
3、以常串連方式串連伺服器
bool Memcache::pconnect ( string $host [, int $port [, int $timeout ]] )
4、關閉對象 (對常串連不起作用)
bool Memcache::close ( void )
案例:
/* procedural API */$memcache_obj = memcache_connect(‘memcache_host‘, 11211);/* do something here .. */memcache_close($memcache_obj);/* OO API */$memcache_obj = new Memcache;$memcache_obj->connect(‘memcache_host‘, 11211);/* do something here .. */$memcache_obj->close();
5、添加一個值,如果已經存在,則返回false
//參數[email protected] string $key 快取資料的鍵 其長度不能超過250個字元[email protected] mixed $var 值,整型將直接儲存,其他類型將被序列化儲存,其值最大1M[email protected] int $flag 是否使用zlib壓縮,當flag=MEMCACHE_COMPRESSED的時候,資料很小的時候不會採用zlib壓縮,只有資料達到一定大小才對資料進行壓縮[email protected] int $expire 到期時間,0為永久不到期,可使用 unix 時間截格式或距離目前時間的秒數,設為秒數時不能大於2592000(30天)[email protected] 成功返回 TRUE,失敗返回 FALSE,如果這個鍵已經存在,其他方面memcache:;add()的行為與memcache::set相似bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )
案例:
$memcache_obj = memcache_connect("localhost", 11211);memcache_add($memcache_obj, 'var_key', 'test variable', false, 30);$memcache_obj->add('var_key', 'test variable', false, 30);
6、對一個已有的key進行覆寫操作
//參數[email protected] string $key 快取資料的鍵[email protected] mixed $var 值,整型將直接儲存,其他類型將被序列化儲存,其值最大為1M[email protected] int $flag 是否使用 zlib 壓縮 ,當flag=MEMCACHE_COMPRESSED的時侯,資料很小的時候不會採用zlib壓縮,只有資料達到一定大小才對資料進行zlib壓縮。(沒有具體的測試資料進行壓縮的最小值是多少)[email protected] int $expire 到期時間,0 為永不到期,可使用 unix 時間戳記格式或距離目前時間的秒數,設為秒數時不能大於 2592000(30 天)bool Memcache::replace ( string $key , mixed $var [, int $flag [, int $expire ]] )
案例:
$memcache_obj = memcache_connect("localhost", 11211);/* procedural API */memcache_replace($memcache_obj, "test_key", "some variable", FALSE, 30);/* OO API */$memcache_obj->replace("test_key", "some variable", FALSE, 30);
7、添加一個值,如果已經存在,則覆寫
//參數[email protected] string $key 快取資料的鍵, 其長度不能超過250個字元[email protected] mixed $var[email protected] int $flag [email protected] int $expirebool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )
案例:
$memcache_obj = memcache_connect("localhost", 11211);/*set value of item with key ‘var_key‘using 0 as flag value, compression is not usedexpire time is 30 second*/memcache_set($memcache_obj, ‘var_key‘, ‘some variable‘, 0, 30);echo memcache_get($memcache_obj, ‘var_key‘);