WordPress 启用 WP Super Cache 和 Memcached Object Cache 方法

WordPress 启用 WP Super Cache 和 Memcached Object Cache 才是终极加速方案,早前已经给大家介绍过了 WP Super Cache 插件,下面给大家介绍下 Memcached Object Cache 。(转载:slyar

1、WP Super Cache 插件缓存HTML页面和资源,减少硬盘读写次数
2、Memcached Object Cache 缓存MySQL查询语句尤其是组合查询语句,减少数据库读写次数
3、CloudFlare CDN 提供CDN缓存,尽可能减少首次访问用户对服务器的读写

CloudFlare不说了,简单明了。主要说一下WP Super Cache和Memcached Object Cache的同时启用

安装组件

WP Super Cache开启PHP缓存模式,压缩关闭使用服务器的Gzip即可,JS和CSS minify关闭因为有可能会导致文件损坏,其他默认即可。在未启动Memcached之前是看不到对象缓存选项的,下面说一下Memcached

首先一点,Memcache(传送门)是一项技术。Memcache是一个自由和开放源代码、高性能、分配的内存对象缓存系统。用于加速动态web应用程序,减轻数据库负载,原理是在内存里维护一张hash表保存SQL查询,后来的相同查询即可直接从内存里读取而不必访问数据库了。因此,你需要安装Memcache,服务器的名称叫做Memcached

# Redhat/Centos 用yum,Debian/Ubuntu用apt-get
yum -y install memcached

# 启动服务
service memcached start

# 开机自启动
chkconfig memcached on

# 安装 dependencies
yum install cyrus-sasl-devel zlib-devel gcc-c++

# 下载最新版 libmemcached 源码包
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -xvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18

# 编译安装
./configure --disable-memcached-sasl
make
make install

# 用 pecl 安装 memcached
pecl install memcached

# Enable it for PHP
# 把 extension=memcached.so 取消注释或者添加到php.ini
# 比如 /usr/local/php/etc/php.ini

# 如果不知道php.ini在哪,执行下面这行即可
php --ini

# 重新启动 nginx 和 php-fpm
service nginx restart
service php-fpm reload

memcached默认监听端口是11211,一般没必要改

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7992690399077928&output=html&h=280&adk=3912376373&adf=1951976151&w=1200&abgtt=6&fwrn=4&fwrnh=100&lmt=1731224946&num_ads=1&rafmt=1&armr=3&sem=mc&pwprc=8547337849&ad_type=text_image&format=1200×280&url=https%3A%2F%2Fxiaoyi.vc%2Fwp-super-cache-memcached-object-cache.html&fwr=0&pra=3&rh=200&rw=1221&rpe=1&resp_fmts=3&wgl=1&fa=27&uach=WyJXaW5kb3dzIiwiMTUuMC4wIiwieDg2IiwiIiwiMTMwLjAuMjg0OS44MCIsbnVsbCwwLG51bGwsIjY0IixbWyJDaHJvbWl1bSIsIjEzMC4wLjY3MjMuMTE3Il0sWyJNaWNyb3NvZnQgRWRnZSIsIjEzMC4wLjI4NDkuODAiXSxbIk5vdD9BX0JyYW5kIiwiOTkuMC4wLjAiXV0sMF0.&dt=1731224946845&bpp=1&bdt=289&idt=-M&shv=r20241106&mjsv=m202411060101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D4335771965ee0dd1%3AT%3D1730182931%3ART%3D1731224807%3AS%3DALNI_MaD8Q_O8Ku5ajhXg1uyZQ1D-08Ueg&gpic=UID%3D00000f59ba1fe6b8%3AT%3D1730182931%3ART%3D1731224807%3AS%3DALNI_MYSlYxqBz2P25OAtoxckyyXJPoi6Q&eo_id_str=ID%3D53a7be49bcdd781c%3AT%3D1730182931%3ART%3D1731224807%3AS%3DAA-AfjZ5su3VxK20cbvyU2WQ16xg&prev_fmts=0x0&nras=2&correlator=2319871223287&frm=20&pv=1&u_tz=480&u_his=5&u_h=1080&u_w=1920&u_ah=1032&u_aw=1920&u_cd=24&u_sd=0.75&dmc=8&adx=731&ady=1654&biw=2540&bih=1226&scr_x=0&scr_y=0&eid=44759876%2C44759927%2C31088723%2C95344190%2C31088764%2C31088457%2C95345967&oid=2&pvsid=3842732176222231&tmod=119920016&wsm=1&uas=0&nvt=1&ref=https%3A%2F%2Fxiaoyi.vc%2F&fc=1408&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1032%2C2549%2C1226&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&bz=0.75&td=1&tdf=2&psd=W251bGwsbnVsbCxudWxsLDNd&nt=1&ifi=2&uci=a!2&btvi=1&fsb=1&dtd=3

好,现在忘掉服务器Memcached,我们来谈一下 PHP Memcache 和 PHP Memcached。这两个都是Memcache的客户端,不带D的是老版本,带D的是新版本,更高更快更强,所以我们用 PHP Memcached (传送门)

整合WordPress

OK现在你有了服务器Memcached,也有了客户端PHP Memcached,下面需要让WordPress可以调用PHP Memcached,所以你需要一个插件,Memcached Object Cache

官网版本(只能用于PHP Memcache!):Memcached Object Cache

PHP Memcached版本:tollmanz/wordpress-pecl-memcached-object-cache

切记要选择对版本,如果你安装的是Memcache(不带D的版本),官网的插件是对的;如果你安装的是Memcached,切记请使用Github那个版本,否则就会出现下面的错误

Slyar$ curl -IL http://www.slyar.com/blog/2016-honda-accord-touring-v6.html
HTTP/1.1 500 Internal Server Error
Date: Tue, 25 Oct 2016 15:07:10 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
GMT; path=/; domain=.slyar.com; HttpOnly
X-Powered-By: PHP

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7992690399077928&output=html&h=280&adk=3912376373&adf=2669303856&w=1200&abgtt=6&fwrn=4&fwrnh=100&lmt=1731224946&num_ads=1&rafmt=1&armr=3&sem=mc&pwprc=8547337849&ad_type=text_image&format=1200×280&url=https%3A%2F%2Fxiaoyi.vc%2Fwp-super-cache-memcached-object-cache.html&fwr=0&pra=3&rh=200&rw=1221&rpe=1&resp_fmts=3&wgl=1&fa=27&uach=WyJXaW5kb3dzIiwiMTUuMC4wIiwieDg2IiwiIiwiMTMwLjAuMjg0OS44MCIsbnVsbCwwLG51bGwsIjY0IixbWyJDaHJvbWl1bSIsIjEzMC4wLjY3MjMuMTE3Il0sWyJNaWNyb3NvZnQgRWRnZSIsIjEzMC4wLjI4NDkuODAiXSxbIk5vdD9BX0JyYW5kIiwiOTkuMC4wLjAiXV0sMF0.&dt=1731224946845&bpp=1&bdt=289&idt=-M&shv=r20241106&mjsv=m202411060101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D4335771965ee0dd1%3AT%3D1730182931%3ART%3D1731224807%3AS%3DALNI_MaD8Q_O8Ku5ajhXg1uyZQ1D-08Ueg&gpic=UID%3D00000f59ba1fe6b8%3AT%3D1730182931%3ART%3D1731224807%3AS%3DALNI_MYSlYxqBz2P25OAtoxckyyXJPoi6Q&eo_id_str=ID%3D53a7be49bcdd781c%3AT%3D1730182931%3ART%3D1731224807%3AS%3DAA-AfjZ5su3VxK20cbvyU2WQ16xg&prev_fmts=0x0%2C1200x280&nras=3&correlator=2319871223287&frm=20&pv=1&u_tz=480&u_his=5&u_h=1080&u_w=1920&u_ah=1032&u_aw=1920&u_cd=24&u_sd=0.75&dmc=8&adx=731&ady=2480&biw=2540&bih=1226&scr_x=0&scr_y=0&eid=44759876%2C44759927%2C31088723%2C95344190%2C31088764%2C31088457%2C95345967&oid=2&pvsid=3842732176222231&tmod=119920016&wsm=1&uas=0&nvt=1&ref=https%3A%2F%2Fxiaoyi.vc%2F&fc=1408&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1032%2C2549%2C1226&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&bz=0.75&td=1&tdf=2&psd=W251bGwsbnVsbCxudWxsLDNd&nt=1&ifi=3&uci=a!3&btvi=2&fsb=1&dtd=11

2016/10/25 23:11:33 [error] 20413#0: *1013 FastCGI sent in stderr: “PHP message: PHP Fatal error: Class ‘Memcache’ not found in /xxxxxxxxxxxxxxxxx/wp-content/object-cache.php on line 425” while reading response header from upstream, client: xxxxxxxxxxx, server: slyar.com, request: “GET /blog/2016-honda-accord-touring-v6.html HTTP/1.1”, upstream: “fastcgi://unix:/tmp/php-cgi.sock:”, host: “www.slyar.com”

下载插件,然后将object-cache.php手动上传到/wp-content/下面(注意不是plugin目录)

然后再次进入WP Super Cache的设置界面 – 高级,这时应该就可以看到这个选项了,『 使用对象缓存系统来存储缓存文件。 (实验室功能)』,勾选,更新设置

如果一切正常,wp-content看起来应该是这样的

[slyar@slyar.com wp-content]$ ls -l *.php
-rw-r--r--. 1 www www  1027 Oct 25 07:46 advanced-cache.php
-rw-r--r--. 1 www www 85719 Oct 25 08:25 object-cache.php
-rw-r--r--. 1 www www  4991 Oct 27 04:53 wp-cache-config.php

Memcached探针

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7992690399077928&output=html&h=280&adk=3912376373&adf=416148578&w=1200&abgtt=6&fwrn=4&fwrnh=100&lmt=1731224946&num_ads=1&rafmt=1&armr=3&sem=mc&pwprc=8547337849&ad_type=text_image&format=1200×280&url=https%3A%2F%2Fxiaoyi.vc%2Fwp-super-cache-memcached-object-cache.html&fwr=0&pra=3&rh=200&rw=1221&rpe=1&resp_fmts=3&wgl=1&fa=27&uach=WyJXaW5kb3dzIiwiMTUuMC4wIiwieDg2IiwiIiwiMTMwLjAuMjg0OS44MCIsbnVsbCwwLG51bGwsIjY0IixbWyJDaHJvbWl1bSIsIjEzMC4wLjY3MjMuMTE3Il0sWyJNaWNyb3NvZnQgRWRnZSIsIjEzMC4wLjI4NDkuODAiXSxbIk5vdD9BX0JyYW5kIiwiOTkuMC4wLjAiXV0sMF0.&dt=1731224946845&bpp=1&bdt=290&idt=1&shv=r20241106&mjsv=m202411060101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D4335771965ee0dd1%3AT%3D1730182931%3ART%3D1731224807%3AS%3DALNI_MaD8Q_O8Ku5ajhXg1uyZQ1D-08Ueg&gpic=UID%3D00000f59ba1fe6b8%3AT%3D1730182931%3ART%3D1731224807%3AS%3DALNI_MYSlYxqBz2P25OAtoxckyyXJPoi6Q&eo_id_str=ID%3D53a7be49bcdd781c%3AT%3D1730182931%3ART%3D1731224807%3AS%3DAA-AfjZ5su3VxK20cbvyU2WQ16xg&prev_fmts=0x0%2C1200x280%2C1200x280&nras=4&correlator=2319871223287&frm=20&pv=1&u_tz=480&u_his=5&u_h=1080&u_w=1920&u_ah=1032&u_aw=1920&u_cd=24&u_sd=0.75&dmc=8&adx=731&ady=3182&biw=2540&bih=1226&scr_x=0&scr_y=0&eid=44759876%2C44759927%2C31088723%2C95344190%2C31088764%2C31088457%2C95345967&oid=2&pvsid=3842732176222231&tmod=119920016&wsm=1&uas=0&nvt=1&ref=https%3A%2F%2Fxiaoyi.vc%2F&fc=1408&brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1032%2C2549%2C1226&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&bz=0.75&td=1&tdf=2&psd=W251bGwsbnVsbCxudWxsLDNd&nt=1&ifi=4&uci=a!4&btvi=3&fsb=1&dtd=13

下面来查看以下Memcached的工作情况

下载pecl-memcache: https://pecl.php.net/get/memcache-3.0.8.tgz

解压缩以后提取里面的memcache.php放置在web能访问到的地方,vim编辑,改一下下面这几个地方就好了

define('ADMIN_USERNAME','www.slyar.com');    // Admin Username
define('ADMIN_PASSWORD','www.slyar.com');    // Admin Password

$MEMCACHE_SERVERS[] = '127.0.0.1:11211'; // add more as an array
// $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

网页访问即可看到Memcached的工作情况了,HIT(命中)92%效果非常显著

WordPress 启用 WP Super Cache 和 Memcached Object Cache 方法-彩豆博客
------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞501 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容