Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a problem with preventing the web clients from caching my scrolling graph and would like to seek your helps. Attached below is a piece of my code. Basically, the code would display a continuous scroll graph. However, the second time I ran the code, the same image is redisplayed on the client screen. If I clicked on refresh, then I would see a new image. I searched the web and the examples I found are similar to this

print "Content-type: image/gif\n\n"; print "Cache-control: no-cache\n"; print "Pragma: no-cache\n"; print "Expires: Mon, 06 May 1996 04:57:00 GMT\n";

However, when I put this into my code, I got an error about incorrect format in my image and nothing is display on the client window. I had to move the

print "Content-type: image/gif\n\n";

below the other three lines to get the error message to go away but then I have a caching problem. Any helps would be appreciated. Thanks.

my @data = ( \@xlabel, \@reads, \@writes, \@updates ); my $graph = GD::Graph::lines->new(400,300); $graph->set( x_label => 'Average Read/Write/Update', y_label => 'Performance ms', title => 'Performance Monitor', y_min_value => 0, y_max_value => $ysp, y_tick_number => 5, y_label_skip => 'auto', x_label_skip => 10, box_axis => 0, ) or die $graph->error; $graph->set_legend( 'Read/Item', 'Write/Item', 'Update/Item'); my $myimage = $graph->plot(\@data); print "Cache-control: no-cache\n"; print "Pragma: no-cache\n"; print "Expires: Mon, 06 May 1996 04:57:00 GMT\n"; print "Content-type: image/gif\n\n"; print $myimage->gif; print "\n"; print "\n--magicalboundarystring\n";

Replies are listed 'Best First'.
Re: Preventing Web Client From Caching
by wind (Priest) on Aug 04, 2007 at 02:39 UTC
    A blank line is used to separate the header from the content in an HTTP response. Therefore simply move the blank return to the end of the header, instead of embedded in the Content-type print statment:
    print "Content-type: image/gif\n"; print "Cache-control: no-cache\n"; print "Pragma: no-cache\n"; print "Expires: Mon, 06 May 1996 04:57:00 GMT\n"; print "\n"; # End Header
    - Miller
      Thanks, this format works since I don't see the error message but my Firefox is still not updating the graph. Did I forget any other options?
        Just to make sure the obvious is covered, do you have a <meta http-equiv=refresh content=60> or similar tag in the <head> of the HTML document? Disabling caching may tell the browser to always reload the page when asked to, but you still need to ask it to reload before it will do so.
Re: Preventing Web Client From Caching
by derby (Abbot) on Aug 04, 2007 at 17:18 UTC

    Looking at the moz dev center it appears your header should be:

    print "cache-control: no-store\n";
    Some googling suggests firefox is a bit sticky about the case too.

    -derby
Re: Preventing Web Client From Caching
by planetscape (Chancellor) on Aug 04, 2007 at 20:21 UTC