WordPress.org

Ready to get started?Download WordPress

Forums

Contextual Related Posts
[resolved] [PATCH] Cache results for faster lookup (4 posts)

  1. flynsarmy
    Member
    Posted 1 year ago #

    I didn't want to do the potentially burdonsome SQL query on every page visit from every customer so I've added caching support. Cache is generated after the first time a post if viewed and is invalidated whenever there's a CRP settings update. Cache is saved in postmeta table.

    Firstly the actual caching logic. In /contextual-related-posts.php function get_crp():
    Below

    $crp_settings = crp_read_options();

    add

    //Support caching to speed up retrieval
    if ( !empty($crp_settings['cache']) )
    {
    	$output = get_post_meta($post->ID, 'crp_related_posts', true);
    	if ( $output )
    		return $output;
    }

    and at the end of the method above

    return $output;

    add

    //Support caching to speed up retrieval
    if ( !empty($crp_settings['cache']) )
    	update_post_meta($post->ID, 'crp_related_posts', $output, '');

    Add the default setting. In function crp_default_options() add

    'cache' => false,			// Cache output for faster page load

    below

    'add_to_archives' => false,		// Add related posts to other archives

    Now for the settings page.

    In /admin.inc.php function crp_options() Add

    $crp_settings['match_content'] = (isset($_POST['match_content']) ? true : false);

    below

    $crp_settings['match_content'] = (isset($_POST['match_content']) ? true : false);

    Above

    <tr style="vertical-align: top;"><th scope="row"><label for="limit"><?php _e('Number of related posts to display: ',CRP_LOCAL_NAME); ?></label></th>

    add

    <tr style="vertical-align: top;"><th scope="row"><label for="cache"><?php _e('Cache output (Invalidated on settings update)',CRP_LOCAL_NAME); ?></label></th>
    	<td><input type="checkbox" name="cache" id="cache" <?php if ($crp_settings['cache']) echo 'checked="checked"' ?> /> <?php _e(' <em>Optional</em>',CRP_LOCAL_NAME); ?></td>
    </tr>

    Handle invalidation. Above

    $str = '<div id="message" class="updated fade"><p>'. __('Options saved successfully.',CRP_LOCAL_NAME) .'</p></div>';

    Add

    delete_post_meta_by_key('crp_related_posts');

    http://wordpress.org/extend/plugins/contextual-related-posts/

  2. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks for this patch! I'm definitely including this into the next version of the plugin.

  3. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    I have implemented this in v1.8.6

  4. flynsarmy
    Member
    Posted 1 year ago #

    Sweet :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.