设为首页 收藏本站
查看: 998|回复: 0

[经验分享] PHP 常用缓存 APC,Memcache & Memcached

[复制链接]

尚未签到

发表于 2015-8-31 09:47:35 | 显示全部楼层 |阅读模式
  APC:Alternative PHP Cache(APC)是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开并且强健的架构来缓存和优化 PHP 的中间代码。
  链接地址:http://php.net/manual/en/book.apc.php
  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.php
  Memcached:高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
  链接地址:http://memcached.org/
  
  APC VS Memcached
  Memcached 为分布式缓存系统,而 APC 为非分布式缓存系统。
  如果你的 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:

  • 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.
  • Read through callbacks are the best thing since sliced bread. It has simplified much of my code.
  • getDelayed() is a nice feature that can reduce the time your script has to wait for the results to come back from the server.
  • 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.
  • 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/memcachepecl/memcachedFirst Release Date2004-06-082009-01-29 (beta)Actively Developed?YesYesExternal DependencyNonelibmemcachedFeaturesAutomatic Key Fixup1YesNoAppend/PrependNoYesAutomatic Serialzation2YesYesBinary ProtocolNoOptionalCASNoYesCompressionYesYesCommunication TimeoutConnect OnlyVarious OptionsConsistent HashingYesYesDelayed GetNoYesMulti-GetYesYesSession SupportYesYesSet/Get to a specific serverNoYesStores NumericsConverted to StringsYes  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  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)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-106666-1-1.html 上篇帖子: Windows下memcache的配置和使用(python) 下篇帖子: Memcache简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表