49
Transients are good for you (and for your website) WordCamp London 2016 Julio Potier WP Media Co-Founder wp-rocket.me & imagify.io

Transients are good for you - WordCamp London 2016

Embed Size (px)

Citation preview

Transients are good for you (and for

your website)WordCamp London 2016

Julio Potier

WP MediaCo-Founder

wp-rocket.me&

imagify.io

What is it?Why?

When?Where?How?Help!!

What is it?Why?

When?Where?How?Help!!

/ˈtræn.zɪənt/

transient : common name,• Temporary thing.

/ˈtræn.zɪənt/

The Transients API offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted.Source: codex.w.org/Transients_API

The Transients API offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted.Source: codex.w.org/Transients_API

/ˈtræn.zɪənt/

- Not the same as an option,- Not only in database,- Timeout not mandatory,- Can be deleted before timeout,- Still in database after expiration.

What is it?Why?

When?Where?How?Help!!

Storage system ?

Storage system

Performance gain=

Cache system!

What is it?Why?

When?Where?How?Help!!

The same content is presenton many pages.

A content comes froman external request.

Ex.: last comments, last posts in sidebar...

Ex.: your followers, last tweets ...

An expensive custom request.Ex.: a big request with joints in custom tables

Front-end

Display a message for a user.Ex.: return from a validation, custom error...

Back-end

Display a message for a user.Ex.: return from a validation, custom error...

Back-end

$delete_result = delete_plugins( $plugins );set_transient( 'plugins_delete_result_' . $user_ID, $delete_result );

$delete_result = get_transient( 'plugins_delete_result_' . $user_ID );delete_transient( 'plugins_delete_result_' . $user_ID );_e( 'The selected plugins have been <strong>deleted</strong>.' );

What is it?Why?

When?Where?How?Help!!

A few examples● Menus● Blogroll● Tag cloud● Last posts● Last comments● Any custom request

● Meteo● Radio● Last tweets● Friends/Followers● Last members● Popular posts

0.5/1h3/5mn1/24h24h24h12/24h

man.man.man.man.man. ?

Manual expiration? Automatic ? Large ? Short ?

Counter exampleDon't use on live data!

Counter exampleDon't use on live data!

What is it?Why?

When?Where?How?Help!!

DB or Object Cache?Transient caching

without objet cache

DB or Object Cache?Transient cachingwith objet cache

(MemCache)

DB or Object Cache?

Basic functions

set_transient()get_transient()delete_transient()

Basic functions

set_site_transient()get_site_transient()delete_site_transient()

All *_site_transient() functions are NOT functions that manage the multisite compatibility.

set_transient()

get_transient()

delete_transient()

Basic functions

set_transient( $transient, $value, $expiration=0)

Basic functions

set_transient( $transient, $value, $expiration=0)

Basic functions

set_transient( $transient, $value, $expiration=0)

_transient__transient_timeout_

Basic functions

set_transient( 'super_plugin_' . md5( $uniq_id ) );// _transient_timeout_super_plugin_d41d8cd98f00b204e9800998ecf8427e= 64 ! MAX !!

set_transient( 'super_plugin_' . date( 'dmy' ) );// super_plugin_160814, then tomorrow, will stay in DB forever.

set_transient( $transient, $value, $expiration=0)

Basic functionsStrings,Integers,Arrays,Objets,Serialized data.

Don't try with a SimpleXML Object !$xml = simplexml_load_file( $file );set_transient( 'xml_file', $xml ); // BOOM!

4 Gb max!

set_transient( $transient, $value, $expiration=0)

Basic functions- Lenght is in seconds, not a date- Max age and not an expiration guarantee.

If object cache, possibly deleted earlier.0 + no object cache = always in DB.Take care to AUTOLOAD!

set_transient()

get_transient()

delete_transient()

Basic functions

get_transient( $transient)

Basic functions

get_transient( $transient)

Basic functionsCheck with === false

The transient is only deleted now if its timeout as expired, otherwise it stays in the database.Attention this do not trigger this action hook:- "delete_transient_$transient" ;But:- "delete_option_transient_$transient",- "delete_option_transient_timeout_$transient",

Demo for get_transient()

// Without transientfunction wpmedia_get_my_data() {

$data = my_external_api_request();// Do something with $data.return $data;

}

Demo for get_transient()

// With transientfunction wpmedia_get_my_data() {

$data = get_transient( 'wpmedia_data' );if ( false === $data ) {

$data = my_external_api_request();set_transient( 'wpmedia_data', $data,

DAY_IN_SECONDS );} // Do something with $data.return $data;

}

Demo for get_transient()

// With persistant cachefunction wpmedia_get_my_data() {

$data = wp_cache_get( 'wpmedia_data', 'wpm' );if ( false === $data ) {

$data = my_external_api_request();wp_cache_set( 'wpmedia_data',

$data,'wpm',DAY_IN_SECONDS );} // Do something with $data.return $data;

}

Demo for get_transient()

// With transientfunction wpmedia_get_my_data() {

$data = get_transient( 'wpmedia_data' );if ( false === $data ) {

$data = my_external_api_request();set_transient( 'wpmedia_data', $data,

DAY_IN_SECONDS );} // Do something with $data.return $data;

}

Demo for get_transient()

// Bad boy!$transient = 'my_transient';$test_timeout = get_option( "_transient_timeout_$transient" );if ( $test_timeout > time() ) {

$data = get_option( "_transient_$transient" );}// ...

set_transient()

get_transient()

delete_transient()

Basic functions

delete_transient( $transient)

Basic functions

delete_transient( $transient)

Basic functions

Attention this is a false good idea:delete_option( "_transient_$transient" );It only works if you're not using an object cache!

Demo for delete_transient()add_action( 'wp_scheduled_delete',

'delete_expired_db_transients' );function delete_expired_db_transients() {

if ( wp_using_ext_object_cache() ) { // magic return; }

global $wpdb;

$time = isset( $_SERVER['REQUEST_TIME'] ) ? (int) $_SERVER['REQUEST_TIME'] : time(); $expired = $wpdb->get_col( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout%' AND option_value < {$time};" );

foreach ( $expired as $transient ) { $key = str_replace( '_transient_timeout_', '', $transient ); delete_transient( $key ); }

}// by @rarst - http://tinyurl.com/purge-transients

delete_transient( $transient)

Basic functions

Attention this is a false good idea:delete_option( "_transient_$transient" );It only works if you're not using an object cache!

Triggers this action hook:- "delete_transient_$transient" ;

What is it?Why?

When?Where?How?Help!!

Plugins!

http://wordpress.org/plugins/artiss-transient-cleaner/http://wordpress.org/plugins/delete-expired-transients/http://wordpress.org/plugins/transients-manager/http://wordpress.org/plugins/debug-bar-transients/

These slides on http://boiteaweb.fr/?p=8412

Thank you!Questions ?

Julio Potier

WP MediaCo-Founder

wp-rocket.me&

imagify.io