http://qs1969.pair.com?node_id=239698

This is really more of a HTTP- than Perl-related post; the following snippet emits all of the currently specified HTTP headers that may affect caching, with values set to coerce clients into not caching the document in question. It’s useful for debugging as well as generated pages that actually are very dynamic, but shouldn’t be used blindly for everything – caching is there for a reason.

(I’m posting this here mainly so as to have a node to refer people to as needed.)

Update 2006-01-12: added private and max-age=0 to the Cache-Control header.

use CGI qw(:standard); use POSIX qw(strftime); print header( # date in the past -expires => 'Sat, 26 Jul 1997 05:00:00 GMT', # always modified -Last_Modified => strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime), # HTTP/1.0 -Pragma => 'no-cache', # HTTP/1.1 + IE-specific (pre|post)-check -Cache_Control => join(', ', qw( private no-cache no-store must-revalidate max-age=0 pre-check=0 post-check=0 )), );

Replies are listed 'Best First'.
Re: overkill anti-caching CGI headers
by webratta (Sexton) on Mar 05, 2003 at 17:36 UTC
    Ohh... nice little snippet. Building on this, you can use this snippit in the cgiapp_prerun method of a CGI::Application module to have all of your run modes cache-free. :)
    package TestApp; use strict; use warnings; use base 'CGI::Application'; use POSIX; sub setup { my $self = shift; $self->start_mode('index'); $self->run_modes(index => 'TestAppIndex'); } sub cgiapp_prerun { my $self = shift; $self->header_props( # date in the past -expires => 'Sat, 26 Jul 1997 05:00:00 GMT', # always modified -Last_Modified => strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime +), # HTTP/1.0 -Pragma => 'no-cache', # HTTP/1.1 -Cache_Control => join(', ', qw( no-store no-cache must-revalidate post-check=0 pre-check=0 )), ); } sub TestAppIndex { return 'Hello, world!'; } 1;
Re: overkill anti-caching CGI headers
by cleverett (Friar) on Jul 23, 2003 at 05:42 UTC
    Nice bit. Get this, though:

    A local ISP has a transparent cache server which ignores HTTP tags completely. It only looks at the meta tags.

    I spoke to them about why such things as standards existed. and all they would do was stop caching our domains.

Re: overkill anti-caching CGI headers
by davebaker (Pilgrim) on Jun 18, 2009 at 15:53 UTC
    This is a great comment.

    I spent quite a little while trying to determine why I was unable to get CGI::Application::Plugin::Authentication to work correctly with my CGI app; I'd fill in the login form, click the button, but no joy. Click again. Then again. Then, on the fourth try, the login would go through. But sometimes it took 5 clicks. Sometimes only 3. Drove me batty, until I realized that the web server was cacheing the result of cgi scripts... set at 10 seconds. Firefox wouldn't submit the login form's data to the script until 10 seconds had passed since the time the login form appeared in the browser.

    I control the Apache config file on my server, and fixing the problem on the server side was easier for me. It was a matter of removing this line:

    ExpiresDefault "access plus 10 seconds""

      This worked really well for me too. Am working on an app where AJAX calls were being cached by MSIE 7. I pasted the code into the cgiapp_prerun and instantly all was well.

      My only change was to use DateTime rather than POSIX

      -Last_Modified => DateTime->now->strftime('%a, %d %b %Y %H:%M:%S GMT')

      This saved me a bunch time -- thanks!!!