Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Created September 18, 2017 21:04
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 chuckreynolds/ed5b6aa52ca83abf3d89529e998a3ede to your computer and use it in GitHub Desktop.
Save chuckreynolds/ed5b6aa52ca83abf3d89529e998a3ede to your computer and use it in GitHub Desktop.
Sample WordPress options with time-based cache flag
<?php
function get_twitter_followers() {
$ttl = 2 * HOUR_IN_SECONDS;
$cache = get_option( 'my_twitter_followers' );
if ( empty( $cache['timeout'] ) || $cache['timeout'] < time() ) {
$followers = wp_remote_get( ... );
$cache = array(
'count' => $followers,
'timeout' => time() + $ttl,
);
update_option( 'my_twitter_followers', $cache );
}
return $cache['count'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment