php操作memcache的使用【轉(zhuǎn)】,php操作memcache
Jun 13, 2016 am 08:42 AMphp操作memcache的使用【轉(zhuǎn)】,php操作memcache
1.簡(jiǎn)介
Memcache是danga.com的一個(gè)項(xiàng)目,最早是為 LiveJournal 服務(wù)的,目前全世界不少人使用這個(gè)緩存項(xiàng)目來構(gòu)建自己大負(fù)載的網(wǎng)站,來分擔(dān)數(shù)據(jù)庫(kù)的壓力。它可以應(yīng)對(duì)任意多個(gè)連接,使用非阻塞的網(wǎng)絡(luò)IO。由于它的工作機(jī) 制是在內(nèi)存中開辟一塊空間,然后建立一個(gè)HashTable,Memcached自管理這些HashTable。Memcache官方網(wǎng) 站:http://www.danga.com/memcached,更多詳細(xì)的信息可以來這里了解。
為什么會(huì)有Memcache和memcached兩種名稱?其實(shí)Memcache是這個(gè)項(xiàng)目的名稱,而memcached是它服務(wù)器端的主程序文件名,知道我的意思了把~~~~。一個(gè)是項(xiàng)目名稱,一個(gè)是主程序文件名。
Memcache的安裝
分為兩個(gè)過程:memcache服務(wù)器端的安裝和memcached客戶端的安裝。
所謂服務(wù)器端的安裝就是在服務(wù)器(一般都是linux系統(tǒng))上安裝Memcache實(shí)現(xiàn)數(shù)據(jù)的存儲(chǔ)。
所謂客戶端的安裝就是指php(或者其他程序,Memcache還有其他不錯(cuò)的api接口提供)去使用服務(wù)器端的Memcache提供的函數(shù),需要php添加擴(kuò)展。
?
memcache模塊是一個(gè)高效的守護(hù)進(jìn)程,提供用于內(nèi)存緩存的過程式程序和面向?qū)ο蟮姆奖愕慕涌?,特別是對(duì)于設(shè)計(jì)動(dòng)態(tài)web程序時(shí)減少對(duì)數(shù)據(jù)庫(kù)的訪問。
memcache也提供用于通信對(duì)話(session_handler)的處理。
更多Memcache 模塊相關(guān)信息可以到?http://www.danga.com/memcached/?查閱。
?1.1.memcache在php.ini中的配置項(xiàng)列表
名稱 | 默認(rèn)值 | 是否可變 | 改變?nèi)罩?/strong> |
memcache.allow_failover | “1” | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.max_failover_attempts | "20" | PHP_INI_ALL | Available since memcache 2.1.0. |
memcache.chunk_size | "8192" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.default_port | "11211" | PHP_INI_ALL | Available since memcache 2.0.2. |
memcache.hash_strategy | "standard" | PHP_INI_ALL | Available since memcache 2.2.0. |
memcache.hash_function | "crc32" | PHP_INI_ALL | Available since memcache 2.2.0. |
session.save_handler | "files" | PHP_INI_ALL | Supported since memcache 2.1.2 |
session.save_path | "" | PHP_INI_ALL | Supported since memcache 2.1.2 |
有關(guān) PHP_INI_* 常量進(jìn)一步的細(xì)節(jié)與定義參見PHP手冊(cè)php.ini 配置選項(xiàng)。
1.2.以下是配置項(xiàng)的簡(jiǎn)要解釋
memcache.allow_failover?Boolean
在錯(cuò)誤時(shí)是否透明的故障轉(zhuǎn)移到其他服務(wù)器上處理(注:故障轉(zhuǎn)移是動(dòng)詞)。
memcache.max_failover_attempts?integer
定義服務(wù)器的數(shù)量類設(shè)置和獲取數(shù)據(jù),只聯(lián)合 memcache.allow_failover 一同使用。
memcache.chunk_size?integer
數(shù)據(jù)將會(huì)被分成指定大?。╟hunk_size)的塊來傳輸,這個(gè)值(chunk_size)越小,寫操作的請(qǐng)求就越多,如果發(fā)現(xiàn)其他的無法解釋的減速,請(qǐng)?jiān)囍鴮⑦@個(gè)值增大到32768.
memcache.default_port?string
當(dāng)連接memcache服務(wù)器的時(shí)候,如果沒有指定端口這個(gè)默認(rèn)的tcp端口將被用。
memcache.hash_strategy?string
控制在映射 key 到服務(wù)器時(shí)使用哪種策略。設(shè)置這個(gè)值一致能使hash 算法始終如一的使用于服務(wù)器接受添加或者刪除池中變量時(shí)將不會(huì)被重新映射。設(shè)置這個(gè)值以標(biāo)準(zhǔn)的結(jié)果在舊的策略被使用時(shí)。
memcache.hash_function?string
控制哪種 hsah 函數(shù)被應(yīng)用于 key映射 到服務(wù)器過程中,默認(rèn)值“crc32”使用 CRC32 算法,而“fnv”則表示使用 FNV-1a 算法。
session.save_handler?string
通過設(shè)置這個(gè)值為memcache來確定使用 memcache 用于通信對(duì)話的處理(session handler)。
session.save_path?string
定義用于通話存儲(chǔ)的各服務(wù)器鏈接的分隔符號(hào),例如:“tcp://host1:11211, tcp://host2:11211”。
每服務(wù)器個(gè)鏈接可以包含被接受于該服務(wù)器的參數(shù),比較類似使用 Memcache::addServer() 來添加的服務(wù)器,例如:“tcp://host1:11211?persistent=1&weight=1&timeout=1& amp; amp;retry_interval=15”。
?1.3.memcache常量列表
名稱 | 類型 | 描述 |
MEMCACHE_COMPRESSED | integer | 用于調(diào)整在使用 Memcache::set(), Memcache::add() 和 Memcache::replace() 幾個(gè)函數(shù)時(shí)的壓縮比率。 |
MEMCACHE_HAVE_SESSION | integer | 如果通信對(duì)話的處理(session handler)被允許使用其值為 1,其他情況值為 0。 |
2Memcache Functions 函數(shù)列表
2.1.Memcache::connect
2.1.1.說明
bool?Memcache::connect?(?string?$host?[,?int?$port?[,?int?$timeout?]]?)連接memcache服務(wù)器
2.1.2.參數(shù)
$host(string)? 服務(wù)器域名或ip
$port(int)? 服務(wù)器tcp端口號(hào),默認(rèn)值是11211
$timeout? 連接memcache進(jìn)程的失效時(shí)間,在修改它的默認(rèn)值1的時(shí)候要三思,以免失去所有memcache緩存的優(yōu)勢(shì)導(dǎo)致連接變得很慢。
2.1.3.返回值
如果成功則返回true,失敗則返回false
2.1.4.范例
/*?procedural?API?*/$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
/*?OO?API?*/
$memcache?=?new?Memcache;
$memcache->connect(‘memcache_host‘,?11211);
?>
2.2.Memcache::pconnect
2.2.1.說明
bool?Memcache::pconnect?(?string?$host?[,?int?$port?[,?int?$timeout?]]?)以常連接方式連接服務(wù)器
2.2.2.參數(shù)
$host(string)? 服務(wù)器域名或ip
$port(int)? 服務(wù)器tcp端口號(hào),默認(rèn)值是11211
$timeout? 連接memcache進(jìn)程的失效時(shí)間,在修改它的默認(rèn)值1的時(shí)候要三思,以免失去所有memcache緩存的優(yōu)勢(shì)導(dǎo)致連接變得很慢。
2.2.3.返回值
如果成功則返回true,失敗則返回false
2.2.4.范例
/*?procedural?API?*/$memcache_obj?=?memcache_pconnect(‘memcache_host‘,?11211);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->pconnect(‘memcache_host‘,?11211);
?>
2.3.Memcache::close
2.3.1.說明
bool?Memcache::close?(?void?)關(guān)閉對(duì)象 (對(duì)常連接不起作用)
2.3.2.返回值
如果成功則返回true,失敗則返回false
2.3.3.范例
/*?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();
?>
2.4.Memcache::addServer
2.4.1.說明
bool?Memcache::addServer?(?string?$host?[,?int?$port?[,?bool?$persistent?[,?int?$weight?[,?int?$timeout?[,?int?$retry_interval?[,?bool?$status?[,?callback?$failure_callback?]]]]]]]?)向?qū)ο筇砑右粋€(gè)服務(wù)器(注:addServer沒有連接到服務(wù)器的動(dòng)作,所以在memcache進(jìn)程沒有啟動(dòng)的時(shí)候,執(zhí)行addServer成功也會(huì)返回true)
2.4.2.參數(shù)
host?????????????? 服務(wù)器域名或 IP
port?????????????? 端口號(hào),默認(rèn)為 11211
persistent???????? 是否使用常連接,默認(rèn)為 TRUE
weight???????????? 權(quán)重,在多個(gè)服務(wù)器設(shè)置中占的比重
timeout????????? 連接服務(wù)器失效的秒數(shù),修改默認(rèn)值 1 時(shí)要三思,有可能失去所有緩存方面的優(yōu)勢(shì)導(dǎo)致連接變得很慢
retry_interval??? 服務(wù)器連接失敗時(shí)的重試頻率,默認(rèn)是 15 秒一次,如果設(shè)置為 -1 將禁止自動(dòng)重試,當(dāng)擴(kuò)展中加載了 dynamically via dl() 時(shí),無論本參數(shù)還是常連接設(shè)置參數(shù)都會(huì)失效。
????????????????????????? 每一個(gè)失敗的服務(wù)器在失效前都有獨(dú)自的生存期,選擇后端請(qǐng)求時(shí)會(huì)被跳過而不服務(wù)于請(qǐng)求。一個(gè)過期的連接將成功的重新連接或者被標(biāo)記為失敗的連接等待下一次 重試。這種效果就是說每一個(gè) web server 的子進(jìn)程在服務(wù)于頁(yè)面時(shí)的重試連接都跟他們自己的重試頻率有關(guān)。
status???????????? 控制服務(wù)器是否被標(biāo)記為 online,設(shè)置這個(gè)參數(shù)為 FALSE 并設(shè)置 retry_interval 為 -1 可以使連接失敗的服務(wù)器被放到一個(gè)描述不響應(yīng)請(qǐng)求的服務(wù)器池子中,對(duì)這個(gè)服務(wù)器的請(qǐng)求將失敗,接受設(shè)置為失敗服務(wù)器的設(shè)置,默認(rèn)參數(shù)為 TRUE,代表該服務(wù)器可以被定義為 online。
failure_callback?? 失敗時(shí)的回調(diào)函數(shù),函數(shù)的兩個(gè)參數(shù)為失敗服務(wù)器的 hostname 和 port
2.4.3.返回值
成功返回 TRUE,失敗返回 FALSE。
注:在測(cè)試addServer函數(shù)的時(shí)候我們主要測(cè)試了其參數(shù)retry_interval和status
2.4.4.范例
2.4.4.1.retry_interval參數(shù)的測(cè)試
$mem?=?new?Memcache;$is_add?=?$mem->addServer(‘localhost‘,?11211,?true,?1,?1,?15,?true);?//?retrt_interval=15
$is_set?=?$mem->set(‘key1‘,?‘中華人民共和國(guó)‘);
?>
上面的例子中如果localhost服務(wù)器down掉或是memcache守護(hù)進(jìn)程當(dāng)?shù)?,?zhí)行請(qǐng)求的時(shí)候連接服務(wù)器失敗時(shí)算起15秒后會(huì)自動(dòng)重試連 接服務(wù)器,但是在這15秒內(nèi)不會(huì)去連接這個(gè)服務(wù)器,就是只要有請(qǐng)求,沒15秒就會(huì)嘗試連接服務(wù)器,但是每個(gè)服務(wù)器連接重試是獨(dú)立的。比如說我一次添加了兩 個(gè)服務(wù)器一個(gè)是localhost,一個(gè)是172.16.100.60,它們分別是從各自連接失敗那個(gè)時(shí)間算起,只要對(duì)各自服務(wù)器有請(qǐng)求就會(huì)每隔15秒去 連接各自的服務(wù)器的。
2.4.4.2.retry_interval和status結(jié)合使用的情況
$mem?=?new?Memcache;$is_add?=?$mem->addServer(‘localhost‘,?11211,?true,?1,?1,?-1,?false);?//?retrt_interval=-1,?status=false
$is_set?=?$mem->set(‘key1‘,?‘中華人民共和國(guó)‘);
?>
在上面的retrt_interval=-1, status=false這種情況下,將連接失敗的服務(wù)器放到一個(gè)不響應(yīng)請(qǐng)求的一個(gè)池子中,因此對(duì)key分配的算法也就沒有影響了,而他是立即返回錯(cuò)誤失 敗還是故障轉(zhuǎn)移還要看memcache.allow_failover的設(shè)置,執(zhí)行set, add, replace,get等請(qǐng)求的時(shí)候都會(huì)失敗返回false,即使memcache進(jìn)程運(yùn)行正常。
2.4.4.3.status參數(shù)的測(cè)試
除了與retry_interval結(jié)合使用,status單獨(dú)使用的情況會(huì)對(duì)函數(shù)memcache::getServerStatu獲得的結(jié)果產(chǎn)生影響
無論memcache進(jìn)程的正常運(yùn)行還是當(dāng)?shù)?,status為true的時(shí)候getServerStatus的結(jié)果都是true,反之則為false
但是在memcache進(jìn)程正常運(yùn)行的情況下,對(duì)set,add,replace,get等函數(shù)都沒有影響。
2.5.Memcache::add
2.5.1.說明
bool?Memcache::add?(?string?$key?,?mixed?$var?[,?int?$flag?[,?int?$expire?]]?)?
添加一個(gè)要緩存的數(shù)據(jù)如果作為這個(gè)緩存的數(shù)據(jù)的鍵在服務(wù)器上還不存在的情況下,
2.5.2.參數(shù)
key??????????????? 緩存數(shù)據(jù)的鍵 其長(zhǎng)度不能超過250個(gè)字符
var??????????????? 值,整型將直接存儲(chǔ),其他類型將被序列化存儲(chǔ) ,其值最大為1M
flag?????????????? 是否使用 zlib 壓縮 ,當(dāng)flag=MEMCACHE_COMPRESSED的時(shí)侯,數(shù)據(jù)很小的時(shí)候不會(huì)采用zlib壓縮,只有數(shù)據(jù)達(dá)到一定大小才對(duì)數(shù)據(jù)進(jìn)行zlib壓縮。(沒有具體的測(cè)試數(shù)據(jù)進(jìn)行壓縮的最小值是多少)
expire???????????? 過期時(shí)間,0 為永不過期,可使用 unix 時(shí)間戳格式或距離當(dāng)前時(shí)間的秒數(shù),設(shè)為秒數(shù)時(shí)不能大于 2592000(30 天)
2.5.3.返回值
成功返回 TRUE,失敗返回 FALSE,如果這個(gè)鍵已經(jīng)存在,其他方面memcache:;add()的行為與memcache::set相似
2.5.4.范例
$memcache_obj?=?memcache_connect("localhost",?11211);/*?procedural?API?*/
memcache_add($memcache_obj,?‘var_key‘,?‘test?variable‘,?FALSE,?30);
/*?OO?API?*/
$memcache_obj->add(‘var_key‘,?‘test?variable‘,?FALSE,?30);
?>
2.6.Memcache::replace
2.6.1.說明
bool?Memcache::replace?(?string?$key?,?mixed?$var?[,?int?$flag?[,?int?$expire?]]?)替換一個(gè)指定 已存在key 的的緩存變量?jī)?nèi)容
2.6.2.參數(shù)
key??????????????? 緩存數(shù)據(jù)的鍵, 其長(zhǎng)度不能超過250個(gè)字符
var??????????????? 值,整型將直接存儲(chǔ),其他類型將被序列化存儲(chǔ),其值最大為1M
flag?????????????? 是否使用 zlib 壓縮 ,當(dāng)flag=MEMCACHE_COMPRESSED的時(shí)侯,數(shù)據(jù)很小的時(shí)候不會(huì)采用zlib壓縮,只有數(shù)據(jù)達(dá)到一定大小才對(duì)數(shù)據(jù)進(jìn)行zlib壓縮。(沒有具體的測(cè)試數(shù)據(jù)進(jìn)行壓縮的最小值是多少)
expire???????????? 過期時(shí)間,0 為永不過期,可使用 unix 時(shí)間戳格式或距離當(dāng)前時(shí)間的秒數(shù),設(shè)為秒數(shù)時(shí)不能大于 2592000(30 天)
2.6.3.返回值
成功返回 TRUE,失敗返回 FALSE。
2.6.4.范例
$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);/*?procedural?API?*/
memcache_replace($memcache_obj,?"test_key",?"some?variable",?FALSE,?30);
/*?OO?API?*/
$memcache_obj->replace("test_key",?"some?variable",?FALSE,?30);
?>
2.7.Memcache::set
2.7.1.說明
bool?Memcache::set?(?string?$key?,?mixed?$var?[,?int?$flag?[,?int?$expire?]]?)
設(shè)置一個(gè)指定 key 的緩存變量?jī)?nèi)容
2.7.2.參數(shù)
key??????????????? 緩存數(shù)據(jù)的鍵, 其長(zhǎng)度不能超過250個(gè)字符
var??????????????? 值,整型將直接存儲(chǔ),其他類型將被序列化存儲(chǔ),其值最大為1M
flag?????????????? 是否使用 zlib 壓縮 ,當(dāng)flag=MEMCACHE_COMPRESSED的時(shí)侯,數(shù)據(jù)很小的時(shí)候不會(huì)采用zlib壓縮,只有數(shù)據(jù)達(dá)到一定大小才對(duì)數(shù)據(jù)進(jìn)行zlib壓縮。(沒有具體的測(cè)試數(shù)據(jù)進(jìn)行壓縮的最小值是多少)
expire???????????? 過期時(shí)間,0 為永不過期,可使用 unix 時(shí)間戳格式或距離當(dāng)前時(shí)間的秒數(shù),設(shè)為秒數(shù)時(shí)不能大于 2592000(30 天)
2.7.3.返回值
成功返回 TRUE,失敗返回 FALSE。
2.7.4.范例
/*?procedural?API?*//*?connect?to?memcached?server?*/
$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
/*
set?value?of?item?with?key?‘var_key‘
using?0?as?flag?value,?compression?is?not?used
expire?time?is?30?second
*/
memcache_set($memcache_obj,?‘var_key‘,?‘some?variable‘,?0,?30);
echo?memcache_get($memcache_obj,?‘var_key‘);
?> /*?OO?API?*/
$memcache_obj?=?new?Memcache;
/*?connect?to?memcached?server?*/
$memcache_obj->connect(‘memcache_host‘,?11211);
/*
set?value?of?item?with?key?‘var_key‘,?using?on-the-fly?compression
expire?time?is?50?seconds
*/
$memcache_obj->set(‘var_key‘,?‘some?really?big?variable‘,?MEMCACHE_COMPRESSED,?50);
echo?$memcache_obj->get(‘var_key‘);
?>
2.8.Memcache::get
2.8.1.說明
string Memcache::get ( string $key [, int &$flags ] )
array Memcache::get ( array $keys [, array &$flags ] )
獲取某個(gè) key 的變量緩存值
2.8.2.參數(shù)
key??????????????? 緩存值的鍵
flags????????????? 如果是傳址某個(gè)變量,獲取緩存值被set或是add的flag結(jié)果將被存于該變量
2.8.3.返回值
返回緩存的指定 key 的變量?jī)?nèi)容或者是在失敗或該變量的值不存在時(shí)返回 FALSE
?如果傳出的key的數(shù)組中的key都不存在,返回的結(jié)果是一個(gè)空數(shù)組,反之則返回key與緩存值相關(guān)聯(lián)的關(guān)聯(lián)數(shù)組
2.8.4.范例
/*?procedural?API?*/$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
$var?=?memcache_get($memcache_obj,?‘some_key‘);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->connect(‘memcache_host‘,?11211);
$var?=?$memcache_obj->get(‘some_key‘);
/*
You?also?can?use?array?of?keys?as?a?parameter.
If?such?item?wasn‘t?found?at?the?server,?the?result
array?simply?will?not?include?such?key.
*/
/*?procedural?API?*/
$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
$var?=?memcache_get($memcache_obj,?Array(‘some_key‘,?‘a(chǎn)nother_key‘));
//如果some_key,another_key不存在?$var?=?array();
//如果some_key,another_key存在?????$var?=?array(‘some_key‘=>‘緩存值‘,?‘a(chǎn)nother_key‘=>‘緩存值‘);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->connect(‘memcache_host‘,?11211);
$var?=?$memcache_obj->get(Array(‘some_key‘,?‘second_key‘));
?>
2.9.Memcache::delete
2.9.1.說明
bool?Memcache::delete?(?string?$key?[,?int?$timeout?]?)刪除某一個(gè)變量的緩存
2.9.2.參數(shù)
key ? ? ? ?? 緩存的鍵 鍵值不能為null和‘’,當(dāng)它等于前面兩個(gè)值的時(shí)候php會(huì)有警告錯(cuò)誤。
timeout?? 刪除這項(xiàng)的時(shí)間,如果它等于0,這項(xiàng)將被立刻刪除反之如果它等于30秒,那么這項(xiàng)被刪除在30秒內(nèi)?
2.9.3.返回值
成功返回 TRUE,失敗返回 FALSE。
2.9.4.范例
/*?procedural?API?*/$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
/*?after?10?seconds?item?will?be?deleted?by?the?server?*/
memcache_delete($memcache_obj,?‘key_to_delete‘,?10);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->connect(‘memcache_host‘,?11211);
$memcache_obj->delete(‘key_to_delete‘,?10);
?>
2.10.Memcache::flush
2.10.1.說明
bool?Memcache::flush?(?void?)清空所有緩存內(nèi)容,不是真的刪除緩存的內(nèi)容,只是使所有變量的緩存過期,使內(nèi)存中的內(nèi)容被重寫
2.10.2.返回值
成功返回 TRUE,失敗返回 FALSE。
2.10.3.范例
/*?procedural?API?*/$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
memcache_flush($memcache_obj);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->connect(‘memcache_host‘,?11211);
$memcache_obj->flush();
?>
2.11.Memcache::getExtendedStats
2.11.1.說明
array?Memcache::getExtendedStats?([?string?$type?[,?int?$slabid?[,?int?$limit?]]]?)獲取所有服務(wù)器擴(kuò)展靜態(tài)信息?
2.11.2.參數(shù)
type?????? 靜態(tài)信息類型,有效值包括{reset, malloc, maps, cachedump, slabs, items, sizes},依照一定規(guī)則協(xié)議這個(gè)可選參數(shù)是為了方便開發(fā)人員查看不同類別的信息而輸入的標(biāo)題
slabid?? 用于按指定類型聯(lián)合設(shè)置 cache 堆為有效的片到堆中。緩存堆被被命令綁定到服務(wù)器上并被嚴(yán)格的用于調(diào)試用途
limit ? ?? 用于按指定類型聯(lián)合設(shè)置 cache 堆為輸入的數(shù)字所限制的大小到堆,默認(rèn)值為 100?
2.11.3.返回值
返回一個(gè)由服務(wù)器擴(kuò)展靜態(tài)信息二維數(shù)組,失敗時(shí)返回 FALSE?
2.11.4.范例
$memcache_obj?=?new?Memcache;$memcache_obj->addServer(‘memcache_host‘,?11211);
$memcache_obj->addServer(‘failed_host‘,?11211);
$stats?=?$memcache_obj->getExtendedStats();?print_r($stats);
?>
<br />輸出結(jié)果Array(
[memcache_host:11211]?=>?Array(
[pid]?=>?3756
[uptime]?=>?603011
[time]?=>?1133810435
[version]?=>?1.1.12
[rusage_user]?=>?0.451931
[rusage_system]?=>?0.634903
[curr_items]?=>?2483
[total_items]?=>?3079
[bytes]?=>?2718136
[curr_connections]?=>?2
[total_connections]?=>?807
[connection_structures]?=>?13
[cmd_get]?=>?9748
[cmd_set]?=>?3096
[get_hits]?=>?5976
[get_misses]?=>?3772
[bytes_read]?=>?3448968
[bytes_written]?=>?2318883
[limit_maxbytes]?=>?33554432
),
[failed_host:11211]?=>
)
2.12.Memcache::getStats
2.12.1.說明
array?Memcache::getStats?([?string?$type?[,?int?$slabid?[,?int?$limit?]]]?)獲取最后添加服務(wù)器靜態(tài)信息?
2.12.2.參數(shù)
type ? ? ? 靜態(tài)信息類型,有效值包括{reset, malloc, maps, cachedump, slabs, items, sizes},依照一定規(guī)則協(xié)議這個(gè)可選參數(shù)是為了方便開發(fā)人員查看不同類別的信息而輸入的標(biāo)題
slabid?? 用于按指定類型聯(lián)合設(shè)置 cache 堆為有效的片到堆中。緩存堆被被命令綁定到服務(wù)器上并被嚴(yán)格的用于調(diào)試用途
limit????? 用于按指定類型聯(lián)合設(shè)置 cache 堆為輸入的數(shù)字所限制的大小到堆,默認(rèn)值為 100?
2.12.3.返回值
返回一個(gè)服務(wù)器靜態(tài)信息數(shù)組,失敗時(shí)返回 FALSE?
2.13.Memcache::getServerStatus
2.13.1.說明
int?Memcache::getServerStatus?(?string?$host?[,?int?$port?]?)通過輸入的 host 及 port 來獲取相應(yīng)的服務(wù)器信息
2.13.2.參數(shù)
host 服務(wù)器域名或 IP
port 端口號(hào),默認(rèn)為 11211
2.13.3.返回值
返回服務(wù)器狀態(tài),0 為失敗,其他情況返回非 0 數(shù)字?
2.13.4.范例
/*?OO?API?*/$memcache?=?new?Memcache;
$memcache->addServer(‘memcache_host‘,?11211);
echo?$memcache->getServerStatus(‘memcache_host‘,?11211);
/*?procedural?API?*/
$memcache?=?memcache_connect(‘memcache_host‘,?11211);
echo?memcache_get_server_status($memcache,?‘memcache_host‘,?11211);
?>
2.14.Memcache::getVersion
2.14.1.說明
string?Memcache::getVersion?(?void?)獲取服務(wù)器的版本號(hào)信息?
2.14.2.返回值
成功返回服務(wù)器的版本號(hào)字符串,失敗返回 FALSE?
2.14.3.范例
/*?OO?API?*/$memcache?=?new?Memcache;
$memcache->connect(‘memcache_host‘,?11211);
echo?$memcache->getVersion();
/*?procedural?API?*/
$memcache?=?memcache_connect(‘memcache_host‘,?11211);
echo?memcache_get_version($memcache);
?>
2.15.Memcache::setCompressThreshold
bool?Memcache::setCompressThreshold?(?int?$threshold?[,?float?$min_savings?]?)設(shè)置壓縮極限
2.15.2.參數(shù)
threshold 設(shè)置控制自動(dòng)壓縮的變量長(zhǎng)度的最小值
min_saving 指定的最低壓縮比率,值必須介于 0 - 1 之間,默認(rèn)為 0.2 代表 20% 的壓縮比率?
2.15.3.返回值
成功返回 TRUE,失敗返回 FALSE。?
2.15.4.范例
/*?OO?API?*/$memcache_obj?=?new?Memcache;
$memcache_obj->addServer(‘memcache_host‘,?11211);
$memcache_obj->setCompressThreshold(20000,?0.2);
/*?procedural?API?*/
$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
memcache_set_compress_threshold($memcache_obj,?20000,?0.2);
?>
2.16.Memcache::setServerParams
2.16.1.說明
bool?Memcache::setServerParams?(?string?$host?[,?int?$port?[,?int?$timeout?[,?int?$retry_interval?[,?bool?$status?[,?callback?$failure_callback?]]]]]?)Memcache version 2.1.0 后增加的函數(shù),運(yùn)行時(shí)設(shè)置服務(wù)器參數(shù)
2.16.2.參數(shù)
host?????????? 服務(wù)器域名或 IP
port 端口號(hào),默認(rèn)為 11211
timeout???? 超時(shí)連接失效的秒數(shù),修改默認(rèn)值 1 時(shí)要三思,有可能失去所有緩存方面的優(yōu)勢(shì)導(dǎo)致連接變得很慢
retry_interval???? 服務(wù)器連接失敗時(shí)的重試頻率,默認(rèn)是 15 秒一次,如果設(shè)置為 -1 將禁止自動(dòng)重試,當(dāng)擴(kuò)展中加載了 dynamically via dl() 時(shí),無論本參數(shù)還是常連接設(shè)置參數(shù)都會(huì)失效。 每一個(gè)失敗的服務(wù)器在失效前都有獨(dú)自的生存期,選擇后端請(qǐng)求時(shí)會(huì)被跳過而不服務(wù)于請(qǐng)求。一個(gè)過期的連接將成功的重新連接或者被標(biāo)記為失敗的連接等待下一次 重試。這種效果就是說每一個(gè) web server 的子進(jìn)程在服務(wù)于頁(yè)面時(shí)的重試連接都跟他們自己的重試頻率有關(guān)。
status ?? 控制服務(wù)器是否被標(biāo)記為 online,設(shè)置這個(gè)參數(shù)為 FALSE 并設(shè)置 retry_interval 為 -1 可以使連接失敗的服務(wù)器被放到一個(gè)描述不響應(yīng)請(qǐng)求的服務(wù)器池子中,對(duì)這個(gè)服務(wù)器的請(qǐng)求將失敗,接受設(shè)置為失敗服務(wù)器的設(shè)置,默認(rèn)參數(shù)為 TRUE,代表該服務(wù)器可以被定義為 online。
failure_callback ?? 失敗時(shí)的回調(diào)函數(shù),函數(shù)的兩個(gè)參數(shù)為失敗服務(wù)器的 hostname 和 port?
2.16.3.返回值
成功返回 TRUE,失敗返回 FALSE。?
2.16.4.范例
function?_callback_memcache_failure($host,?$port){
print?"memcache?‘$host:$port‘?failed";
}
/*?OO?API?*/
$memcache?=?new?Memcache;
//?Add?the?server?in?offline?mode
$memcache->addServer(‘memcache_host‘,?11211,?FALSE,?1,?1,?-1,?FALSE);
//?Bring?the?server?back?online
$memcache->setServerParams(‘memcache_host‘,?11211,?1,?15,?TRUE,?‘_callback_memcache_failure‘);
/*?procedural?API?*/
$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
memcache_set_server_params($memcache_obj,?‘memcache_host‘,?11211,?1,?15,?TRUE,?‘_callback_memcache_failure‘);
?>
2.17.Memcache::increment
2.17.1.說明
int?Memcache::increment?(?string?$key?[,?int?$value?]?)給指定 key 的緩存變量一個(gè)增值,如果該變量不是數(shù)字時(shí)不會(huì)被轉(zhuǎn)化為數(shù)字,這個(gè)增值將會(huì)加到該變量原有的數(shù)字之上,變量不存在不會(huì)新增變量,對(duì)于壓縮存儲(chǔ)的變量不要使用本函數(shù)因?yàn)橄鄳?yīng)的取值方法會(huì)失敗。?
2.17.2.參數(shù)
key 緩存值的鍵
var 值,整型將直接存儲(chǔ),其他類型將被序列化存儲(chǔ)?
2.17.3.返回值
成功返回新的變量值,失敗返回 FALSE。
2.17.4.范例
/*?procedural?API?*/$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
/*?increment?counter?by?2?*/
$current_value?=?memcache_increment($memcache_obj,?‘counter‘,?2);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->connect(‘memcache_host‘,?11211);
/*?increment?counter?by?3?*/
$current_value?=?$memcache_obj->increment(‘counter‘,?3);
?>
2.18.Memcache::decrement
2.18.2.說明
int?Memcache::decrement?(?string?$key?[,?int?$value?]?)給指定 key 的緩存變量一個(gè)遞減值,與 increment 操作類似,將在原有變量基礎(chǔ)上減去這個(gè)值,該項(xiàng)的值將會(huì)在轉(zhuǎn)化為數(shù)字后減去,新項(xiàng)的值不會(huì)小于 0,對(duì)于壓縮存儲(chǔ)的變量不要使用本函數(shù)因?yàn)橄鄳?yīng)的取值方法會(huì)失敗。?
2.18.2.參數(shù)
key 緩存值的鍵
var 值,整型將直接存儲(chǔ),其他類型將被序列化存儲(chǔ)?
2.18.3.返回值
成功返回新的變量值,失敗返回 FALSE。?
2.18.4.范例
/*?procedural?API?*/$memcache_obj?=?memcache_connect(‘memcache_host‘,?11211);
/*?decrement?item?by?2?*/
$new_value?=?memcache_decrement($memcache_obj,?‘test_item‘,?2);
/*?OO?API?*/
$memcache_obj?=?new?Memcache;
$memcache_obj->connect(‘memcache_host‘,?11211);
/*?decrement?item?by?3?*/
$new_value?=?$memcache_obj->decrement(‘test_item‘,?3);
?>
2.19.memcache_debug
2.19.1.說明
bool?memcache_debug?(?bool?$on_off?)設(shè)置 memcache 的調(diào)試器是否開啟,值為 TRUE 或 FALSE。 受影響于 php 安裝時(shí)是否使用了 --enable-debug 選項(xiàng),如果使用了該函數(shù)才會(huì)返回 TRUE,其他情況將始終返回 FALSE。?
2.19.2.參數(shù)
on_off 設(shè)置調(diào)試模式是否開啟,TRUE 為開啟,F(xiàn)ALSE 為關(guān)閉
2.19.3.返回值
php 安裝時(shí)如果使使用了 --enable-debug 選項(xiàng)返回 TRUE,否則將返回 FALSE。
轉(zhuǎn)于:http://www.cnblogs.com/whoamme/p/3437146.html

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to use Memcache for efficient data writing and querying in PHP development? With the continuous development of Internet applications, the requirements for system performance are getting higher and higher. In PHP development, in order to improve system performance and response speed, we often use various caching technologies. One of the commonly used caching technologies is Memcache. Memcache is a high-performance distributed memory object caching system that can be used to cache database query results, page fragments, session data, etc. By storing data in memory

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

The practice and thinking of Memcache caching technology to optimize data interaction in PHP In modern Web applications, data interaction is a very important issue. It is not efficient enough and will limit the scalability and performance of Web applications. In order to speed up data interaction, our usual approach is to optimize the design of the database, improve the performance of the hardware and increase the server capacity. However, these methods all have a common limitation: they increase the cost of the system. In recent years, Memcache technology has made progress in solving this problem.

Memcache is an open source, distributed caching technology. It greatly improves the speed of data access by storing data in memory, thus improving the performance and responsiveness of the website. In PHP projects, Memcache caching technology is also widely used and has achieved good results. This article will deeply explore the application and practice of Memcache caching technology in PHP projects. 1. Principles and advantages of Memcache Memcache is a memory caching technology that can store data

With the rapid development of the Internet, more and more applications need to face a large number of concurrent requests. How to improve the concurrent processing capabilities of applications has become a problem that developers need to solve. Among them, using Memcache caching technology for concurrency optimization has become a relatively popular solution. Memcache is an efficient caching technology suitable for large-scale web applications, databases and distributed systems. Its characteristic is to store data in memory to achieve high-speed read and write operations. During the data access process of web applications,

As web applications become increasingly complex, performance has become a critical issue. In many applications, database queries are one of the most time-consuming operations. In order to avoid frequently reading data from the database, a caching system can be used to store frequently read data in memory for quick access. In PHP development, using Memcached for distributed caching is an extremely common practice. In this article we will introduce how to use Memcached for distributed caching. What is Memca

How to use Memcache to optimize data storage operations in your PHP application? In web application development, data storage is a crucial link. In PHP applications, Memcache, as a memory cache system, can effectively improve the efficiency of data storage and reading operations. This article will introduce how to use Memcache to optimize data storage operations in PHP applications, and attach specific code examples. Step 1: Install the Memcache extension First, you need to install Me in your PHP environment
