Skip to content

Commit

Permalink
Do not let the client cache future requests
Browse files Browse the repository at this point in the history
Imagine we have a client whose clock is in the future. Then when it
makes a request in which the end timestamp is in the future and we
cache it, the next client who makes a request for the same timestamp
will get the cached version. But since the cached data is much older
there is a chance meanwhile there will be changes and we will serve
the wrong data.
  • Loading branch information
nb committed Jul 4, 2012
1 parent 558008c commit 6c66757
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions liveblog.php
Expand Up @@ -40,6 +40,7 @@ class WPCOM_Liveblog {


static $post_id = null; static $post_id = null;
static $entries = null; static $entries = null;
static $do_not_cache_response = false;


function load() { function load() {
add_action( 'init', array( __CLASS__, 'init' ) ); add_action( 'init', array( __CLASS__, 'init' ) );
Expand Down Expand Up @@ -92,6 +93,10 @@ function handle_entries_ajax_request() {
wp_safe_redirect( get_permalink() ); wp_safe_redirect( get_permalink() );
} }


if ( $end_timestamp > $current_timestamp ) {
self::$do_not_cache_response = true;
}

$entries = self::$entries->get_between_timestamps( $start_timestamp, $end_timestamp ); $entries = self::$entries->get_between_timestamps( $start_timestamp, $end_timestamp );
if ( !$entries ) { if ( !$entries ) {
self::json_return( true, '', array( 'entries' => array(), 'current_timestamp' => $current_timestamp, 'latest_timestamp' => null ) ); self::json_return( true, '', array( 'entries' => array(), 'current_timestamp' => $current_timestamp, 'latest_timestamp' => null ) );
Expand Down Expand Up @@ -377,6 +382,9 @@ function json_return( $success, $message, $data = array() ) {
) ); ) );


header( 'Content-Type: application/json' ); header( 'Content-Type: application/json' );
if ( self::$do_not_cache_response ) {
nocache_headers();
}
echo $return; echo $return;
exit; exit;
} }
Expand Down

0 comments on commit 6c66757

Please sign in to comment.