Force WP-Cache to preserve home page
-
This is an ugly hack and a request to help make it better.
The story: If you are getting really slammed with traffic, you get WP-Cache. And it works very well.
But if you are ALSO getting a ton of comments, WP-Cache is less helpful because each new comment clears your cache. I wanted a way to KEEP my cache FOR THE FRONT PAGE — even if a new comment was submitted (but still clear it if a new post is posted/edited).
Here’s one way: in wp-cache-phase2.php, replace the functions wp_cache_get_postid_from_comment with this:
function wp_cache_get_postid_from_comment($comment_id) {
$comment = get_commentdata($comment_id, 1, true);
$postid = $comment['comment_post_ID'];
// We must check it up again due to WP bugs calling two different actions
// for delete, for example both wp_set_comment_status and delete_comment
// are called whene deleting a comment
if ($postid > 0)
return wp_cache_post_change($postid, true); // second parameter protects home page (wbh)
else
return wp_cache_post_change(wp_cache_post_id(), true);// second parameter protects home page (wbh)
}
And wp_cache_post_change with this:
// second parameter protects home page (wbh)
function wp_cache_post_change($post_id, $savehome = false) {
global $file_prefix;
global $cache_path;
static $last_processed = -1;
// set this to something that will match your front page (wbh)
$front_page_pattern = "/blog/index.php/";
// Avoid cleaning twice the same pages
if ($post_id == $last_processed) return $post_id;
$last_processed = $post_id;
$meta = new CacheMeta;
$matches = array();
wp_cache_writers_entry();
if ( ($handle = opendir( $cache_path )) ) {
while ( false !== ($file = readdir($handle))) {
if ( preg_match("/^($file_prefix.*).meta/", $file, $matches) ) {
$meta_pathname = $cache_path . $file;
$content_pathname = $cache_path . $matches[1] . ".html";
if ($post_id > 0 && ($meta = unserialize(@file_get_contents($meta_pathname))) ) {
if (!$savehome || !preg_match($front_page_pattern, $meta->uri)) // protect home page (wbh)
{
if ( !$meta->post || $meta->post == $post_id ) {
unlink($meta_pathname);
unlink($content_pathname);
}
}
} else {
// wbh: this if-then just to protect home page
if (!$savehome || !preg_match($front_page_pattern, $meta->uri)) // protect home page (wbh)
{
unlink($meta_pathname);
unlink($content_pathname);
}
}
}
}
closedir($handle);
}
wp_cache_writers_exit();
return $post_id;
}
You have to tweak the line after SET THIS (which will be line 265).
Ugliest hack ever. Not for the timid. But if you are facing a slashdot level of traffic and comments, it will help.
I need a way to dynamically and reliably figure out what the REQUEST_URI of someone’s front page is.
- The topic ‘Force WP-Cache to preserve home page’ is closed to new replies.