PHP中Memcache操作類及用法執行個體,phpmemcache
本文執行個體講述了PHP中Memcache操作類及用法。分享給大家供大家參考。具體分析如下:
複製代碼 代碼如下:
<?php
/* 記憶體緩衝管理
*/
class Yc_Memcache{
private $memcache=null;
public function __construct(){
}
/**
* 串連資料庫
*
* @param mixed $host
* @param mixed $port
* @param mixed $timeout
*/
public function connect($host,$port=11211,$timeout=1){
if(!function_exists(memcache_connect)){ return FALSE;}
$this->memcache=@memcache_connect($host,$port,$timeout);
if(emptyempty($this->memcache)){
return FALSE;
}else{
return TRUE;
}
}
/**
* 存放值
*
* @param mixed $key
* @param mixed $var
* @param mixed $flag 預設為0不壓縮 壓縮狀態填寫:MEMCACHE_COMPRESSED
* @param mixed $expire 預設緩衝時間(單位秒)
*/
public function set($key,$var,$flag=0,$expire=10){
$f=@memcache_set($this->memcache,$key,$var,$flag,$expire);
if(emptyempty($f)){
return FALSE;
}else{
return TRUE;
}
}
/**
* 取出對應的key的value
*
* @param mixed $key
* @param mixed $flags
* $flags 如果此值為1表示經過序列化,
* 但未經過壓縮,2表明壓縮而未序列化,
* 3表明壓縮並且序列化,0表明未經過壓縮和序列化
*/
public function get($key,$flags=0){
$val=@memcache_get($this->memcache,$key,$flags);
return $val;
}
/**
* 刪除緩衝的key
*
* @param mixed $key
* @param mixed $timeout
*/
public function delete($key,$timeout=1){
$flag=@memcache_delete($this->memcache,$key,$timeout);
return $flag;
}
/**
* 重新整理緩衝但不釋放記憶體空間
*
*/
public function flush(){
memcache_flush($this->memcache);
}
/**
* 關閉記憶體串連
*
*/
public function close(){
memcache_close($this->memcache);
}
/**
* 替換對應key的value
*
* @param mixed $key
* @param mixed $var
* @param mixed $flag
* @param mixed $expire
*/
public function replace($key,$var,$flag=0,$expire=1){
$f=memcache_replace($this->memcache,$key,$var,$flag,$expire);
return $f;
}
/**
* 開啟大值自動壓縮
*
* @param mixed $threshold 單位b
* @param mixed $min_saveings 預設值是0.2表示20%壓縮率
*/
public function setCompressThreshold($threshold,$min_saveings=0.2){
$f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings);
return $f;
}
/**
* 用於擷取一個伺服器的線上/離線狀態
*
* @param mixed $host
* @param mixed $port
*/
public function getServerStatus($host,$port=11211){
$re=memcache_get_server_status($this->memcache,$host,$port);
return $re;
}
/**
* 快取服務器池中所有伺服器統計資訊
*
* @param mixed $type 期望抓取的統計資訊類型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}
* @param mixed $slabid cachedump命令會完全佔用伺服器通常用於 比較嚴格的調
* @param mixed $limit 從服務端擷取的實體條數
*/
public function getExtendedStats($type='',$slabid=0,$limit=100){
$re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit);
return $re;
}
}
/***********測試地區********************/
$mem=new Yc_Memcache();
$f=$mem->connect('125.64.41.138',12000);
var_dump($f);
if($f){
// $mem->setCompressThreshold(2000,0.2);
$mem->set('key','hello',0,30);
// var_dump($mem->delete('key1'));
// $mem->flush();
// var_dump($mem->replace('hao','d'));
// echo $mem->get('key');
echo $mem->getServerStatus('127.0.0.1',12000);
echo $mem->get('key');
echo '
';
print_r($mem->getExtendedStats());
}
?>
希望本文所述對大家的PHP程式設計有所協助。
http://www.bkjia.com/PHPjc/926868.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/926868.htmlTechArticlePHP中Memcache操作類及用法執行個體,phpmemcache 本文執行個體講述了PHP中Memcache操作類及用法。分享給大家供大家參考。具體分析如下: 複製代碼 代碼...