Skip to content

Instantly share code, notes, and snippets.

@tollmanz
Created May 28, 2012 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tollmanz/2821651 to your computer and use it in GitHub Desktop.
Save tollmanz/2821651 to your computer and use it in GitHub Desktop.
An Example Cache Refresh Function
<?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