• 欢迎访问微视觉网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入微视觉微视觉-影视后期交流
  • 本站全面支持自动充值,目的是更好的服务大家!
  • 本站全面开启SSL服务,请放心使用!
  • 如果您觉得本站对你非常有用,那么赶紧使用Ctrl+D 收藏吧

通过Redis加速WordPress

前沿资讯 Jason.w.wei 1年前 (2017-10-09) 474次浏览 已收录 0个评论

首先将你的 index.php 文件做个备份,建议更名为 index.php.bak(实际上这个文件用处不太大,直接删除都行,不信你自己看内容)

然后将下面的内容,保存为 index.php,并且上传到根目录:

<?php
/*
    author: jeedo aquino
    file: wp-index-redis.php
    credit: jim westergren
    updated: 2012-10-23
    this is a redis caching system for wordpress inspired by jim westergren.
    see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
    some caching mechanics are different from jim's script which is summarized below:
    - cached pages do not expire not unless explicitly deleted or reset
    - appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in
    - appending a ?r=y to a url deletes the cache of that url
    - script still works even if allow_fopen is disabled
    - submitting a comment deletes the cache of that page
    - refreshing (f5) a page deletes the cache of that page
    - includes a debug mode, stats are displayed at the bottom most part after </html>
    for setup and configuration see more here:
    www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/
    use this script at your own risk. i currently use this albeit a slightly modified version
    to display a redis badge whenever a cache is displayed.
*/
// change vars here
$cf = 0;                // set to 1 if you are using cloudflare
$debug = 1;             // set to 1 if you wish to see execution time and cache actions
$start = microtime();   // start timing page exec
// if cloudflare is enabled
if ($cf) {
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
    }
}
// from wp
define('WP_USE_THEMES', true);
// init predis
include("predis.php");
$redis = new Predis\Client('');
// init vars
$domain = $_SERVER['HTTP_HOST'];
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = str_replace('?r=y', '', $url);
$url = str_replace('?c=y', '', $url);
$dkey = md5($domain);
$ukey = md5($url);
// check if page isn't a comment submission
(($_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0);
// check if logged in to wp
$cookie = var_export($_COOKIE, true);
$loggedin = preg_match("/wordpress_logged_in/", $cookie);
// check if a cache of the page exists
if ($redis->hexists($dkey, $ukey) && !$loggedin && !$submit) {
    echo $redis->hget($dkey, $ukey);
    if (!$debug) exit(0);
    $msg = 'this is a cache';
// if a comment was submitted or clear page cache request was made delete cache of page
} else if ($submit || substr($_SERVER['REQUEST_URI'], -4) == '?r=y') {
    require('./wp-blog-header.php');
    $redis->hdel($dkey, $ukey);
    $msg = 'cache of page deleted';
// delete entire cache, works only if logged in
} else if ($loggedin && substr($_SERVER['REQUEST_URI'], -4) == '?c=y') {
    require('./wp-blog-header.php');
    if ($redis->exists($dkey)) {
        $redis->del($dkey);
        $msg = 'domain cache flushed';
    } else {
        $msg = 'no cache to flush';
    }
// if logged in don't cache anything
} else if ($loggedin) {
    require('./wp-blog-header.php');
    $msg = 'not cached';
// cache the page
} else {
    // turn on output buffering
    ob_start();
    require('./wp-blog-header.php');
    // get contents of output buffer
    $html = ob_get_contents();
    // clean output buffer
    ob_end_clean();
    echo $html;
    // store html contents to redis cache
    $redis->hset($dkey, $ukey, $html);
    $msg = 'cache is set';
}
$end = microtime(); // get end execution time
// show messages if debug is enabled
if ($debug) {
    echo "<div id=\"redis_powered\"><div>$msg : ".t_exec($start, $end)." sec</div></div>";
}
// time diff
function t_exec($start, $end) {
    $t = (getmicrotime($end) - getmicrotime($start));
    return round($t,5);
}
// get time
function getmicrotime($t) {
    list($usec, $sec) = explode(" ",$t);
    return ((float)$usec + (float)$sec);
}
?>

还需要一个文件predis.php,已经打包好了,直接下载即可(猜猜下载地址在哪)

最后在你主题的 style.css 末尾添加如下代码:

#redis_powered{margin:0px auto;background:url(https://birdteam.net/wp-content/themes/begin/img/redis.png) 10px no-repeat #fff;border:1px solid #D7D8DF;padding:10px;width:320px;}
#redis_powered div{width:300px;text-align:right;font:9px/10px arial,sans-serif;color:#000;}

微视觉 , 版权所有丨如未注明 , 均为网络收集丨本网站采用BY-NC-SA协议进行授权 , 转载请注明通过 Redis 加速 WordPress
喜欢 (6)
[wuwei967@126.com]
分享 (0)

您必须 登录 才能发表评论!