NAME

CGI::Cache - facility for caching the results of CGI scripts

SYNOPSIS

# unless the query string to this CGI script explicitly says
# do not look for pre-generated HTML, lets try to find pregen HTML
unless ($cgi->param('force_gen')) { 
# unless ($formdata{force_gen}) { 

 # --------------------------------------------------
 # Caching and Serving of Pre-processed results
 # --------------------------------------------------

 use CGI::Cache;
 CGI::Cache->Serve
   (
    {
     'url'         => 'http://www.angryman.com/cgi-bin/vote/votesearcher.cgi',
     'key'         => $formdata{issueid},
     'display_error'=> 0,
     'fdat'        => \%formdata
    },
    {
     'namespace'  => 'votesearcher.cgi', 
     'expires_in' => 60*10  # 10 minutes till next re-gen
    }
   );
 
 # CGI::Cache->Serve() exits the CGI script here

}

# ... the rest of the script is here... and will only run when
# the query string has explicitly required forced HTML generation.

DESCRIPTION

CGI::Cache caches the results of cgi scripts so that whatever time-intensive actions they are engaging in (usually database access) can be reduced. The first call to the script with CGI::Cache functionality is just as slow as the script normally is. The only difference is that the results of this execution are automatically stored away in a file cache so that the next call to the script with the same key within the file cache expiry time will serve the page that was generated the last time.

eg/votesearcher.cgi shows an example of adding caching facility to a normally slow cgi script.

The only function of CGI::Cache, Serve(), takes two hash references as its arguments. The first hashref configures CGI::Cache and the second argument is passed untouched to File::Cache. The first hashref can take four arguments, three of which are required:

  • url - the URL of the CGI script you want cached results for.

  • fdat - the form data to post to the CGI script

  • key - a unique key on which to cache this invocation of the CGI script. It will usually be a value posted to the CGI script. For example, if you have a script which is outputting a person's schedule, then you might key the CGI caching on the person's username, since this will be unique.

  • display_error (OPTIONAL) - tells CGI::Cache what to do in the event of that the attempt to serve a page (cached or not) is unsuccessful. If 0, then an error message is written to /tmp/cgi-cache-error.html and the browser is redirected to the $ENV{HTTP_REFERER}. However if display_error is set to a true value, then the error message will be served to the browser.

AUTHOR

T.M. Brannon <TBONE@cpan.org>

SEE ALSO

  • File::Cache (CPAN id: DCLINTON)