php memcache和php memcached是php的memcache分布式的高速緩存系統的兩個客戶端,php memcache是老客戶端,php memcached是功能更加完善的新的代替php memcached的.
php memcache獨立用php實現,是老客戶端,從我們實踐中已發現有多個問題,而且功能少,屬性也可設置的少;
php memcached是基于原生的c的libmemcached的擴展,更加完善,建議替換為php memcached.
1.Php memcache的問題
1.1 分布式問題
php memcache默認會自動切換實例,所以有時取到老數據,并且value飄忽不定.
網友分享的問題:
這幾天做某個產品的時候遇到一個小問題,現象比較詭異,產品用了兩臺分布式的memcached服務器,某一個計數器取回來的數偶爾會不對,最后定位在php memcache client的failover機制上面.
我們知道,在memcached分布式環境下,某一個key是通過hash計算,分配到某一個memcached上面的.
如果php.ini里面 memcache.allow_failover = 1的時候,在分布式環境下,某一臺memcached出問題的話,會自動到其他的memcached嘗試,就會出現上面的問題,所以要設置 allow_failover = 0 那么取不到時就直接返回失敗而不會從其它mc上取,這樣以避免網絡異常或server端異常時,經常切換實例,會取到老數據.
1.2 高并發下穩定性問題
新浪微博提到的教訓:
php memcache換成php memcached,在高并發下穩定下極大提高;
另外功能更多,出錯碼更精確。
Twitter的緩存經驗
多層次Cache,減輕某些cache節點宕掉后的影響,讀寫都cache;將memcached api統一換為libmemcached(方便多語言訪問memcached,讓分布式等各種規則都一致).
1.3 1秒超時間隔沒法修改問題
php memcache客戶端有個1秒超時間隔沒法修改問題:
bool Memcache::connect(string $host [,int $port [,int $timeout ]])
第三個參數本來可設置timeout,單位秒,但無法修改.
測試了以下三種修改timeout的方法都無效:
1.3.1. 用memcache api Memcache::setServerParams不能修改;
1.3.2. 改memcache 源代碼vi php_memcache.h宏定義不能修改;
1.3.3. php.ini內這個配置:default_socket_timeout = 60對本timeout無效。
2.memcache和memcached對比
Php memcache這個老客戶端在屬性設置方面可設置的很少,出錯碼粒度很粗,出錯后難以定位,而且功能欠缺一些:
- 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.
- 項目 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
- //開源軟件:Vevb.com
- //注釋:
- 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
新聞熱點
疑難解答