Last active
June 21, 2017 13:27
-
-
Save yumyo/485b1135679caad50e2f928747ac38d8 to your computer and use it in GitHub Desktop.
An alternative to transient and object-cache
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
$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