Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
WordPress Fragment Caching convenience wrapper
<?php
/*
Usage:
$frag = new CWS_Fragment_Cache( 'unique-key', 3600 ); // Second param is TTL
if ( !$frag->output() ) { // NOTE, testing for a return of false
functions_that_do_stuff_live();
these_should_echo();
// IMPORTANT
$frag->store();
// YOU CANNOT FORGET THIS. If you do, the site will break.
}
*/
class CWS_Fragment_Cache {
const GROUP = 'cws-fragments';
var $key;
var $ttl;
public function __construct( $key, $ttl ) {
$this->key = $key;
$this->ttl = $ttl;
}
public function output() {
$output = wp_cache_get( $this->key, self::GROUP );
if ( !empty( $output ) ) {
// It was in the cache
echo $output;
return true;
} else {
ob_start();
return false;
}
}
public function store() {
$output = ob_get_flush(); // Flushes the buffers
wp_cache_add( $this->key, $output, self::GROUP, $this->ttl );
}
}
@westonruter

This comment has been minimized.

Show comment Hide comment
@westonruter

westonruter Apr 28, 2013

@markjaquith It seems there could be a simpler API to do the same thing by using a wrapper function for a closure:

<?php
/*
Usage:
    cache_fragment_output( 'unique-key', 3600, function () {
        functions_that_do_stuff_live();
        these_should_echo();
    });
*/

function cache_fragment_output( $key, $ttl, $function ) {
    $group = 'fragment-cache';
    $output = wp_cache_get( $key, $group );
    if ( empty($output) ) {
        ob_start();
        call_user_func( $function );
        $output = ob_get_clean();
        wp_cache_add( $key, $output, $group, $ttl );
    }
    echo $output;
}

See fork: https://gist.github.com/westonruter/5475349

@markjaquith It seems there could be a simpler API to do the same thing by using a wrapper function for a closure:

<?php
/*
Usage:
    cache_fragment_output( 'unique-key', 3600, function () {
        functions_that_do_stuff_live();
        these_should_echo();
    });
*/

function cache_fragment_output( $key, $ttl, $function ) {
    $group = 'fragment-cache';
    $output = wp_cache_get( $key, $group );
    if ( empty($output) ) {
        ob_start();
        call_user_func( $function );
        $output = ob_get_clean();
        wp_cache_add( $key, $output, $group, $ttl );
    }
    echo $output;
}

See fork: https://gist.github.com/westonruter/5475349

@markjaquith

This comment has been minimized.

Show comment Hide comment
@markjaquith

markjaquith May 8, 2013

It seems there could be a simpler API to do the same thing by using a wrapper function for a closure

I considered that, but there are two problems:

  1. It requires PHP 5.3 (though, this is becoming less and less of an issue).
  2. It changes variable scope.

#2 is the big one. I wanted something that I could wrap around existing template code with zero changes to the code inside. If the code inside is using global variables, that will cease working when wrapped in a closure.

Owner

markjaquith commented May 8, 2013

It seems there could be a simpler API to do the same thing by using a wrapper function for a closure

I considered that, but there are two problems:

  1. It requires PHP 5.3 (though, this is becoming less and less of an issue).
  2. It changes variable scope.

#2 is the big one. I wanted something that I could wrap around existing template code with zero changes to the code inside. If the code inside is using global variables, that will cease working when wrapped in a closure.

@astrotim

This comment has been minimized.

Show comment Hide comment
@astrotim

astrotim Oct 11, 2013

What are the pros & cons of using WP object cache vs transients? http://codex.wordpress.org/Transients_API

What are the pros & cons of using WP object cache vs transients? http://codex.wordpress.org/Transients_API

@markjaquith

This comment has been minimized.

Show comment Hide comment
@markjaquith

markjaquith Sep 27, 2014

@astrotim transients will fall back to database caching on installs without a persistent object caching backend. You might want this (caching a feed) or you might not (caching 2000 query results).

Owner

markjaquith commented Sep 27, 2014

@astrotim transients will fall back to database caching on installs without a persistent object caching backend. You might want this (caching a feed) or you might not (caching 2000 query results).

@Mallinanga

This comment has been minimized.

Show comment Hide comment
@Mallinanga

Mallinanga Jan 23, 2015

@markjaquith Can we use this as a wrapper to an ajax callback?

For example, I have an ajax call that does expensive db queries and it returns the results as json for creating a graph via javascript.
I'm asking cause you state at the usage comment that the functions inside the wrapper should echo something.

Could this class be extended or modified to take into account also output that isn't echo'ed but returned?

@markjaquith Can we use this as a wrapper to an ajax callback?

For example, I have an ajax call that does expensive db queries and it returns the results as json for creating a graph via javascript.
I'm asking cause you state at the usage comment that the functions inside the wrapper should echo something.

Could this class be extended or modified to take into account also output that isn't echo'ed but returned?

@rask

This comment has been minimized.

Show comment Hide comment
@rask

rask Mar 23, 2015

Have you tested this on WP Multisite? Didn't see any references to multisite in the WP object cache API documentation.

rask commented Mar 23, 2015

Have you tested this on WP Multisite? Didn't see any references to multisite in the WP object cache API documentation.

@elvismdev

This comment has been minimized.

Show comment Hide comment
@elvismdev

elvismdev Jul 15, 2015

@markjaquith should I wrap add_action() on my genesis theme with this? e.g.

$frag = new CWS_Fragment_Cache( 'custom-header', 3600 ); // Second param is TTL
    if ( !$frag->output() ) { // NOTE, testing for a return of false

        add_action( 'genesis_header', 'my_custom_header' );

        // IMPORTANT
        $frag->store();
        // YOU CANNOT FORGET THIS. If you do, the site will break.
    }

@markjaquith should I wrap add_action() on my genesis theme with this? e.g.

$frag = new CWS_Fragment_Cache( 'custom-header', 3600 ); // Second param is TTL
    if ( !$frag->output() ) { // NOTE, testing for a return of false

        add_action( 'genesis_header', 'my_custom_header' );

        // IMPORTANT
        $frag->store();
        // YOU CANNOT FORGET THIS. If you do, the site will break.
    }
@danimalweb

This comment has been minimized.

Show comment Hide comment
@danimalweb

danimalweb Nov 10, 2016

I used this today and updated it to use transients API. Works like a charm. Thanks.

I used this today and updated it to use transients API. Works like a charm. Thanks.

@yumyo

This comment has been minimized.

Show comment Hide comment
@yumyo

yumyo Nov 14, 2016

How to clean individual fragments on, let's say, custom post type save/update?

yumyo commented Nov 14, 2016

How to clean individual fragments on, let's say, custom post type save/update?

@zTea90

This comment has been minimized.

Show comment Hide comment
@zTea90

zTea90 Jul 26, 2017

Hello, i was try but don't know why it not work.
Just show plant text :(

zTea90 commented Jul 26, 2017

Hello, i was try but don't know why it not work.
Just show plant text :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment