Skip to content

Instantly share code, notes, and snippets.

@yumyo
Last active June 21, 2017 13:27
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 yumyo/485b1135679caad50e2f928747ac38d8 to your computer and use it in GitHub Desktop.
Save yumyo/485b1135679caad50e2f928747ac38d8 to your computer and use it in GitHub Desktop.
An alternative to transient and object-cache
$cache_key = '_ssr-schedule';
$cache = get_post_meta( $frag_ID, $cache_key, true );
if ( empty( $cache ) || $cache['expires'] < time() ) {
$var = '[path]';
$url = '[path]' . $var;
$request = wp_remote_get( $url, array( 'timeout' => 120, ) );
if( is_wp_error( $request ) ) { return false; }
$response = wp_remote_retrieve_body( $request );
$data = json_decode( $response, true ); // `true` translate into an array instead of an object
$cache = array(
'expires' => time() + 24 * HOUR_IN_SECONDS,
'data' => $data,
);
update_post_meta( $post->ID, $cache_key, $cache );
}
$data = $cache['data'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment