Transient caching for wp query

I read about transient in the codex and some questions and answers here.

So i’m developing a web app based on wp. It works now, but i want to optimize it. Users manage content regulary, so crud operations is common. Due to this i don’t want to use page caching with expirations.

I want to implement some caching functions on “get” operations.
This is in my mind:

  1. a user add some content, populate a list
  2. i create a transient “record” for that list object, and for the item itself
  3. when the list changes (update, delete, create) i delete the previous cache record and add the new one

Mainly i use WP_Query (posts and meta).
Questions:

Do i need this caching method, or using transient cache with wp_query won’t take a large effet?

My other concern to put all results into another table feel a bit irrelevant..?!

1 Answer
1

For my WordPress testimonials widget plugin I’ve successfully used transients with expiration dates to drop page loads by 0.1 to 0.5 seconds.

Using WordPress’s own transients is a life-saver. It’s so much easier to package or use straight as needed and then concentrate again on application logic. Rolling yet another caching system is useless. Further, as you get into using caching plugin like W3 Total Cache and WP Super Cache, there’s additional caching tie-ins that’ll speed things further along.

At this time, expired transients do fill up your wp_options table. Future versions of WordPress will better handle transient cleanup. For now, check https://gist.github.com/ScottPhillips/2907732 for scheduled transient purging or roll it into your application.

Since I do free and premium plugins, I’ve abstracted the caching operations, but there’s no reason why you couldn’t move my abstractions into your own work.

While in your case, you’re talking about users creating data, you could think of that data creation being the widget or shortcode options being passed into testimonialswidget_list as $atts in below.

The $content = apply_filters( 'testimonials_widget_cache_get', false, $atts ); is checking that there’s a current cache entry for the given $atts. As such, if the incoming $atts have changed, then the caching will fail and false is returned so that a new set of data is created.

Line $content = apply_filters( 'testimonials_widget_cache_set', $content, $atts ); is where our newly generated data is saved back to the cache for quick, easy reuse.

The data grabbing from testimonials-widget.php.

public function testimonialswidget_list( $atts ) {
    self::add_instance();

    $atts = wp_parse_args( $atts, self::get_defaults() );
    $atts = Testimonials_Widget_Settings::validate_settings( $atts );

    if ( get_query_var( 'paged' ) ) {
        $atts['paged'] = get_query_var( 'paged' );
    } elseif ( get_query_var( 'page' ) ) {
        $atts['paged'] = get_query_var( 'page' );
    } else {
        $atts['paged'] = 1;
    }

    $atts['type'] = 'testimonialswidget_list';

    $content = apply_filters( 'testimonials_widget_cache_get', false, $atts );

    if ( false === $content ) {
        $testimonials = self::get_testimonials( $atts );
        $content      = self::get_testimonials_html( $testimonials, $atts );
        $content      = apply_filters( 'testimonials_widget_cache_set', $content, $atts );
    }

    return $content;
}

The caching operations

add_filter( 'testimonials_widget_cache_get', array( $this, 'cache_get' ) );
add_filter( 'testimonials_widget_cache_set', array( $this, 'cache_set' ), 10, 2 );
public static function cache_get( $args ) {
    $hash     = self::create_hash( $args );
    $do_cache = apply_filters( 'testimonials_widget_disable_cache', true );
    $no_cache = isset( $args['no_cache'] ) && Testimonials_Widget_Settings::is_true( $args['no_cache'] );
    if ( ! $do_cache || $no_cache ) {
        delete_transient( $hash );
        return false;
    }

    $data = get_transient( $hash );

    return $data;
}


public static function cache_set( $data, $args ) {
    $hash = self::create_hash( $args );
    set_transient( $hash, $data, self::$cache_period );

    return $data;
}

Note The code above are scraps. Copying and pasting into your own application will not work right off. I would suggest looking at where self:: and Testimonials_Widget_Settings:: is used as probable points to alter code to suit your needs.

Leave a Comment