Created
May 28, 2012 23:32
-
-
Save tollmanz/2821651 to your computer and use it in GitHub Desktop.
An Example Cache Refresh Function
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 ohsc_get_ratings( $prime_cache = false ) { | |
$movies = array( | |
'The Usual Suspects', | |
'Braveheart', | |
'The King\s Speech' | |
); | |
$cache_key = 'ohsc-' . serialize( $movies ); | |
// Check the cache for the movies | |
$ratings = get_transient( $cache_key ); | |
if ( false === $ratings || $prime_cache ) { | |
// This function queries the API and gets the 3 movie's data | |
$imdb_movies = ohsc_get_movies( $movies ); | |
$ratings = array(); | |
foreach ( $imdb_movies as $movie ) | |
$ratings[$movie->Title] = $movie->Rating; | |
// Add the ratings to the cache | |
if ( ! empty( $ratings ) ) | |
set_transient( $cache_key, $ratings ); | |
else | |
set_transient( $cache_key, 'no-data' ); | |
} | |
return $ratings; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment