Skip to content

Commit

Permalink
Merge pull request #542 from Automattic/update/improve-caching-of-fut…
Browse files Browse the repository at this point in the history
…ure-timestamps

Cache future timestamps for a short time
  • Loading branch information
nickdaugherty authored Nov 1, 2018
2 parents 34e1eab + a7f9c9e commit b2d70c9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions liveblog.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class WPCOM_Liveblog {
public static $post_id = null;
private static $entry_query = null;
private static $do_not_cache_response = false;
private static $cache_control_max_age = null;
private static $custom_template_path = null;

public static $is_rest_api_call = false;
Expand Down Expand Up @@ -429,9 +430,11 @@ public static function get_entries_by_time( $start_timestamp, $end_timestamp ) {
$latest_timestamp = null;
$entries_for_json = array();

// Do not cache if it's too soon
if ( $end_timestamp > time() ) {
self::$do_not_cache_response = true;
$now = time();

// If end timestamp is in future, set a cache TTL until it's not
if ( $end_timestamp > $now ) {
self::$cache_control_max_age = $end_timestamp - $now;
}

if ( empty( self::$entry_query ) ) {
Expand Down Expand Up @@ -1680,6 +1683,9 @@ public static function protect_liveblog_meta_key( $protected, $meta_key ) {
public static function prevent_caching_if_needed() {
if ( self::$do_not_cache_response ) {
nocache_headers();
} else if ( self::$cache_control_max_age ) {
header( 'Cache-control: max-age=' . self::$cache_control_max_age );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s \G\M\T', time() + self::$cache_control_max_age ) );
}
}

Expand Down

0 comments on commit b2d70c9

Please sign in to comment.