6. /ˈ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
7. 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.
13. The same content is present
on many pages.
A content comes from
an 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
14. Display a message for a user.
Ex.: return from a validation, custom error...
Back-end
15. 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>.' );
17. 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/1h
3/5mn
1/24h
24h
24h
12/24h
man.
man.
man.
man.
man.
?
Manual expiration? Automatic ? Large ? Short ?
36. get_transient(
$transient
)
Basic functions
Check 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",
37. Demo for get_transient()
// Without transient
function wpmedia_get_my_data() {
$data = my_external_api_request();
// Do something with $data.
return $data;
}
38. Demo for get_transient()
// With transient
function 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;
}
39. Demo for get_transient()
// With persistant cache
function 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;
}
40. Demo for get_transient()
// With transient
function 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;
}
41. 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" );
}
// ...