Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joelkallman/5595721 to your computer and use it in GitHub Desktop.
Save joelkallman/5595721 to your computer and use it in GitHub Desktop.

Revisions

  1. @JimWestergren JimWestergren revised this gist Apr 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index-with-redis.php
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@
    $ukey = md5($url);

    // check if page isn't a comment submission
    (($_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0);
    (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0);

    // check if logged in to wp
    $cookie = var_export($_COOKIE, true);
  2. @JimWestergren JimWestergren revised this gist Feb 15, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index-with-redis.php
    Original file line number Diff line number Diff line change
    @@ -110,8 +110,8 @@
    ob_end_clean();
    echo $html;

    // Store to cache only if the page exist. Otherwise status code 200 is sent when 404 should be sent.
    if (!is_404()) {
    // Store to cache only if the page exist and is not a search result.
    if (!is_404() && !is_search()) {
    // store html contents to redis cache
    $redis->hset($dkey, $ukey, $html);
    $msg = 'cache is set';
  3. @JimWestergren JimWestergren revised this gist Dec 12, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion README
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    See more info here: [www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/] (http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/)
  4. @JimWestergren JimWestergren revised this gist Dec 12, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    See more info here: http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
    See more info here: [www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/] (http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/)
  5. @JimWestergren JimWestergren revised this gist Dec 12, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    See more info here: http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
  6. @JimWestergren JimWestergren revised this gist Dec 5, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions index-with-redis.php
    Original file line number Diff line number Diff line change
    @@ -110,9 +110,12 @@
    ob_end_clean();
    echo $html;

    // store html contents to redis cache
    $redis->hset($dkey, $ukey, $html);
    $msg = 'cache is set';
    // Store to cache only if the page exist. Otherwise status code 200 is sent when 404 should be sent.
    if (!is_404()) {
    // store html contents to redis cache
    $redis->hset($dkey, $ukey, $html);
    $msg = 'cache is set';
    }
    }

    $end = microtime(); // get end execution time
  7. @JimWestergren JimWestergren revised this gist Oct 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index-with-redis.php
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,7 @@
    $loggedin = preg_match("/wordpress_logged_in/", $cookie);

    // check if a cache of the page exists
    if ($redis->hexists($dkey, $ukey) && !$loggedin && !$submit) {
    if ($redis->hexists($dkey, $ukey) && !$loggedin && !$submit && !strpos($url, '/feed/')) {

    echo $redis->hget($dkey, $ukey);
    $cached = 1;
  8. @JimWestergren JimWestergren revised this gist Oct 25, 2012. 1 changed file with 134 additions and 58 deletions.
    192 changes: 134 additions & 58 deletions index-with-redis.php
    Original file line number Diff line number Diff line change
    @@ -1,69 +1,145 @@
    <?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".
    Author: Jim Westergren & Jeedo Aquino
    File: index-with-redis.php
    Updated: 2012-10-25
    This is a redis caching system for wordpress.
    see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
    Originally written by Jim Westergren but improved by Jeedo Aquino.
    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
    - 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.
    */

    // Very necessary if you use Cloudfare:
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];

    // change vars here
    $cf = 1; // set to 1 if you are using cloudflare
    $debug = 0; // set to 1 if you wish to see execution time and cache actions
    $display_powered_by_redis = 1; // set to 1 if you want to display a powered by redis message with execution time, see below

    $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'];
    }
    }
    // This is from WordPress:

    // from wp
    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:

    // init predis
    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:

    // 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);
    $cached = 1;
    $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 {
    require('./wp-blog-header.php');

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


    // 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. -->";

    if ($cached && $display_powered_by_redis) {
    // You should move this CSS to your CSS file and change the: float:right;margin:20px 0;
    echo "<style>#redis_powered{float:right;margin:20px 0;background:url(http://images.staticjw.com/jim/3959/redis.png) 10px no-repeat #fff;border:1px solid #D7D8DF;padding:10px;width:190px;}
    #redis_powered div{width:190px;text-align:right;font:10px/11px arial,sans-serif;color:#000;}</style>";
    echo "<a href=\"http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/\" style=\"text-decoration:none;\"><div id=\"redis_powered\"><div>Page generated in<br/> ".t_exec($start, $end)." sec</div></div></a>";
    }

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

    ?>
  9. @JimWestergren JimWestergren renamed this gist Jul 5, 2012. 1 changed file with 0 additions and 0 deletions.
  10. @JimWestergren JimWestergren renamed this gist Jul 5, 2012. 1 changed file with 0 additions and 0 deletions.
  11. @invalid-email-address Anonymous created this gist Jul 5, 2012.
    69 changes: 69 additions & 0 deletions Redis as a Frontend Cache for WordPress
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?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. -->";
    }
    ?>