PHP有兩個(gè)memcache客戶端:php memcache和php memcached。
php memcache獨(dú)立用php實(shí)現(xiàn),是老客戶端,從我們實(shí)踐中已發(fā)現(xiàn)有多個(gè)問題,而且功能少,屬性也可設(shè)置的少;
php memcached是基于原生的c的libmemcached的擴(kuò)展,更加完善,建議替換為php memcached。
1. Php memcache的問題
1.1 分布式問題
php memcache默認(rèn)會(huì)自動(dòng)切換實(shí)例,所以有時(shí)取到老數(shù)據(jù),并且value飄忽不定。
網(wǎng)友分享的問題:
這幾天做某個(gè)產(chǎn)品的時(shí)候遇到一個(gè)小問題,現(xiàn)象比較詭異,產(chǎn)品用了兩臺(tái)分布式的memcached服務(wù)器。某一個(gè)計(jì)數(shù)器取回來(lái)的數(shù)偶爾會(huì)不對(duì),最后定位在php memcache client的failover機(jī)制上面。
我們知道,在memcached分布式環(huán)境下,某一個(gè)key是通過(guò)hash計(jì)算,分配到某一個(gè)memcached上面的。
如果php.ini里面 memcache.allow_failover = 1的時(shí)候,在分布式環(huán)境下,某一臺(tái)memcached出問題的話,會(huì)自動(dòng)到其他的memcached嘗試,就會(huì)出現(xiàn)上面的問題。所以要設(shè)置 allow_failover = 0 那么取不到時(shí)就直接返回失敗而不會(huì)從其它mc上取,這樣以避免網(wǎng)絡(luò)異?;騭erver端異常時(shí),經(jīng)常切換實(shí)例,會(huì)取到老數(shù)據(jù)。
1.2 高并發(fā)下穩(wěn)定性問題
新浪微博提到的教訓(xùn):
php memcache換成php memcached,在高并發(fā)下穩(wěn)定下極大提高;
另外功能更多,出錯(cuò)碼更精確。
Twitter的緩存經(jīng)驗(yàn)
多層次Cache,減輕某些cache節(jié)點(diǎn)宕掉后的影響,讀寫都cache;
將memcached api統(tǒng)一換為libmemcached(方便多語(yǔ)言訪問memcached,讓分布式等各種規(guī)則都一致。)
1.3 1秒超時(shí)間隔沒法修改問題
php memcache客戶端有個(gè)1秒超時(shí)間隔沒法修改問題:
bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )
第三個(gè)參數(shù)本來(lái)可設(shè)置timeout,單位秒,但無(wú)法修改。
測(cè)試了以下三種修改timeout的方法都無(wú)效:
1.3.1. 用memcache api Memcache::setServerParams不能修改;
1.3.2. 改memcache 源代碼vi php_memcache.h宏定義不能修改;
1.3.3. php.ini內(nèi)這個(gè)配置:default_socket_timeout = 60對(duì)本timeout無(wú)效。
2. memcache和memcached對(duì)比
Php memcache這個(gè)老客戶端在屬性設(shè)置方面可設(shè)置的很少;
出錯(cuò)碼粒度很粗,出錯(cuò)后難以定位;
而且功能欠缺一些:
There are primarily two clients used with PHP. One is the older, more widespread pecl/memcache and the other is the newer, less used, more feature rich pecl/memcached.
Both support the basics such as multiple servers, setting vaules, getting values, increment, decrement and getting stats.
Here are some more advanced features and information.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
項(xiàng)目 pecl/memcache pecl/memcached
First Release Date 2004-06-08 2009-01-29 (beta)
Actively Developed Yes Yes
External Dependency None libmemcached
Automatic Key Fixup1 Yes No
Append/Prepend No Yes
Automatic Serialzation2 Yes Yes
Binary Protocol No Optional
CAS No Yes
Compression Yes Yes
Communication Timeout Connect Only Various Options
Consistent Hashing Yes Yes
Delayed Get No Yes
Multi-Get Yes Yes
Session Support Yes Yes
Set/Get to a specific server No Yes
Stores Numerics Converted to Strings Yes
|
注釋:
1 pecl/memcache will convert an invalid key into a valid key for you. pecl/memcached will return false when trying to set/get a key that is not valid.
2 You do not have to serialize your objects or arrays before sending them to the set commands. Both clients will do
哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無(wú)需額外費(fèi)用,即可穩(wěn)步提升排名至首頁(yè)。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。
