php扩展Memcache和Memcached的区别

来源:互联网 发布:数组中删除指定元素 编辑:程序博客网 时间:2024/05/19 04:02

关于这两个扩展的区别网上有很多基本相同的说法,大致可以总结为下面几点

  1. memcache是原生的,pecl扩展,memcached是基于libmemcached建立的,memcached提供了更多的使用方法,这点可以从php Manual中提供的api可以看出。
  2. memcache由于是原生的,可以支持OO和非OO两种编程接口,memcached仅可以支持OO一种。
  3. memcached支持binary protocol,拥有更佳的性能。
  4. memcached可以使用setOption()方法进行配置项设置,memcache需要在php.ini文件中配置或者在代码中使用ini_set()方法进行配置。
  5. memcache支持长连接,memcached不支持长连接。

大致以上几点,找了几篇文章,大都是雷同的,但是关于第5点memcached不支持长连接,从php Manual中可以轻松查到memcached也支持长连接。

这是memcached的构造方法:
public Memcached::__construct ([ string $persistent_id ] )
其中的可选参数$persistent_id 的说明为:

By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.

大致的意思就是memcached默认是不支持持久化的,每次请求结束之后就会被销毁,但是可以通过`persistent_id`这个参数创建一个可以持久化的memcached实例,所有相同`persistent_id` 值得memcached实例可以共享相同的连接。

下面就可以试验一下memcached构造函数中的这个参数的作用。

//使用过3次,就可以使用该持久连接了.$memcache = new Memcached('mc');var_dump($memcache->getServerList());if(!count($memcache->getServerList())){    echo "--";    $memcache->addServer('localhost',11211);}$memcache->set('name',12);var_dump($memcache->get('name'));

通过php-fpm运行以上代码,发现前三次请求$memcache->getServerList() 都是空数组,第四次开始就可以直接使用$memcache 实例进行getset 等操作了。

0 0
原创粉丝点击