前言:
Redis是一种高级的键值存储系统,类似于memcached。它将所有数据存储在内存中,因此每秒可以执行超过10万次的GET操作。
下面是汇站提出的解决方案:在Redis中缓存所有输出的HTML内容,而无需让WordPress重复执行页面脚本。使用Redis代替Varnish设置简单,并且可能更快。
首先安装Redis:
如果你使用的是Debian或其衍生操作系统,可以使用以下命令安装Redis:
apt-get install redis-server
或者参考安装指南进行安装。
使用Predis作为Redis的PHP客户端
你需要一个PHP客户端开发包,以便PHP可以连接到Redis服务。我们推荐使用Predis。将predis.php上传到WordPress的根目录。
前端缓存的PHP脚本
步骤1:在WordPress的根目录中创建一个新文件index-with-redis.php。
代码如下:
<?php
// Change these two variables:
$seconds_of_caching = 60*60*24*7; // 7 days.
$ip_of_this_website = '204.62.14.112';
/*
- This file is written by Jim Westergren, copyright all rights reserved.
- See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
- The code is free for everyone to use how they want but please mention my name and link to my article when writing about this.
- Change $ip_of_this_website to the IP of your website above.
- Add ?refresh=yes to the end of a URL to refresh it's cache
- You can also enter the redis client via the command prompt with the command "redis-cli" and then remove all cache with the command "flushdb".
*/
// Very necessary if you use Cloudfare:
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
// This is from WordPress:
define('WP_USE_THEMES', true);
// Start the timer:
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$start = microtime();
// Initiate redis and the PHP client for redis:
include("predis.php");
$redis = new Predis\Client('');
// few variables:
$current_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$current_page_url = str_replace('?refresh=yes', '', $current_page_url);
$redis_key = md5($current_page_url);
// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment
if (isset($_GET['refresh']) || substr($_SERVER['REQUEST_URI'], -12) == '?refresh=yes' || ($_SERVER['HTTP_REFERER'] == $current_page_url && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REMOTE_ADDR'] != $ip_of_this_website)) {
require('./wp-blog-header.php');
$redis->del($redis_key);
// Second case: cache exist in redis, let's display it
} else if ($redis->exists($redis_key)) {
$html_of_current_page = $redis->get($redis_key);
echo $html_of_current_page;
echo "<!-- This is cache -->";
// third: a normal visitor without cache. And do not cache a preview page from the wp-admin:
} else if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website && strstr($current_page_url, 'preview=true') == false) {
require('./wp-blog-header.php');
$html_of_current_page = file_get_contents($current_page_url);
$redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);
echo "<!-- Cache has been set -->";
// last case: the normal WordPress. Should only be called with file_get_contents:
} else {
require('./wp-blog-header.php');
}
// Let's display some page generation time (note: CloudFlare may strip out comments):
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {
echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";
}
?>
或者直接下载 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)。
性能测试:
1.没有Redis 的情况下,平均首页执行1.614 秒,文章页0.174 秒(无任何缓存插件)
2.使用Redis 的情况下,平均页面执行时间0.00256秒
我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。
其他建议:
汇站的环境是Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 nano VPS 中,无缓存插件。
请确认使用了gzip压缩,可加快访问速度。
访问 wp-admin
要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.
转载请注明:汇站网 » (WordPress加速教程)纯代码为Redis缓存输出HTML页面
汇站网 WordPress教程 (WordPress加速教程)纯代码为Redis缓存输出HTML页面 https://www.huizhanii.com/33126.html


相关文章
- WordPress禁用自动生成缩略图和多尺寸图片的方法(经过亲测可行) 2023-09-27
- WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100% 2023-09-27
- 在WordPress中添加复制版权提示 2023-09-24
- 解决 WordPress 启用 CDN 后无法获取用户真实 IP 的方法 2023-09-24
- 解决 Windows 无法使用代理的问题的可能方法 2023-09-24
- WordPress利用jQuery代码绕过防盗链限制 2023-09-23
- WordPress 如何改变JPEG图片的压缩质量 2023-09-22
- 禁止WordPress订阅用户登陆后访问后台和跳转到指定页面 2023-09-22
- 纯代码自动为WordPress文章添加已使用过的标签 2023-09-21
- 纯代码生成WordPress网站地图使用XML格式创建一个sitemap.xml 2023-09-21