PHP中Memcache操作類及用法實(shí)例
本文實(shí)例講述了PHP中Memcache操作類及用法。分享給大家供大家參考。具體分析如下:
<?php
/* 內(nèi)存緩存管理
*/
class Yc_Memcache{
private $memcache=null;
public function __construct(){
}
/**
* 連接數(shù)據(jù)庫(kù)
*
* @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 默認(rèn)為0不壓縮 壓縮狀態(tài)填寫:MEMCACHE_COMPRESSED
* @param mixed $expire 默認(rèn)緩存時(shí)間(單位秒)
*/
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;
}
}
/**
* 取出對(duì)應(yīng)的key的value
*
* @param mixed $key
* @param mixed $flags
* $flags 如果此值為1表示經(jīng)過(guò)序列化,
* 但未經(jīng)過(guò)壓縮,2表明壓縮而未序列化,
* 3表明壓縮并且序列化,0表明未經(jīng)過(guò)壓縮和序列化
*/
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;
}
/**
* 刷新緩存但不釋放內(nèi)存空間
*
*/
public function flush(){
memcache_flush($this->memcache);
}
/**
* 關(guān)閉內(nèi)存連接
*
*/
public function close(){
memcache_close($this->memcache);
}
/**
* 替換對(duì)應(yīng)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;
}
/**
* 開(kāi)啟大值自動(dòng)壓縮
*
* @param mixed $threshold 單位b
* @param mixed $min_saveings 默認(rèn)值是0.2表示20%壓縮率
*/
public function setCompressThreshold($threshold,$min_saveings=0.2){
$f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings);
return $f;
}
/**
* 用于獲取一個(gè)服務(wù)器的在線/離線狀態(tài)
*
* @param mixed $host
* @param mixed $port
*/
public function getServerStatus($host,$port=11211){
$re=memcache_get_server_status($this->memcache,$host,$port);
return $re;
}
/**
* 緩存服務(wù)器池中所有服務(wù)器統(tǒng)計(jì)信息
*
* @param mixed $type 期望抓取的統(tǒng)計(jì)信息類型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}
* @param mixed $slabid cachedump命令會(huì)完全占用服務(wù)器通常用于 比較嚴(yán)格的調(diào)
* @param mixed $limit 從服務(wù)端獲取的實(shí)體條數(shù)
*/
public function getExtendedStats($type='',$slabid=0,$limit=100){
$re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit);
return $re;
}
}
/***********測(cè)試區(qū)域********************/
$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 '<pre>';
print_r($mem->getExtendedStats());
}
?>
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)的簡(jiǎn)單sha1加密功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單sha1加密功能,結(jié)合具體實(shí)例形式分析了php sha1加密功能的具體實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2017-08-08通過(guò)實(shí)例解析PHP數(shù)據(jù)類型轉(zhuǎn)換方法
這篇文章主要介紹了通過(guò)實(shí)例解析PHP數(shù)據(jù)類型轉(zhuǎn)換方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07PHP目錄函數(shù)實(shí)現(xiàn)創(chuàng)建、讀取目錄教程實(shí)例
今天主要介紹在PHP網(wǎng)站開(kāi)發(fā)中文件目錄函數(shù)的應(yīng)用。2011-01-01PHP中$GLOBALS[''HTTP_RAW_POST_DATA'']和$_POST的區(qū)別分析
這篇文章主要介紹了PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的區(qū)別,結(jié)合具體實(shí)例形式分析了$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的功能與使用過(guò)程中的區(qū)別,需要的朋友可以參考下2017-07-07PHP+SQL 注入攻擊的技術(shù)實(shí)現(xiàn)以及預(yù)防辦法
最近在折騰 PHP + MYSQL 的編程。了解了一些 PHP SQL 注入攻擊的知識(shí),于是寫了這篇文章.2010-12-12輕松修復(fù)Discuz!數(shù)據(jù)庫(kù)
各位站長(zhǎng)經(jīng)常會(huì)遇到的數(shù)據(jù)庫(kù)損壞的錯(cuò)誤,錯(cuò)誤來(lái)了就去面對(duì),不要慌張,瞎著急是沒(méi)有用的。其實(shí)熟悉Discuz! 的朋友都知道,Discuz! 后臺(tái)自帶數(shù)據(jù)庫(kù)修復(fù)工具的,如果數(shù)據(jù)庫(kù)損壞導(dǎo)致首頁(yè)打不開(kāi)了,但是后臺(tái)還可以進(jìn)去。2008-05-05php下刪除字符串中HTML標(biāo)簽的函數(shù)
一個(gè)可以刪除字符串中html標(biāo)記的php函數(shù)2008-08-08