Skip to content

Instantly share code, notes, and snippets.

@PeterBooker
Created August 19, 2013 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBooker/6274802 to your computer and use it in GitHub Desktop.
Save PeterBooker/6274802 to your computer and use it in GitHub Desktop.
This functions fetches and refreshes the data if needed.
<?php
function pb_get_news() {
/*
* Get transient and if expired refresh data immediately
*/
if ( false === ( $news = get_transient( 'pb_bbc_news' ) ) ) {
// Uses BBC News API to fetch new data
$news = pb_refresh_news();
}
/*
* Check if News has soft expired, if so run refresh after page load.
*/
elseif ( $news->expiry < time() ) {
// Add 60 seconds to soft expire, to stop other threads trying to update it at the same time.
$news->expiry = ( time() + 60 );
/*
* Update soft expire time.
*/
set_transient( 'pb_bbc_news', $news, 24 * HOUR_IN_SECONDS );
// Set the refresh function to run just before PHP shutsdown.
add_action( 'shutdown', 'pb_refresh_news' );
}
return $news;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment