Using wp_cache_switch_to_blog
-
Background: I’ve created an ‘index’ job for my WP network install which builds an SDF object of all the content on the network so it can be sent to Amazon’s CloudSearch service. Typically this works pretty well on a per-post basis; but sometimes I make a change that requires re-indexing an entire network. It doesn’t happen often but when I’m working on new features for our reporting interface, it is required. That’s where I start to run into performance problems.
Code:
add_action('as_rebuild_site_index', 'as_index_entire_site',10,1); function as_index_entire_site($blog_id){ wp_cache_flush(); switch_to_blog($blog_id); $args = array( 'posts_per_page' => -1, 'post_type' => array('event', 'news'), 'post_status' => 'publish', 'orderby' => 'ID' ); $lastposts = get_posts($args); foreach($lastposts as $post) : if(function_exists('as_add_post_to_index_queue')){ ///format as SDF and add to pending json queue as_add_post_to_index_queue($post->ID,false,'add'); } unset($post); endforeach; unset($lastposts); unset($blog_id); } //add all posts from everywhere to the queue add_action('as_rebuild_post_index', 'as_force_post_index_rebuild'); function as_force_post_index_rebuild(){ ini_set('memory_limit','1000M'); //testing only... dear god don't put this in the wild set_time_limit (600); //allow script to run for up to 10 min... delete_site_option('index_queue'); $blogs = wp_get_sites(array( 'limit' => 200, 'public' => 1, 'offset' => 4 //we dont want anything with ID =< 5 ) ); foreach ($blogs as $blog) { do_action('as_rebuild_site_index',$blog["blog_id"],$start_mem); unset($blog); } //end foreach restore_current_blog(); //using a single restore because we dont need to go back to the original site until the end unset($blogs); }
My network contains 100-110 sites with 2-3k posts and no matter how high I raise my memory or time limit, the script doesn’t finish. After debugging, the memory usage seems to take huge leaps upward after every
switch_to_blog
leading me to believe there is a bunch of caching happening.So, I came across this post about using
wp_cache_switch_to_blog
to toss out the site cache after each switch (since I don’t need to go back to a site after I’ve switched, there isn’t much benefit to keeping it in cache. I see a potential solution in that post but don’t fully understand how to implement it and the function isn’t very well documented.Can anyone shine some light on how I might be abel to more efficiently cycle through 100+ blogs without blowing up my memory? Is
wp_cache_switch_to_blog
the answer?
- The topic ‘Using wp_cache_switch_to_blog’ is closed to new replies.