Created
August 19, 2013 22:12
-
-
Save PeterBooker/6274802 to your computer and use it in GitHub Desktop.
This functions fetches and refreshes the data if needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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