konakona
[原创]整合Redis到WordPress进行缓存加速!
[原创]整合Redis到WordPress进行缓存加速!

众所周知的redis,我用,我知道。

 

要求PHP版本5.3以上,我知道现在还有很多不同话的孩子用着PHP5.2,不乖,真的很不乖,难怪做的网站慢!!活该!!!http://blog.crazyphper.com/wp-content/uploads/2012/09/2.gif

我ubuntu啊!!!

1.

 

apt-get install redis-server

 

2. 下载 predis.php  放到 WordPress 的根目录。

 

3.在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. -->";

}

?>

4.将代码中的IP换成你网站的IP

5.访问 http://www.你的网站.com/index-with-redis.php 就可以看到效果啦!当然,你还需要将.htaccess中的index.php改为 index-with-redis.php

 

 

 

 

 

 

 

 

 

赞赏
首页      程序开发      PHP      wordpress      [原创]整合Redis到WordPress进行缓存加速!
https://secure.gravatar.com/avatar/3b712b34a0e1b689cfb524c9c6bcdc47?s=256&r=g

团哥

文章作者

继续玩我的CODE,让别人说去。 低调,就是这么自信。

发表评论

textsms
account_circle
email

konakona

[原创]整合Redis到WordPress进行缓存加速!
众所周知的redis,我用,我知道。   要求PHP版本5.3以上,我知道现在还有很多不同话的孩子用着PHP5.2,不乖,真的很不乖,难怪做的网站慢!!活该!!! 我ubuntu啊!!! …
扫描二维码继续阅读
2012-12-03