PHP 经常使用缓存 APC,Memcache & Memcached

APC:Alternative PHP Cache(APC)是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开而且强健的架构来缓存和优化 PHP 的中间代码。
连接地址:http://php.net/manual/en/book.apc.phpphp

Memcache:Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.
连接地址:http://php.net/manual/en/book.memcache.phpweb

Memcached:高性能的,分布式的内存对象缓存系统,用于在动态应用中减小数据库负载,提高访问速度。
连接地址:http://memcached.org/算法

 APC VS Memcached数据库

Memcached 为分布式缓存系统,而 APC 为非分布式缓存系统。api

若是你的 web 应用程序架设于不一样的 web 服务器(负载平衡),你可使用 memcached 构建你的缓存系统。若是不是,彻底可使用 APC 来构建,并且架设于一台服务器上,APC 的速度优于 memcached。数组

Memcached 内存使用方式也和 APC 不一样。APC是基于共享内存和 MMAP 的,memcachd 有本身的内存分配算法和管理方式,它和共享内存没有关系,也没有共享内存的限制,一般状况下,每一个 memcached 进程能够管理2GB的内存空间,若是须要更多的空间,能够增长进程数。 (截取自:http://blog.developers.api.sina.com.cn/?p=124缓存

Memcached 是“分布式”的内存对象缓存系统,那么就是说,那些不须要“分布”的,不须要共享的,或者干脆规模小到只有一台服务器的应用,memcached 不会带来任何好处,相反还会拖慢系统效率,由于网络链接一样须要资源,即便是 UNIX 本地链接也同样。 在我以前的测试数据中显示,memcached 本地读写速度要比直接PHP内存数组慢几十倍,而 APC、共享内存方式都和直接数组差很少。可见,若是只是本地级缓存,使用 memcached 是很是不划算的。(截取自:http://blog.developers.api.sina.com.cn/?p=124服务器

跨服务器使用 memcached,最好要使用内网,否则的话,受路由的影响,memcached 常常会链接超时(超过100ms),并且会凭空多出来两倍的宽带流量。(截取自:http://www.9enjoy.com/compare-one-server-apc-memcached/网络

注:更多关于Memcached信息,移步 http://blog.developers.api.sina.com.cn/?p=124架构

Memcached VS Memcache

主要区别是php memcached扩展比较新,几乎支持 memcached 的全部特性(如 Delayed Get, Append/Prepend 等). 可是它依赖 libmemcached 才能运行(在debian里面包名是libMemcached5).
因此若是你不使用如 Delayed Get 这样的特性,又不想多依赖 libmemcached 库, 彻底可使用 memcache 扩展. 反之请选择 memcached 扩展。(截取自:http://www.leonzhang.com/2011/06/24/memcached-vs-php-memcache/

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. The most important features that memcached has are:

  1. Cas tokens. This made my life much easier and is an easy preventive system for stale data. Whenever you pull something from the cache, you can receive with it a cas token (a double number). You can than use that token to save your updated object. If no one else updated the value while your thread was running, the swap will succeed. Otherwise a newer cas token was created and you are forced to reload the data and save it again with the new token.
  2. Read through callbacks are the best thing since sliced bread. It has simplified much of my code.
  3. getDelayed() is a nice feature that can reduce the time your script has to wait for the results to come back from the server.
  4. While the memcached server is supposed to be very stable, it is not the fastest. You can use binary protocol instead of ASCII with the newer client.
  5. Whenever you save complex data into memcached the client used to always do serialization of the value (which is slow), but now with memcached client you have the option of using igbinary. So far I haven't had the chance to test how much of a performance gain this can be.

All of this points were enough for me to switch to the newest client, and can tell you that it works like a charm. There is that external dependency on the libmemcached library, but have managed to install it nonetheless on Ubuntu and Mac OSX, so no problems there so far.

If you decide to update to the newer library, I suggest you update to the latest server version as well as it has some nice features as well. You will need to install libevent for it to compile, but on Ubuntu it wasn't much trouble.

I haven't seen any frameworks pick up the new memcached client thus far (although I don't keep track of them), but I presume Zend will get on board shortly.

(截取自:http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached-with-php

 

表格对比了这两个扩展的具体特性

  pecl/memcache pecl/memcached
First Release Date 2004-06-08 2009-01-29 (beta)
Actively Developed? Yes Yes
External Dependency None libmemcached
Features
Automatic Key Fixup1 Yes No
Append/Prepend No Yes
Automatic Serialzation2 Yes Yes
Binary Protocol No Optional
CAS No Yes
Compression Yes Yes
Communication Timeout Connect Only Various Options
Consistent Hashing Yes Yes
Delayed Get No Yes
Multi-Get Yes Yes
Session Support Yes Yes
Set/Get to a specific server No Yes
Stores Numerics Converted to Strings Yes

 

1. pecl/memcache will convert an invalid key into a valid key for you. pecl/memcached will return false when trying to set/get a key that is not valid.

2. You do not have to serialize your objects or arrays before sending them to the set commands. Both clients will do this for you.

(来源:http://code.google.com/p/memcached/wiki/PHPClientComparison

相关文章
相关标签/搜索