最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
WordPress给网站配置Redis 缓存的例子
时间:2022-06-25 19:01:10 编辑:袖梨 来源:一聚教程网
我下面提出的解决方案是在 Redis 中缓存所有输出的 HTML 内容而无需再让 WordPress 重复执行页面脚本。这里使用 Redis 代替 Varnish 设置简单,而且可能更快。
安装 Redis
如果你使用的是 Debian 或者衍生的操作系统可使用如下命令安装 Redis:
apt-get install redis-server
使用 Predis 作为 Redis 的 PHP 客户端
你需要一个客户端开发包以便 PHP 可以连接到 Redis 服务上。
这里我们推荐 Predis. 上传 predis.php 到 WordPress 的根目录。
前端缓存的 PHP 脚本
步骤1: 在 WordPress 的根目录创建新文件 index-with-redis.php ,内容如下:
代码如下 | 复制代码 |
// change vars here $start = microtime(); // start timing page exec // if cloudflare is enabled // from wp // init predis // init vars // check if page isn't a comment submission // check if logged in to wp // check if a cache of the page exists echo $redis->hget($dkey, $ukey); // if a comment was submitted or clear page cache request was made delete cache of page require('./wp-blog-header.php'); // delete entire cache, works only if logged in require('./wp-blog-header.php'); // if logged in don't cache anything require('./wp-blog-header.php'); // cache the page // turn on output buffering require('./wp-blog-header.php'); // get contents of output buffer // clean output buffer // Store to cache only if the page exist and is not a search result. $end = microtime(); // get end execution time // show messages if debug is enabled if ($cached && $display_powered_by_redis) { Page generated in ".t_exec($start, $end)." sec } // time diff // get time ?> |
你也可以在 Github 上查看 index-with-redis.php
步骤2:将上述代码中的 IP 地址和网站域名替换成你网站的 IP 地址和域名
步骤3:在 .htaccess 中将所有出现 index.php 的地方改为 index-with-redis.php ,如果你使用的是 Nginx 则修改 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。
性能测试
◦没有 Redis 的情况下,平均首页执行 1.614 秒,文章页 0.174 秒(无任何缓存插件)
◦使用 Redis 的情况下,平均页面执行时间 0.00256 秒
我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。
其他建议
本文作者的 WordPress 环境是 Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 VPS 中,无缓存插件。
请确认使用了 gzip 压缩,可加快访问速度。
访问 wp-admin
要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.
本文其实在国内已经有很翻译过了,但我看到作者也一直在更新此文,反而国内译者都不怎么更新,我就自己去重新折腾了一遍。
相关文章
- 奥比岛梦想国度卡死闪退有哪些解决方法 04-30
- DNF手游骨戒在哪个位置 04-30
- 回望羊驼:当利空成为短暂的财富密码 04-30
- 为什么每个 Web3 项目都逃不过 DEX? 04-30
- HashKey Exchange 虚拟资产保险覆盖规模登顶全球第一 04-30
- HTX DeepThink:冲上$95,000后,等待比特币的是什么? 04-30