On Github tollmanz / wcyvr-2013
Zack Tollman
The Theme Foundry
@tollmanz
Page Caching is Lovely
Page Caching is Limiting
github.com/dashboard/issues
Page caching is effective
but cannot work in these scenarios
Expensive
Hard to scale
Partial page caching
using object caching
<div id="latest-activity">
<?php
$activity = wp_cache_get( 'latest_activity' );
if ( false === $activity ) {
$activity = get_latest_activity();
wp_cache_set( 'latest_activity', $activity, '', 3600 );
}
echo $activity;
?>
</div>
Cache user based variants
<div id="latest-activity">
<?php
$user_id = get_current_user_id();
$activity = wp_cache_get( 'latest_activity', $user_id );
if ( false === $activity ) {
$activity = get_latest_activity();
wp_cache_set( 'latest_activity', $activity, $user_id, 3600 );
}
echo $activity;
?>
</div>
TLC transients
Fragment caching class by Jaquith
Pods framework
Works well
Easy to understand
Always load all of WordPress
Not very performant
Daniel Dvorkin
advanced-cached.php implements a page cache for WordPress
Loaded after 1% of WordPress is loaded
<div id="latest-activity">
<nocache callback="get_latest_activity"></nocache>
</div>
If metadata does not exist, strip the tags and display
If metadata exists, generate the block
<div id="latest-activity">
<nocache is_user_logged_in callback="get_latest_activity">
<p>Activity feed is not available.</p>
</nocache>
</div>
Uses core standards
"callback" method is may present problems
Partial Page Templating
Embed ESI tags in the template
<div id="latest-activity">
<?php
// Loads partial-activity.php
get_template_part( 'partial', 'activity' );
?>
</div>
<?php if ( isset( $_GET['partial'] ) && 'activity' === $_GET['partial'] ) : ?>
<?php echo get_latest_activity(); ?>
<?php else : ?>
<esi:remove>
<p>Activity feed is not available.</p>
</esi:remove>
<!--esi
<esi:include src="<?php echo site_url( '/?partial=activity' ); ?>" />
-->
<?php endif; ?>
<div id="latest-activity">
<esi:remove>
<p>Activity feed is not available.</p>
</esi:remove>
<!--esi
<esi:include src="<?php echo site_url( '/?partial=activity' ); ?>" />
-->
</div>
function zdt_page_template_redirect() {
if ( isset( $_GET['partial'] ) ) {
$sanitize_partial = sanitize_key( $_GET['partial'] );
get_template_part( 'partial', $sanitize_partial );
exit();
}
}
add_action( 'template_redirect', 'zdt_page_template_redirect' );
<?php if ( isset( $_GET['partial'] ) && 'activity' === $_GET['partial'] ) : ?>
<?php echo get_latest_activity(); ?>
<?php else : ?>
<esi:remove>
<p>Activity feed is not available.</p>
</esi:remove>
<!--esi
<esi:include src="<?php echo site_url( '/?partial=activity' ); ?>" />
-->
<?php endif; ?>
Adheres to WordPress standards
Many partials can lead to many WordPress loads
There is no right answer
There are a lot of wrong answers
Each app needs its own technique
Zack Tollman
The Theme Foundry
@tollmanz
tollmanz.github.io/wcyvr-2013
tollmanz.com/partial-page-templating-in-wordpress