PHP 的两个 memcache 扩展:memcache 和 memcached

jackxiang 2010-6-5 15:53 | |
作者:Hily 原始链接:http://hily.me/blog/2010/05/php-memcache-vs-memcached/
版权声明:可以转载,转载时务必以超链接形式标明文章原始出处和作者信息及版权声明

从手册上明显可以看到这两个扩展:
http://php.net/manual/en/book.memcache.php
http://php.net/manual/en/book.memcached.php

在Q群里问了下,没有人能分辨出它们的差别,甚至有人怀疑我说的 Memcached 是 Memcached 服务器(守护进程)。

从手册上看,memcached 会比 memcache 多几个方法,使用方式上都差不多。
手册上有一句:
Memcached::cas()执行一个“检查并设置”的操作,因此,它仅在当前客户端最后一次取值后,该key 对应的值没有被其他客户端修改的情况下, 才能够将值写入。检查是通过cas_token参数进行的, 这个参数是Memcach指定给已经存在的元素的一个唯一的64位值, 怎样获取这个值请查看Memcached::get*() 系列方法的文档。注意:这个值作为double类型是因为PHP的整型空间限制。

译注:这是Memcached扩展比Memcache扩展一个非常重要的优势, 在这样一个系统级(Memcache自身提供)的冲突检测机制(乐观锁)下, 我们才能保证高并发下的数据安全。

也就是原子锁这块不同,摘自:http://php.net/manual/zh/memcached.cas.php

看看严谨的老外们怎么说的:
http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached-with-php

Memcached client library was just recently released as stable. It is being used by digg ( was developed for digg by Andrei Zmievski, now no longer with digg) and implements much more of the memcached protocol than the older memcache client.

memcached 实现了更多的 memcached 协议(毕竟是基于 libmemcached 库的)。

http://serverfault.com/questions/63383/memcache-vs-memcached

As Nate's link suggests, both work perfectly well for simple usage. However, memcached supports more features that allow you to get the most performance out of memcached. The binary protocol reduces the amount of data required to be sent between client and server. Multigets and multisets allow you to get/set multiple items at the same time. If you're finding you need more oomph out of memcache, memcached is the better module. The use of libmemcached suggests that the library itself is possibly more optimised than the PHP only version.

Memcached is a more recent module compared to memcache, having only been released 8 months ago. If you need to target an older version of PHP, then you can only really use memcache.

memcached 的版本比较新,而且使用的是 libmemcached 库。libmemcached 被认为做过更好的优化,应该比 php only 版本的 memcache 有着更高的性能。

这里有另外一个对比表,很明显,用 memcached 会让人放心很多:
http://code.google.com/p/memcached/wiki/PHPClientComparison
差别比较大的一点是,memcached 支持 Binary Protocol,而 memcache 不支持,意味着 memcached 会有更高的性能。不过,还需要注意的是,memcached 目前还不支持长连接:

pecl/memcached does not support failover or persistent connections. This is quite annoying, since I'm sure the underlying libmemcached C library supports both.

pecl/memcache does not have the very handy getServerByKey() method, which is immensely useful when debugging.

在这里,我推荐大家使用 memcached  

setup memcached:
无废话安装 memcache PHP 扩展 memcached

wget http://launchpad.net/libmemcached/1.0/0.40/+download/libmemcached-0.40.tar.gz
tar zxf libmemcached-0.40.tar.gz
cd libmemcached-0.40
./configure
make && make install

wget http://pecl.php.net/get/memcached-1.0.2.tgz
tar zxf memcached-1.0.2.tgz
cd memcached-1.0.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/
make && make install

echo "extension = memcached.so" >> /usr/local/php/etc/php.ini

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/3144/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2015-4-7 09:42
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]