Skip to content

Commit

Permalink
Merge pull request #1551 from CPAN-API/leo/cache_2
Browse files Browse the repository at this point in the history
Leo/cache 2
  • Loading branch information
ranguard committed Jun 22, 2015
2 parents fb3508d + 45ace82 commit 6f830ce
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 38 deletions.
19 changes: 9 additions & 10 deletions bin/purge.pl
Expand Up @@ -11,13 +11,16 @@ =head1 NAME
=head1 SYNOPSIS
purge.pl --all
purge.pl --tag foo --tag bar
purge.pl --url '/about/'
purge.pl --key homepage --key about
=head1 DESCRIPTION
Script to purge things from Fastly CDN.
To clear a specific URL use:
curl -X PURGE https://metacpan.org/XXXX
=cut

use MetaCPAN::Web;
Expand All @@ -26,10 +29,8 @@ =head1 DESCRIPTION
my ( $opt, $usage ) = describe_options(
'purge.pl %o',
[ 'all', "purge all", ],
[ 'tag|t=s@', "tag(s) to purge", ],
[ 'url|u=s@', "url(s) to purge", ],
[],
[ 'help', "print usage message and exit" ],
[ 'key|k=s@', "key(s) to purge", ],
[], [ 'help', "print usage message and exit" ],
);

print( $usage->text ), exit if $opt->help;
Expand All @@ -42,13 +43,11 @@ =head1 DESCRIPTION
}
else {

my $tags = $opt->tag;
my $urls = $opt->url;
my $keys = $opt->key;

$c->cdn_purge_now(
{
tags => $tags,
urls => $urls
keys => $keys,
}
);

Expand Down
8 changes: 7 additions & 1 deletion lib/MetaCPAN/Web/Controller/About.pm
Expand Up @@ -11,7 +11,7 @@ sub auto : Private {
$c->add_surrogate_key('about');
$c->res->header(
'Cache-Control' => 'max-age=' . $c->cdn_times->{one_day} );
$c->cdn_cache_ttl( $c->cdn_times->{one_day} );
$c->cdn_cache_ttl( $c->cdn_times->{one_year} );

}

Expand Down Expand Up @@ -60,6 +60,12 @@ sub stats : Local {

$c->add_surrogate_key('stats');

# Only want a day for this, so they get refreshed
$c->cdn_cache_ttl( $c->cdn_times->{one_day} );

# Only want a day for this, so they get refreshed
$c->cdn_cache_ttl( $c->cdn_times->{one_day} );

$c->stash( template => 'about/stats.html' );

# See if user has the fastly credentials
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web/Controller/Root.pm
Expand Up @@ -42,7 +42,7 @@ sub index : Path : Args(0) {
$c->add_surrogate_key('homepage');
$c->res->header(
'Cache-Control' => 'max-age=' . $c->cdn_times->{one_hour} );
$c->cdn_cache_ttl( $c->cdn_times->{one_day} );
$c->cdn_cache_ttl( $c->cdn_times->{one_year} );

$c->stash->{template} = 'home.html';
}
Expand Down
46 changes: 22 additions & 24 deletions lib/MetaCPAN/Web/Role/Fastly.pm
Expand Up @@ -115,11 +115,11 @@ sub fastly_magic {
if ( $c->has_surrogate_keys_to_purge ) {

# Something changed, means we need to purge some keys
my @tags = $c->surrogate_keys_to_purge();
my @keys = $c->surrogate_keys_to_purge();

$c->cdn_purge_now(
{
tags => \@tags,
keys => \@keys,
}
);
}
Expand Down Expand Up @@ -154,31 +154,33 @@ sub fastly_magic {
}
}

sub _cdn_get_service {
my ( $c, $args ) = @_;

my $net_fastly = $c->_net_fastly();
return unless $net_fastly;

my $fsi = $c->config->{fastly_service_id};
return $net_fastly->get_service($fsi);

}

=head2 cdn_purge_now
$c->cdn_purge_now({
tags => [ 'foo', 'bar' ]
urls => [ 'this', 'and/that' ],
keys => [ 'foo', 'bar' ]
});
=cut

sub cdn_purge_now {
my ( $c, $args ) = @_;

my $net_fastly = $c->_net_fastly();
return unless $net_fastly;

my $fsi = $c->config->{fastly_service_id};

foreach my $tag ( @{ $args->{tags} || [] } ) {
my $purge_string = "https://metacpan.org/${fsi}/purge/${tag}";
$net_fastly->purge($purge_string);
}
my $service = $c->_cdn_get_service();
return unless $service; # dev box

foreach my $url ( @{ $args->{urls} || [] } ) {
my $purge_string = "https://metacpan.org/${url}";
$net_fastly->purge($purge_string);
foreach my $key ( @{ $args->{keys} || [] } ) {
$service->purge_by_key($key);
}
}

Expand All @@ -189,16 +191,12 @@ sub cdn_purge_now {
=cut

sub cdn_purge_all {
my $c = shift;
my $net_fastly = $c->_net_fastly();

die "No access" unless $net_fastly;

my $fsi = $c->config->{fastly_service_id};
my $c = shift;

my $purge_string = "/service/${fsi}/purge_all";
my $fastly_service = $c->_cdn_get_service();
die "No access" unless $fastly_service;

$net_fastly->purge($purge_string);
$fastly_service->purge_all;
}

1;
2 changes: 1 addition & 1 deletion t/controller/about.t
Expand Up @@ -23,7 +23,7 @@ test_psgi app, sub {
{
cache_control => 'max-age=86400',
surrogate_key => 'about',
surrogate_control => 'max-age=86400',
surrogate_control => 'max-age=31536000',
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion t/controller/home.t
Expand Up @@ -12,7 +12,7 @@ test_psgi app, sub {
{
cache_control => 'max-age=3600',
surrogate_key => 'homepage',
surrogate_control => 'max-age=86400',
surrogate_control => 'max-age=31536000',
}
);

Expand Down

0 comments on commit 6f830ce

Please sign in to comment.