(WordPress终极优化)使用 Redis 缓存来提升 WordPress 的性能 - 汇站网

(WordPress终极优化)使用 Redis 缓存来提升 WordPress 的性能

2023-11-06 0 282

正文:

Redis 是一种高级的键值存储系统,类似于 memcached,它将所有数据存储在内存中,因此每秒可以执行超过 10 万次的 GET 操作。

下面是我提出的解决方案:在 Redis 中缓存所有输出的 HTML 内容,而无需让 WordPress 重复执行页面脚本。使用 Redis 代替 Varnish 设置简单,而且可能更快。

(WordPress终极优化)使用 Redis 缓存来提升 WordPress 的性能

安装 Redis

如果你使用的是 Debian 或其衍生操作系统,可以使用以下命令安装 Redis:

apt-get install redis-server
或者参考安装指南进行安装。

使用 Predis 作为 Redis 的 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 PredisClient(”);

// 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

注意:将上述代码中的 IP 地址替换成你网站的 IP 地址

ps:

在 .htaccess 中将所有出现 index.php 的地方改为 index-with-redis.php ,如果你使用的是 Nginx 则修改 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。

转载请注明:汇站网 » (WordPress 终极优化)使用 Redis 缓存来提升 WordPress 的性能

收藏 (0)

微信扫一扫

支付宝扫一扫

点赞 (0)

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

本站声明

本资源仅用于个人学习和研究使用,禁止用于任何商业环境!

 1.  本网站名称:汇站网
 2.  本站永久网址:https://www.huizhanii.com/
 3.  本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
 4.  未经原版权作者许可,禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
 5.  为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
 6.  若资源侵犯了您的合法权益, 请持您的版权证书和相关原作品信息来信通知我们请来信     通知我们我们会及时删除,给您带来的不便,我们深表歉意!
 7.  如下载链接失效、广告或者压缩包问题请联系站长处理!
 8.  如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
 9.  本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
 10.  因源码具有可复制性,一经赞助 ,不得以任何形式退款。
 11.  更多详情请点击查看

汇站网 WordPress教程 (WordPress终极优化)使用 Redis 缓存来提升 WordPress 的性能 https://www.huizhanii.com/33648.html

汇站

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

发表评论
暂无评论
  随机评论 表情开关按钮图片
表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情
登录后评论
联系官方客服

为您解决烦忧 - 24小时在线 专业服务

(汇站网)一个专注站长资源的平台网站,提供最新的网站模板和整站源码,内容包含各类精品网页模板,企业网站模板,网站模板,DIV+CSS模板,织梦模板,帝国cms模板,discuz模板,wordpress模板,个人博客论坛模板,上千种免费网页模板下载尽在汇站网.找源码上汇站.huizhanii.com