霜天部落 | 关注LAMP高性能、高并发架构的设计与研究

PHP 的两个 memcache 扩展:memcache 和 memcached

配置好PHP + memcache 后,做测试发现错误:Class ‘Memcached’ not found in …,于是查手册。

在手册上明显可以看到这两个扩展:

http://php.net/manual/en/book.memcache.php

http://php.net/manual/en/book.memcached.php

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

从手册上看,memcached 会比 memcache 多几个方法,使用方式上都差不多。

看看严谨的老外们怎么说的: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.

结合自己的项目情况,我使用了 memcache。