WebDyne::Cache 3pm
WebDyne::Cache WebDyne module to cache dynamic output for improved performance.

    #  Sample time.psp compiled to cached static HTML. Every time this page is requested it will show
    #  the same time unless an interval of more than 10 seconds has elapsed.
    #
    <start_html>
    The most recent time this page was run was <? localtime ?>
    __PERL__
    use WebDyne::Static;
    use WebDyne::Cache (\&cache);

    sub cache {

        #  Self ref
        #
        my $self=shift();

        #  Get file last modified time (mtime)
        #
        my $mtime=${ $self->cache_mtime() };

        #  If older than 10 seconds force recompile
        #
        if ((time()-$mtime) > 10) {
            $self->cache_compile(1)
        };

        #  Done
        #
        return \1;

    }

## DESCRIPTION

The WebDyne::Cache module works in conjunction with the WebDyne::Static module to allow less frequent running of dynamic code, speeding up responsiveness for CPU or I/O heavy pages.

The WebDyne framework will continue to monitor for changes in the source file and recompile if the source .psp file is updated, regardless of the response of any caching directive.

## USAGE

The WebDyne::Cache code must be invoked in the requested page via one of two ways:

1. Via use of the WebDyne::Cache module, supplying a code ref as an parameter option as in the synopis:

       <start_html>
       ...
       __PERL__
       use WebDyne::Static;
       use WebDyne::Cache qw(\&cache);
       sub cache {
       ...

2. Via use of the meta-data field, with supply of the code ref in the <head> section of the document, e.g.

       <start_html meta="%{ 'WebDyne' => 'cache=&cache;static=1' }">
       ...
       __PERL__
       sub cache {
       ...

   Or:

       <html>
       <head>
       <title>Cache Demo</title>
       <meta name="WebDyne" content="cache=&cache;static=1">
       </head>
       <body>
       ...
       __PERL__
       sub cache {
       ...

In both cases the routine must flag to the WebDyne engine that the page should be recompiled. After whatever logic is required to make that determination (time elapsed, user input etc.) it should call the cache_compile() method with a true value to flag recompilation is required.

## METHODS

Methods below are not actually specific to the WebDyne::Cache module (they are presented by the main WebDyne module), but are listed here for convenience:

cache_mtime()

  Return a scalar ref of the cache file modification time in Unix epoch seconds.

cache_compile()

  Flag that page recompilation is required by supplying a true value.

## OPTIONS

WebDyne::Cache takes a single subroutine reference as an import parameter. No other options are available

## AUTHOR

Andrew Speer <andrew.speer@isolutions.com.au> and contributors.

## LICENSE and COPYRIGHT

This file is part of WebDyne.

This software is copyright (c) 2025 by Andrew Speer <andrew.speer@isolutions.com.au>.

This is free software; you can redistribute it and/or modify it underthe same terms as the Perl 5 programming language system itself.

Full license text is available at:

<http://dev.perl.org/licenses/>