在写这篇文章之前,xxx已经写过了几篇关于改站点缓存主题的文章,想要了解的朋友可以去翻一下之前的文章
本篇文章重要分析一下在maegnto里cache(File System, APC, Memcached, Redis)的应用,及在不同的服务器环境中改怎么应用让其能性到达最好。
解理magento的Two-Level Caching
magento默许应用zend framework的二层缓存存储式方。就是说它应用两层构结对cache停止配合理管,一个快的,但小大有制限的构结是一层比如APC或者Memcached ,一个比较慢的构结作为第二层比如file system.每一种存储构结各有利弊,要不同情况不同分析应用,APC 和 Memcached 是应用 key/value来存储cache,他们都不持支tag。File system 和Redis 持支tag.
magento二级缓存构结工作流程图示 (Thanks to Fabrizio Branca):

magento自带的各种后端缓存分析:
File system (var/cache)
默许情况下,Magento 将它的缓存目条存储在file系统中,在var/cache/下可查看。这类情况很合适小型的,数据量不大的站点。但是对于大型的站点,随着浏览量的一直增多,对file的读写作操也将越来越多,站点也会越来越慢。magento是由tags来对cache停止织组理管的,这意味着可以对某一个cache组(同相的tag为一个group)停止作操。
长处:这是默许的,不要需装额定的件软
缺陷:清除cache依赖于tag,常通修改某个product或理处某个order完以后,对应的前台页面都要需更新缓存。每次更新缓存时,都要需根据tag停止有所目条即file停止查找,试想如果站点有多于1000个product,个整cache的小大将会大于50MB,大约有3500个file,你能象想到每次更新cache都要对3500个file停止查找有多慢吗。
小提示
1:应用SSD替换一般硬盘
2:把var/cache接入tmpfs
----------------------------------------------------------------------------------------------------------------------------------
APC – Alternative PHP Cache (Key/Value)
APC是一个收费,源开且强壮的架框用来缓存和化优 PHP 的中间代码。
长处:相对file cache system是很快了
缺陷:不持支tag,所以然依要需file system作为slow level cache。服务器要需安装PHP APC 块模
小提示:保确有够足的内存给APC ,可在 php.ini 中修改数参apc.shm_size
Configuration (app/etc/local.xml)
<global>
...
<cache>
<backend>apc</backend>
<prefix>mgt_</prefix>
</cache>
...
</global>
Settings for php.ini
apc.enabled = 1
apc.optimization = 0
apc.shm_segments = 1
apc.shm_size = 768M
apc.ttl = 48000
apc.user_ttl = 48000
apc.num_files_hint = 8096
apc.user_entries_hint = 8096
apc.mmap_file_mask = /tmp/apc.XXXXXX
apc.enable_cli = 1
apc.cache_by_default = 1
apc.max_file_size = 10M
apc.include_once_override = 0
---------------------------------------------------------------------------------------------------------------------------
Memcached (Key/Value)
Memcache是一个高能性的分布式的内存对象缓存系统,通过在内存里维护一个一统的伟大的hash表,它能够用来存储各种格式的数据,括包图像、视频、文件以及数据库检索的结果等。单简的说就是将数据调用到内存中,然后从内存中取读,从而大大提高取读速度。
长处:更快的存取速度
缺陷:不持支tag,所以然依要需file system作为slow level cache
需求:1:Memcached server 2: PHP extension for memcached
每日一道理
古人云:“海纳百川,有容乃大。”人世间,不可能没有矛盾和争吵,我们要以磊落的胸怀和宽容的微笑去对面它 。哈伯德也曾说过:“宽恕和受宽恕的难以言喻的快乐,是连神明都会为之羡慕的极大乐事。”让我们从宽容中享受快乐,从谅解中体会幸福吧!
<global>
...
<cache>
<backend>memcached</backend><!-- apc / memcached / empty=file -->
<memcached><!-- memcached cache backend related config -->
<servers><!-- any number of server nodes can be included -->
<server>
<host><![CDATA[127.0.0.1]]></host>
<port><![CDATA[11211]]></port>
<persistent><![CDATA[1]]></persistent>
</server>
</servers>
<compression><![CDATA[0]]></compression>
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>
...
</global>
---------------------------------------------------------------------------------------------------------------------
Redis – Advanced key-value store with full cache tag support
magento答应我们应用redis server作为中心存储仓库,它持支tag的应用,所以不再要需file system作为slow level cache。在多服务器多站点环境中,强烈荐推应用redis
,用一个中心缓存仓库,对有所server cache停止理管。
长处:快;持支tag;已在一个日均ip为500000的站点做过测试,能性极好且稳定。
需求:1:服务器上要需装Redis 2:PHP 扩展 phpredis 要需安装 3:Magento扩展“Cm_Cache_Backend_Redis”要需安装
Installation
1. Install redis (2.4+ required)
2. Install phpredis
3. Install the magento extension “Cm_Cache_Backend_Redis”
4. Edit your app/etc/local.xml
<global>
...
<cache>
<backend>Cm_Cache_Backend_Redis</backend>
<backend_options>
<server>127.0.0.1</server> <!-- or absolute path to unix socket -->
<port>6379</port>
<persistent></persistent>
<database>0</database>
<password></password>
<force_standalone>0</force_standalone>
<connect_retries>1</connect_retries>
<automatic_cleaning_factor>0</automatic_cleaning_factor>
<compress_data>1</compress_data>
<compress_tags>1</compress_tags>
<compress_threshold>20480</compress_threshold>
<compression_lib>gzip</compression_lib> <!-- Supports gzip, lzf and snappy -->
</backend_options>
</cache>
...
</global>
Useful tool for redis
phpRedisAdmin
phpRedisAdmin 是一个单简作操界对面 Redis databases停止作操
Demo: http://dubbelboer.com/phpRedisAdmin/?overview
结总
小型站点应用APC + file system(作为slow level cache)便可,此外荐推应用SSD和把var/cache/放入tmpfs。
对于大型站点荐推应用redis, 即便cache file到达 500 MB,它然依很快,在多服务器环境中,Redis 也是很美完的选择。
载转请注标jonas的magento博客