关于PHP扩展memcache和memcached的区别

来源:互联网 发布:阿里云幕布ps能过吗 编辑:程序博客网 时间:2024/06/10 08:34

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

PHP的客户端目前常用的有两个,一个是memcache,另一个是memcached,两个客户端只差了一个字母,这两个的区别是什么呢?

  • 服务器中的memcached进程跑的是memcached服务;
  • 实现了memcached接口的PHP扩展memcache,在PHP框架之内实现的;
  • 实现了memcached接口的PHP扩展memcached,基于libmemcached实现;

手册是最好的说明(大家没事还是多看看官方说明吧):
memcached
memcached类官方说明
memcached扩展包

memcache
memcache类官方说明
memcache扩展包

下面是PHP官网给予的相应解释
For those confuse about the memcached extension and the memcache extension, the short story is that both of them are clients of memcached server, and the memcached extension offer more features than the memcache extension.

大致意思是memcached extension和memcache extension都是memcached server的客户端,memcached extension比memcache extension的功能要多。

两个都支持一致性hash算法:

  • memcache
# 修改php.ini[Memcache]Memcache.allow_failover = 1 Memcache.hash_strategy = consistentMemcache.hash_function = crc32
  • memcached
$memcachedObj = new memcached();$memcachedObj->setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);$memcachedObj->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE,true); 

另外说一下session在memcached(注意这个是指memcached服务)中的存储,下面是配置

# memcache:session.save_handler = memcachesession.save_path = "tcp://localhost:11211" # memcached:session.save_handler = memcachedsession.save_path = "localhost:11211"
0 0