Mojolicious-Plugin-StaticCache

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

  max_age
      # Mojolicious
      $self->plugin('StaticCache' => { max_age => 2592000 });

    Specify the maximum cache time for the Cache-Control header.

    Default is 2592000.

  cache_control
      # Mojolicious
      $self->plugin('StaticCache' => { cache_control => 'max-age=2592000, must-revalidate' });

    Specify the content of the Cache-Control header.

    Default is "max-age=$max_age, must-revalidate".

METHODS
    Mojolicious::Plugin::StaticCache inherits all methods from
    Mojolicious::Plugin and implements the following new ones.

  register
      $plugin->register(Mojolicious->new);

    Register plugin in Mojolicious application.

README.md  view on Meta::CPAN

    # Mojolicious
    $self->plugin('StaticCache' => { max_age => 2592000 });

Specify the maximum cache time for the Cache-Control header.

Default is 2592000.

## cache\_control

    # Mojolicious
    $self->plugin('StaticCache' => { cache_control => 'max-age=2592000, must-revalidate' });

Specify the content of the Cache-Control header.

Default is "max-age=$max\_age, must-revalidate".

# METHODS

[Mojolicious::Plugin::StaticCache](https://metacpan.org/pod/Mojolicious::Plugin::StaticCache) inherits all methods from
[Mojolicious::Plugin](https://metacpan.org/pod/Mojolicious::Plugin) and implements the following new ones.

## register

    $plugin->register(Mojolicious->new);

lib/Mojolicious/Plugin/StaticCache.pm  view on Meta::CPAN

package Mojolicious::Plugin::StaticCache;
use Mojo::Base 'Mojolicious::Plugin';

our $VERSION = '0.02';

sub register {
    my ($self, $app, $conf) = @_;

    $conf->{even_in_dev}   ||= 0;
    $conf->{max_age}       ||= 2592000;
    $conf->{cache_control} ||= "max-age=$conf->{max_age}, must-revalidate";
    my $mode = $app->mode;
    my $edev = $conf->{even_in_dev};

    $app->hook(after_static => sub {
        my $c = shift;
        if ($mode ne 'development' || $edev) {
            $c->res->headers->cache_control($conf->{cache_control});
        }
    });
}

lib/Mojolicious/Plugin/StaticCache.pm  view on Meta::CPAN

  # Mojolicious
  $self->plugin('StaticCache' => { max_age => 2592000 });

Specify the maximum cache time for the Cache-Control header.

Default is 2592000.

=head2 cache_control

  # Mojolicious
  $self->plugin('StaticCache' => { cache_control => 'max-age=2592000, must-revalidate' });

Specify the content of the Cache-Control header.

Default is "max-age=$max_age, must-revalidate".

=head1 METHODS

L<Mojolicious::Plugin::StaticCache> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.

=head2 register

  $plugin->register(Mojolicious->new);

t/basic.t  view on Meta::CPAN


get '/' => sub {
  my $c = shift;
  $c->render(text => 'Hello Mojo!');
};

my $t = Test::Mojo->new;
$t->get_ok('/mojo/logo-white.png')->status_is(200)->header_is('Cache-Control' => undef);

plugin 'StaticCache' => { even_in_dev => 1 };
$t->get_ok('/mojo/logo-white.png')->status_is(200)->header_is('Cache-Control' => 'max-age=2592000, must-revalidate');

plugin 'StaticCache' => { even_in_dev => 1, max_age => 2 };
$t->get_ok('/mojo/logo-white.png')->status_is(200)->header_is('Cache-Control' => 'max-age=2, must-revalidate');

plugin 'StaticCache' => { even_in_dev => 1, cache_control => 'public' };
$t->get_ok('/mojo/logo-white.png')->status_is(200)->header_is('Cache-Control' => 'public');

plugin 'StaticCache' => { even_in_dev => 1, max_age => 2, cache_control => 'public' };
$t->get_ok('/mojo/logo-white.png')->status_is(200)->header_is('Cache-Control' => 'public');

done_testing();



( run in 0.654 second using v1.01-cache-2.11-cpan-39bf76dae61 )