php memcache与php memcached以及遇到的问题

默北 Memcache PHP317,9361字数 2114阅读7分2秒阅读模式

PHP有两个memcache客户端:php memcache和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端异常时,经常切换实例,会取到老数据。文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

1.2 高并发下稳定性问题
新浪微博提到的教训:
php memcache换成php memcached,在高并发下稳定下极大提高;
另外功能更多,出错码更精确。文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

Twitter的缓存经验
多层次Cache,减轻某些cache节点宕掉后的影响,读写都cache;
将memcached api统一换为libmemcached(方便多语言访问memcached,让分布式等各种规则都一致。)文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

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无效。文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

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.文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

Here are some more advanced features and information.文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

项目				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文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

转载请注明出处:https://www.ttlsa.com/html/2467.html文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/ 文章源自运维生存时间-https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 20/08/2013 23:04:31
  • 转载请务必保留本文链接:https://www.ttlsa.com/memcache/memcache-and-memcached-php-php-and-problems/
评论  3  访客  2
    • 圣达菲
      圣达菲 9

      是的

      • 规范
        规范 9

        好的

      评论已关闭!