加速WordPress技巧:Redis缓存输出的HTML页面

Redis是一个高级的key-value存储系统,类似memcached,所有内容都存在内存中,因此每秒钟可以超过10万次GET操作。我下面提出的解决方案是在Redis中缓存所有输出的HTML 内容而无需再让wordpress重复执行页面脚本。这里使用Redis代替Varnish设置简单,而且可能更快。安装 Redis如果你使用的是 Debian 或者衍生的操作系统可使用如下命令安装 Redis:apt-get install redis-server或者阅读 安装指南使用 Predis 作为 Redis 的 PHP 客户端你需要一个客户端开发包以便 PHP 可以连接到 Redis 服务上。这里我们推荐 Predis. 上传 predis.PHPwordpress 的根目录。前端缓存的PHP脚本步骤1:在wordpress 的根目录创建新文件 index-with-redis.PHP内容如下:<div class="msgborder" id="PHPcode43"><?php// Change these two variables:$seconds_of_caching = 6060247; // 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 commentif (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 "";// 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 "";// 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 "";}?>

相关文章

WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的...
这两段代码可以让你先体验一下WordPress 3.0的菜单样式 首先在functions.php添加如下代码: register_n...
有人说 WordPress 本身的 SEO 就比较好,我想这个和 WordPress 可以方便的自定义文章的固定链接有分不开...
曾经给大家介绍了在 WordPress 后台移除主题编辑器,今天继续加深,直接关闭 WordPress 后台的文件编辑...
WordPress之iNove主题内容页增加公告栏的方法,导航栏下面默认的公告栏只能在首页显示,只要修改一个文...
大多数WordPress用户都知道该怎么从数千个WordPress主题里挑出自己喜欢,但要自己开发新主题恐怕不是人...