The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CGI::Buffer - Optimise the output of a CGI Program

VERSION

Version 0.17

SYNOPSIS

CGI::Buffer speeds the output of CGI programs by compressing output and by nearly seemlessley making use of client and server caches.

To make use of client caches, that is to say to reduce needless calls to your server asking for the same data, all you need to do is to include the package, and it does the rest.

    use CGI::Buffer;

    ...

To also make use of server caches, that is to say to save regenerating output when different clients ask you for the same data, you will need to create a cache. But that's simple:

    use CGI::Buffer;
    use CHI;

    # Put this at the top before you output anything
    CGI::Buffer::set_options(
        cache => CHI->new(driver => 'File')
    );
    if(CGI::Buffer::is_cached()) {
        exit;
    }

    ...

SUBROUTINES/METHODS

set_options

Sets the options.

    # Put this toward the top of your program before you do anything
    # By default, generate_tag and compress_content are both ON and
    # optimise_content is OFF
    use CGI::Buffer;
    CGI::Buffer::set_options(
        generate_etag => 1,     # make good use of client's cache
        compress_content => 1,  # if gzip the output
        optimise_content => 0,  # optimise your program's HTML
        cache => CHI->new(driver => 'File'),    # cache requests
        cache_key => 'string'   # key for the cache
    );

If no cache_key is given, one will be generatated which may not be unique. The cache_key should be a unique value dependent upon the values set by the browser.

is_cached

Returns true if the output is cached. If it is then it means that all of the expensive routines in the CGI script can be by-passed because we already have the result stored in the cache.

    # Put this toward the top of your program before you do anything

    # Example key generation - use whatever you want as something
    # unique for this call, so that subsequent calls with the same
    # values match something in the cache
    use CGI::Info;
    use CGI::Lingua;

    my $i = CGI::Info->new();
    my $l = CGI::Lingua->new(supported => ['en']);

    # To use server side caching you must give the cache argument, however
    # the cache_key argument is optional - if you don't give one then one will
    # be generated for you
    CGI::Buffer::set_options(
        cache => CHI->new(driver => 'File'),
        cache_key => $i->script_name() . '/' . $i->as_string() . '/' . $l->language()
    );
    if(CGI::Buffer::is_cached()) {
        # Output will be retrieved from the cache and sent automatically
        exit;
    }
    # Not in the cache, so now do our expensive computing to generate the
    # results
    print "Content-type: text/html\n";
    # ...

AUTHOR

Nigel Horne, <njh at bandsman.co.uk>

BUGS

There are no real tests because I haven't yet worked out how to capture the output that a module outputs at the END stage to check if it's outputting the correct data.

Mod_deflate can confuse this when compressing output. Ensure that deflation is off for .pl files:

    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|pl)$ no-gzip dont-vary

Please report any bugs or feature requests to bug-cgi-buffer at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Buffer. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc CGI::Buffer

You can also look for information at:

ACKNOWLEDGEMENTS

The inspiration and code for some if this is cgi_buffer by Mark Nottingham: http://www.mnot.net/cgi_buffer.

LICENSE AND COPYRIGHT

The licence for cgi_buffer is:

    "(c) 2000 Copyright Mark Nottingham <mnot@pobox.com>

    This software may be freely distributed, modified and used,
    provided that this copyright notice remain intact.

    This software is provided 'as is' without warranty of any kind."

The reset of the program is Copyright 2011 Nigel Horne, and is released under the following licence: GPL