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

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

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

1.目前大多数php环境里使用的都是不带d的memcache版本,这个版本出的比较早,是一个原生版本,完全在php框架内开发的。与之对应的带d的memcached是建立在libmemcached的基础上,所以相对来说,memcached版本的功能更全一些。
memcache:http://cn2.php.net/manual/en/book.memcache.php
memcached:http://cn2.php.net/manual/en/book.memcached.php
2.Memcache是原生实现的,支持OO和非OO两套接口并存。而memcached是使用libmemcached,只支持OO接口。
3.memcached还有个非常称赞的地方,就是flag不是在操作的时候设置了,而是有了一个统一的setOption()。Memcached实现了更多的memcached协议。
4.memcached支持Binary Protocol,而memcache不支持。这意味着memcached会有更高的性能。不过memcached目前还不支持长连接。

下面有一张表,来对比php客户端扩展memcache与memcached
http://code.google.com/p/memcached/wiki/PHPClientComparison

另外一点也是大家比较关心的,就是所使用的算法。大家都知道“一致性hash算法”是当添加或删除存储节点时,对存储在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.

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

首先看下时间,memcache最早是在2004年2月开发的,最后更新是在2013年4月,而memcached最早是在2009年1月开发的,最后更新是在2014年1月更新的。所以memcache的历史比memcached早。

在安装memcache扩展的时候并不要求安装其他东东,但是在安装memcached的时候会要求你安装libmemcached,问题来了,libmemcached是memcache的C客户端,它具有的优点是低内存,线程安全等特点。比如新浪微博之前就全面将php的memcache替换成php的memcached,在高并发下,稳定性果断提高。

memcache的方法列表在:http://cn2.php.net/memcache

memcached的方法列表在:http://www.php.net/manual/zh/book.memcached.php

memcache的方法特别少,比如getMulti,setMulti都是没有的,基本就剩下最简单的get和set了。所以说“memcached比memcache支持更多的memcache协议”。

现在在php中memcached用的很多,以前一直使用的是php的memcache扩展,最近开始改用了php的memcached扩展(注意这里memcache和memcached扩展的名字就相差了一个d)。或许在google或者百度搜索php的memcached扩展的时候,很多结果是memcache.dll或者memcache.so,很少的结果是memcached.so,windows下面甚至没有memcached.dll扩展。
memcache扩展的下载地址为:http://pecl.php.net/package/memcache
memcached扩展的下载地址为:http://pecl.php.net/package/memcached
以上两个都是源码包。
这两个扩展都是用c写的,具体的来看看memcache扩展和memcached扩展在使用上到底有哪些差别。

加载memcache扩展之后,可以在php中直接使用Memcache类,Memcache类有以下一些方法:

[php][/php]

view plaincopy

  1. Memcache {
  2.     bool add ( string $key , mixed $var [, int $flag [, int $expire ]] )
  3.     bool addServer ( string $host [, int $port = 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] )
  4.     bool close ( void )
  5.     bool connect ( string $host [, int $port [, int $timeout ]] )
  6.     int decrement ( string $key [, int $value = 1 ] )
  7.     bool delete ( string $key [, int $timeout ] )
  8.     bool flush ( void )
  9.     string get ( string $key [, int &$flags ] )
  10.     array getExtendedStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )
  11.     int getServerStatus ( string $host [, int $port = 11211 ] )
  12.     array getStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )
  13.     string getVersion ( void )
  14.     int increment ( string $key [, int $value = 1 ] )
  15.     bool pconnect ( string $host [, int $port [, int $timeout ]] )
  16.     bool replace ( string $key , mixed $var [, int $flag [, int $expire ]] )
  17.     bool set ( string $key , mixed $var [, int $flag [, int $expire ]] )
  18.     bool setCompressThreshold ( int $threshold [, float $min_savings ] )
  19.     bool setServerParams ( string $host [, int $port = 11211 [, int $timeout [, int $retry_interval = false [, bool $status [, callback $failure_callback ]]]]] )
  20. }
[php][/php]

view plaincopy

  1. add — 增加一个条目到缓存服务器
  2. addServer — 向连接池中添加一个memcache服务器
  3. close — 关闭memcache连接
  4. connect — 打开一个memcached服务端连接
  5. decrement — 减小元素的值
  6. delete — 从服务端删除一个元素
  7. flush — 清洗(删除)已经存储的所有的元素
  8. get — 从服务端检回一个元素
  9. getExtendedStats — 缓存服务器池中所有服务器统计信息
  10. getServerStatus — 用于获取一个服务器的在线/离线状态
  11. getStats — 获取服务器统计信息
  12. getVersion — 返回服务器版本信息
  13. increment — 增加一个元素的值
  14. pconnect — 打开一个到服务器的持久化连接
  15. replace — 替换已经存在的元素的值
  16. set — Store data at the server
  17. setCompressThreshold — 开启大值自动压缩
  18. setServerParams — 运行时修改服务器参数和状态

加载memcached扩展之后,可以在php中直接使用Memcached类,Memcached类有以下一些方法:

[php][/php]

view plaincopy

  1. Memcached {
  2.     __construct ([ string $persistent_id ] )
  3.     public bool add ( string $key , mixed $value [, int $expiration ] )
  4.     public bool addByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )
  5.     public bool addServer ( string $host , int $port [, int $weight = 0 ] )
  6.     public bool addServers ( array $servers )
  7.     public bool append ( string $key , string $value )
  8.     public bool appendByKey ( string $server_key , string $key , string $value )
  9.     public bool cas ( float $cas_token , string $key , mixed $value [, int $expiration ] )
  10.     public bool casByKey ( float $cas_token , string $server_key , string $key , mixed $value [, int $expiration ] )
  11.     public int decrement ( string $key [, int $offset = 1 ] )
  12.     public bool delete ( string $key [, int $time = 0 ] )
  13.     public bool deleteByKey ( string $server_key , string $key [, int $time = 0 ] )
  14.     public array fetch ( void )
  15.     public array fetchAll ( void )
  16.     public bool flush ([ int $delay = 0 ] )
  17.     public mixed get ( string $key [, callback $cache_cb [, float &$cas_token ]] )
  18.     public mixed getByKey ( string $server_key , string $key [, callback $cache_cb [, float &$cas_token ]] )
  19.     public bool getDelayed ( array $keys [, bool $with_cas [, callback $value_cb ]] )
  20.     public bool getDelayedByKey ( string $server_key , array $keys [, bool $with_cas [, callback $value_cb ]] )
  21.     public mixed getMulti ( array $keys [, array &$cas_tokens [, int $flags ]] )
  22.     public array getMultiByKey ( string $server_key , array $keys [, string &$cas_tokens [, int $flags ]] )
  23.     public mixed getOption ( int $option )
  24.     public int getResultCode ( void )
  25.     public string getResultMessage ( void )
  26.     public array getServerByKey ( string $server_key )
  27.     public array getServerList ( void )
  28.     public array getStats ( void )
  29.     public array getVersion ( void )
  30.     public int increment ( string $key [, int $offset = 1 ] )
  31.     public bool prepend ( string $key , string $value )
  32.     public bool prependByKey ( string $server_key , string $key , string $value )
  33.     public bool replace ( string $key , mixed $value [, int $expiration ] )
  34.     public bool replaceByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )
  35.     public bool set ( string $key , mixed $value [, int $expiration ] )
  36.     public bool setByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )
  37.     public bool setMulti ( array $items [, int $expiration ] )
  38.     public bool setMultiByKey ( string $server_key , array $items [, int $expiration ] )
  39.     public bool setOption ( int $option , mixed $value )
[php][/php]

view plaincopy

  1. add — 向一个新的key下面增加一个元素
  2. addByKey — 在指定服务器上的一个新的key下增加一个元素
  3. addServer — 向服务器池中增加一个服务器
  4. addServers — 向服务器池中增加多台服务器
  5. append — 向已存在元素后追加数据
  6. appendByKey — 向指定服务器上已存在元素后追加数据
  7. cas — 比较并交换值
  8. casByKey — 在指定服务器上比较并交换值
  9. __construct — 创建一个Memcached实例
  10. decrement — 减小数值元素的值
  11. delete — 删除一个元素
  12. deleteByKey — 从指定的服务器删除一个元素
  13. fetch — 抓取下一个结果
  14. fetchAll — 抓取所有剩余的结果
  15. flush — 作废缓存中的所有元素
  16. get — 检索一个元素
  17. getByKey — 从特定的服务器检索元素
  18. getDelayed — 请求多个元素
  19. getDelayedByKey — 从指定的服务器上请求多个元素
  20. getMulti — 检索多个元素
  21. getMultiByKey — 从特定服务器检索多个元素
  22. getOption — 获取Memcached的选项值
  23. getResultCode — 返回最后一次操作的结果代码
  24. getResultMessage — 返回最后一次操作的结果描述消息
  25. getServerByKey — 获取一个key所映射的服务器信息
  26. getServerList — 获取服务器池中的服务器列表
  27. getStats — 获取服务器池的统计信息
  28. getVersion — 获取服务器池中所有服务器的版本信息
  29. increment — 增加数值元素的值
  30. prepend — 向一个已存在的元素前面追加数据
  31. prependByKey — Prepend data to an existing item on a specific server
  32. replace — 替换已存在key下的元素
  33. replaceByKey — Replace the item under an existing key on a specific server
  34. set — 存储一个元素
  35. setByKey — Store an item on a specific server
  36. setMulti — 存储多个元素
  37. setMultiByKey — Store multiple items on a specific server
  38. setOption — 设置一个memcached选项
PHP 的两个 memcache 扩展:memcache 和 memcached
标签: