#WCLVPA @salcode

Getting Started with Transients

Sal Ferrarello / @salcode

How Long Until We Get There ?

Caching

What would you like for Breakfast ?

Current Temperature ?

Dark Sky API

https://api.darksky.net/forecast/abc/40.6,-75.4
Part of a Dark Sky API JSON Data Response
$response = wp_remote_get( $url );
$body     = wp_remote_retrieve_body( $response );
$weather  = json_decode( $body );
$temp     = $weather->currently->temperature;
function fe_get_lv_temp_api_call() {


  $response = wp_remote_get( $url );
  $body     = wp_remote_retrieve_body($response);
  $weather  = json_decode( $body );
  $temp     = $weather->currently->temperature;

  return $temp;
}
function fe_get_lv_temp_api_call() {

  // This is slow!
  $response = wp_remote_get( $url );
  $body     = wp_remote_retrieve_body($response);
  $weather  = json_decode( $body );
  $temp     = $weather->currently->temperature;

  return $temp;
}
function fe_get_lv_temp_f() {




  $temp = fe_get_lv_temp_api_call(); // SLOW.



  return $temp;
}
function fe_get_lv_temp_f() {




  $temp = fe_get_lv_temp_api_call(); // SLOW.

  set_transient('fe_lv_temp', $temp, 1800 );

  return $temp;
}
function fe_get_lv_temp_f() {

  $temp = get_transient( 'fe_lv_temp' );


  $temp = fe_get_lv_temp_api_call(); // SLOW.

  set_transient('fe_lv_temp', $temp, 1800 );

  return $temp;
}
function fe_get_lv_temp_f() {

  $temp = get_transient( 'fe_lv_temp' );
  if ( false !== $temp ) { return $temp; }

  $temp = fe_get_lv_temp_api_call(); // SLOW.

  set_transient('fe_lv_temp', $temp, 1800 );

  return $temp;
}
set_transient(
	$transient_key, // Unique key.
	$value,         // Value to cache.
	$max_time       // Max time in seconds to keep.
);
get_transient( $transient_key );
// Returns the cached value or false.

Query Monitor Plugin

Query Monitor 0.78s Execution Time - no transient
Query Monitor 0.20s Execution Time - with transient
Query Monitor Showing a (1) for Transients Set

Database Queries

Too much of a good thing

Top Tags Plugin

Before Top Tags plugin

  • 29 database queries
  • 0.0013 Database query time

Without Transient

  • 66 database queries
  • 0.0028 Database query time
function fe_top_tags_shortcode() {




  $output = fe_get_terms_posts_markup(); // SLOW.



  return $output;
}
function fe_top_tags_shortcode() {




  $output = fe_get_terms_posts_markup(); // SLOW.

  set_transient('fe_tts_output', $output, 86400);

  return $output;
}
function fe_top_tags_shortcode() {

  $output = get_transient( 'fe_tts_output' );
  if ( false !== $output ) { return $output; }

  $output = fe_get_terms_posts_markup(); // SLOW.

  set_transient('fe_tts_output', $output, 86400);

  return $output;
}

With Transient

  • 31 database queries
  • 0.0014 Database query time
# Queries DB Time
Before 29 .0013
without Transient 66 .0028
with Transient 31 .0014

Outdated Information

a.k.a. stale data

add_action( 'save_post',
	'fe_tts_clear_transient', 10, 2 );

function fe_tts_clear_transient($post_id, $post) {





}
add_action( 'save_post',
	'fe_tts_clear_transient', 10, 2 );

function fe_tts_clear_transient($post_id, $post) {

	if ( $post && 'post' === $post->post_type ) {
		delete_transient( 'fe_tts_output' );
	}

}

When I Use Transients

  • Remote API Calls
  • Slow database queries

When I Don't Use Transients

When I Can't Recreate the Data

WordPress Time Constants

$max_time = 1800;

$max_time = 30 * MINUTE_IN_SECONDS;


$max_time = 1 * DAY_IN_SECONDS;

Transients Manager Plugin

Prefer Data Over HTML

TLC Transients

  • support for soft-expiration
  • background updating of the transients

Iron Code Studio

Sal Ferrarello

salcode.com/wordpress-transients

@salcode         #WCLVPA

Image Credits