perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Lindner <>
Subject [RFC] Apache::CacheContent - Caching PerlFixupHandler
Date Thu, 06 Dec 2001 16:19:09 GMT

I would like to propose a new Apache module before I send it off to
CPAN.  The name chosen is Apache::CacheContent.

It's pretty generic code, and is intended to be subclassed.  It
handles the gory details of caching a page to disk and serving it up
until it expires.  

It's derived from work done on the mod_perl Developer's Cookbook, so
it's already been reviewed by a number of people.

I've attached a README below.  To download it go to

    Apache::CacheContent - PerlFixupHandler class that caches dynamic

    * Make your method handler a subclass of Apache::CacheContent
    * allow your web server process to write into portions of your document
    * Add a ttl() subroutine (optional)
    * Add directives to your httpd.conf that are similar to these:
      PerlModule MyHandler

      # dynamic url
      <Location /dynamic>
        SetHandler perl-script
        PerlHandler MyHandler->handler

      # cached URL
      <Location /cached>
        SetHandler perl-script
        PerlFixupHandler MyHandler->disk_cache
        PerlSetVar CacheTTL 120   # in minutes...

    Note:          This code is derived from the *Cookbook::CacheContent*
                   module, available as part of "The mod_perl Developer's

    The Apache::CacheContent module implements a PerlFixupHandler that helps
    you to write handlers that can automatically cache generated web pages
    to disk. This is a definite performance win for sites that end up
    generating the exact same content for many users.

    The module is written to use Apache's built-in file handling routines to
    efficiently serve data to clients. This means that your code will not
    need to worry about HTTP/1.X, byte ranges, if-modified-since, HEAD
    requests, etc. It works by writing files into your DocumentRoot, so be
    sure that your web server process can write there.

    To use this you MUST use mod_perl method handlers. This means that your
    version of mod_perl must support method handlers (the argument
    EVERYTHING=1 to the mod_perl build will do this). Next you'll need to
    have a content-generating mod_perl handler. If isn't a method handler
    modify the *handler* subroutine to read:

      sub handler ($$) {
        my ($class, $r) = @_;

    Next, make your handler a subclass of *Apache::CacheContent* by adding
    an ISA entry:

      @MyHandler::ISA = qw(Apache::CacheContent);

    You may need to modify your handler code to only look at the *uri* of
    the request. Remember, the cached content is independent of any query
    string or form elements.

    After this is done, you can activate your handler. To use your handler
    in a fully dyamic way configure it as a PerlHandler in your httpd.conf,
    like this:

      PerlModule MyHandler
      <Location /dynamic>
        SetHandler perl-script
        PerlHandler MyHandler->handler

    So requests to *http://localhost/dynamic/foo.html* will call your
    handler method directly. This is great for debugging and testing the
    module. To activate the caching mechanism configure httpd.conf as

      PerlModule MyHandler
      <Location /cached>
        SetHandler perl-script
        PerlFixupHandler MyHandler->disk_cache
        PerlSetVar CacheTTL 120  # in minutes..

    Now when you access URLs like *http://localhost/cached/foo.html* the
    content will be generated and stored in the file
    *DocumentRoot*/cached/foo.html. Subsequent request for the same URL will
    return the cached content, depending on the *CacheTTL* setting.

    For further customization you can write your own *ttl* function that can
    dynamically change the caching time based on the current request.

    Paul Lindner <>, Geoffrey Young, Randy Kobes

    The example mod_perl method handler the CacheWeather manpage.

    The mod_perl Developer's Cookbook

Paul Lindner    ||||| | | | |  |  |  |   |   |

    mod_perl Developer's Cookbook
         Human Rights Declaration

View raw message