#WCPhilly @salcode

Getting Started with Transients

Sal Ferrarello / @salcode

salcode.com/wcphilly19

Jump to resources

What's the temperature?

Caching

Current Temperature on Your Website

Dark Sky
Web API

Web API ?

Website for a Computer

Web API ?

Website for a Computer

Dark Sky
Website for a Computer

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



  $response = wp_remote_get( $url );
  $body     = wp_remote_retrieve_body($response);


  $response = wp_remote_get( $url );
  $body     = wp_remote_retrieve_body($response);
  $weather  = json_decode( $body );

  $response = wp_remote_get( $url );
  $body     = wp_remote_retrieve_body($response);
  $weather  = json_decode( $body );
  $temp     = $weather->currently->temperature;
Part of a Dark Sky API JSON Data Response displayed nicely formatted.
Website displaying current temperature.
  $response = wp_remote_get( $url );
  $body     = wp_remote_retrieve_body($response);
  $weather  = json_decode( $body );
  $temp     = $weather->currently->temperature;
function fe_get_wp_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_wp_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_wp_temp_f() {




  $temp = fe_get_wp_temp_api_call(); // SLOW.



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




  $temp = fe_get_wp_temp_api_call(); // SLOW.



  return $temp;
}
function fe_get_wp_temp_f() {




  $temp = fe_get_wp_temp_api_call(); // SLOW.

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

  return $temp;
}
function fe_get_wp_temp_f() {

  $temp = get_transient( 'fe_wp_temp' );


  $temp = fe_get_wp_temp_api_call(); // SLOW.

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

  return $temp;
}
function fe_get_wp_temp_f() {

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

  $temp = fe_get_wp_temp_api_call(); // SLOW.

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

  return $temp;
}
set_transient(
	$transient_key, // Unique key (max length 172).
	$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 WordPress plugin toolbar dropdown menu.
Query Monitor toolbar with 1 HTTP API Calls item.
Query Monitor toolbar with HTTP API Calls details.
Query Monitor toolbar with 1 Transient Updates item.
Query Monitor toolbar with Transient Updates details.
Query Monitor toolbar with no HTTP API Calls nor Transient Updates.

How much faster?

Getting a transient is approximately 1,000 times faster than a HTTP API Call.

How are Transients Stored

  • In the options table (default)
  • Persistent Object Cache (Redis, Memcached)

Transients in Options Table

Stores two values

  • Value to be stored (e.g. temp)
  • Timeout expiration as unix time stamp

Unix Time Stamp

Number of seconds since midnight January 1, 1970 UTC

e.g. 1570312800

Transients in Options Table

  • _transient_{transient_key}
  • _transient_timeout_{transient_key}

Transients in Options Table

  • _transient_fe_wp_temp 
  • _transient_timeout_fe_wp_temp     

Our Transients in the Options Table

_transient_fe_wp_temp 59
_transient_timeout_fe_wp_temp 1570308600

Transients Manager Plugin

Transients Manager wp-admin page.

Expired Transients

Expired transients are deleted by get_transient()

Abandoned Transients

Abandoned transients are never deleted.

Transients Manager Plugin

Transients Manager 'Delete Expired Transients' button.

Delete Transients

on Plugin Deactivation

Transients Manager 'Suspend Transients' button.

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;

Prefer Data Over HTML

Summary

WebDevStudios

Sal Ferrarello

salcode.com/wcphilly19

Resources

Image Credits