Topics

cache

Warming up WordPress cache, HHVM and testing blog pages

If you are minifying scripts and css files using a caching plugin or using FastCGI cache then you might need to warmup your blog after purging your cache. This is a simple warm up cli script for WordPress to initiate cache or HHVM HHBC and making sure all pages/posts do not have errors. Additionally the script creates a urllist.txt file that you can use with siege to test load your server.

#!/usr/bin/env php
< ?php

if (PHP_SAPI != 'cli') {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden', true, 403);
    exit();
}


$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('BASE_PATH', "");
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

//Disable cache
ob_end_clean();

$homeUrl = home_url() . "/";
$urlList = [$homeUrl];


$the_query = new WP_Query(['post_type' => 'any', 'post_status' => 'publish']);
//add homepage pages
warmup_pages_links($the_query->max_num_pages, $homeUrl);

//get all posts
$the_query = new WP_Query(['post_type' => 'any', 'posts_per_page' => '-1', 'post_status' => 'publish']);
$the_query->the_post();
while ($the_query->have_posts()) {
    $urlList[] = get_permalink();
    $the_query->the_post();
}

$categories = get_categories();
foreach ($categories as $category) {
    $catUrl = get_category_link($category->term_id);
    $urlList[] = $catUrl;
    $the_query = new WP_Query(['cat' => $category->term_id]);
    warmup_pages_links($the_query->max_num_pages, $catUrl);
}

$tags = get_tags();
foreach ($tags as $tag) {
    $tagUrl = get_tag_link($tag->term_id);
    $urlList[] = $tagUrl;
    $the_query = new WP_Query(['tag_id' => $tag->term_id]);
    warmup_pages_links($the_query->max_num_pages, $tagUrl);
}

$authors = get_users(array('who' => 'authors'));
foreach ($authors as $author) {
    $authorUrl = get_author_posts_url(false, $author->user_nicename);
    $urlList[] = $authorUrl;
    $the_query = new WP_Query(['author_id' => $author->term_id]);
    warmup_pages_links($the_query->max_num_pages, $authorUrl);
}

//test search page
$urlList[] = get_search_link('bala la lam');

file_put_contents('urllist.txt', implode("\n", $urlList));

echo "Found " . count($urlList) . " URLS and saved to urlList.txt \n";
echo "Testing URLs \n";
foreach ($urlList as $url) {
    warmup_getUrl($url);
}
echo "\nWarmup done \n";

function warmup_getUrl($url)
{
    if ($contents = wp_remote_get($url, ['timeout' => 15])) {
        if (is_wp_error($contents) || (isset($contents['response']['code']) && $contents['response']['code'] != 200)) {
            $subject = "[WP " . get_bloginfo('name') . "] Error on {$url} ";
            $error = is_wp_error($contents) ? $contents->get_error_message() : print_r($contents, true);
            wp_mail(get_bloginfo('admin_email'), $subject, $error);
            die("\n{$subject} :" . $error);
        }
        echo ".";
    }
    return $contents;
}

function warmup_pages_links($total, $url)
{
    for ($i = 2; $i < = $total; $i++) {
        $GLOBALS['urlList'][] = "{$url}page/{$i}/";
    }
}

// siege -c200 -d1 -r30 -f urllist.txt

install memcached 1.4.13 on CentOS 6.2

– Make sure to check PHP-FPM and MYSQL posts.

– One of the dependencies of memcache is libevent, get latest version from libevent.org

mkdir libevent
cd libevent
wget https://github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
tar xfz libevent-2.0.17-stable.tar.gz
cd libevent-2.0.17-stable

– get latest version from memcached.org

Ban IP from logging in for 5 minutes after 10 failed logins


if( login_limit() )
   die( 'Your IP has been banned from logging in for the next 5 minutes' );



/*
 * Counts login times by same IP 
 * returns true if limit reached or false if not 
 */
function login_limit(){
    //get real IP if user behind proxy noted by Sebastian Enger
    $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    
    //user still banned from login
    if( _cache( $ip . 'banned' ) )
        return true;

    //number of seconds 
    $sec = 30;
    //find ip info array in cache saved less than $sec ago
    if( ($ip_info=_cache( $ip ))  && $ip_info[0] > time()-$sec ){
        
        //user login 10 times during last $sec 
        if(  $ip_info[1] > 10 ){
            //ban user ip for the next 5 minutes
            _cache( $ip . 'banned', 1, 0, 60*5 );
            _cache( $ip, -1 );
            return true;
        }
        //increase login retries +1 
        _cache( $ip, array(  $ip_info[0],   ++$ip_info[1] ), 0, $sec );
        return false;
        
    }
        
    //add ip info to cache
    _cache( $ip, array( time(), 1 ), 0, $sec );
    return false;
}

function _cache( $name, $val=NULL, $ttl=false ){
        //memcached
	global $mcdb;
	if(empty($mcdb) ) 
		$mcdb = memcache_connect('unix:///etc/sockets/memcached.sock', 0);
	
	if($val === -1){
		return memcache_delete($mcdb,$name);
	}elseif( $val !== NULL ){
		return memcache_set($mcdb,$name,$val, false, $ttl);
	}else{
		$retval = memcache_get($mcdb,$name);
		return  $retval ? $retval : NULL;
	}

}

Dynamic Content Caching using Lighty + mod_magnet + lua

*Updated 11 Jan 2007 to include luazlib, fix lighty config file and add zlib decoding to cache.lua for older browsers
Since I read the documentations for mod_cml, I was very excited to use this module since caching using PHP running as FastCGI is not helping much during server peak load. However, mod_cml was replaced by mod_magnet which is more flexible and gives more control over request handling in Lighttpd. This Article will focus on caching your PHP scripts using Lua and mod_magnet under Lighttpd,