apache_conf Wordpress缓存使用redis(eric mann方法)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache_conf Wordpress缓存使用redis(eric mann方法)相关的知识,希望对你有一定的参考价值。

<?php

/**
 * WP Redix Index
 * 
 * Redis caching system for WordPress. Inspired by Jim Westergren.
 * 
 * @author Jeedo Aquino
 * @see http://www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/
 * @see http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
 */
// 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/autoload.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 $msg . ': ';
    echo t_exec($start, $end);
}

// 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);
}

?>

*Assuming that you already have redis installed in your server. <br/> (if not, read https://gist.github.com/amrt0510/62fd45dd5aa409a255cd )*


##Points to follow:-

1. Goto http://eamann.com/tech/ludicrous-speed-wordpress-caching-with-redis/ & read full artical.
2. In your server root put predis library.
3. You need to have redis server installed somewhere (can be on the same server).
4. Inside your wp root place both the files `redis-index.php` & `.htaccess`.
5. Run `{your-domain}/{your-wp}/index.php` (make sure admin isn't logged in).
6. When you run it for the first time it will cache all the html & afterwards whenever you visit the page it will load the cache but not wp.
7. you can change **.htaccess** at any time and redirection of your wp will alter on that basis.


RewriteEngine On
RewriteRule ^index\.(php|html?)$ http://localhost/wpnew1/redis-index.php?page=$1 [R=301,L]

以上是关于apache_conf Wordpress缓存使用redis(eric mann方法)的主要内容,如果未能解决你的问题,请参考以下文章

apache_conf 使用这些.htaccess添加更安全的WordPress

apache_conf 使用.htaccess锁定WordPress仪表板登录

apache_conf 使用这些.htaccess添加更安全的WordPress

apache_conf 使用这些.htaccess添加更安全的WordPress

apache_conf WordPress的默认.htaccess - 使用它来做不同的事情。

apache_conf Wordpress .htaccess