Forums

[resolved] Manually refreshing Wp-cache (1 post)

  1. zxb2
    Member
    Posted 4 years ago #

    I am by no means a PHP expert, but what I would like to do is manually refresh a certain posts cache when a comment is inserted.

    I realize Wp-cache already does this. However, my comment system is separate from WordPress and is integrated into a forum.

    Looking at the WP-cache source I saw this function:

    function wp_cache_post_change($post_id) {
    	global $file_prefix;
    	global $cache_path;
    	global $blog_id;
    	static $last_processed = -1;
    
    	// 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";
    				$meta = unserialize(@file_get_contents($meta_pathname));
    				if ($post_id > 0 && $meta) {
    					if ($meta->blog_id == $blog_id  && (!$meta->post || $meta->post == $post_id) ) {
    						unlink($meta_pathname);
    						unlink($content_pathname);
    					}
    				} elseif ($meta->blog_id == $blog_id) {
    					unlink($meta_pathname);
    					unlink($content_pathname);
    				}
    
    			}
    		}
    		closedir($handle);
    	}
    	wp_cache_writers_exit();
    	return $post_id;
    }

    Basically can I just give that function a post ID and it'll update the cache for the specified post?

    I'm going to setup a local server and try it, but I wondered if anyone here did the same and could guide me on this.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags