Skip to content

Better caching and serve from cache more #3374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 8, 2025
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 0 additions & 179 deletions News.md

This file was deleted.

1 change: 0 additions & 1 deletion lib/MetaCPAN/Middleware/OldUrls.pm
Original file line number Diff line number Diff line change
@@ -70,7 +70,6 @@ sub _routes { (
[ '/permission/module/:module', '/module/:module/permissions' ],

[ '/feed/recent', '/recent.:type', $feed_type ],
[ '/feed/news', '/news.:type', $feed_type ],
[ '/feed/author/:author', '/author/:author/activity.:type', $feed_type ],
[ '/feed/distribution/:dist', '/dist/:dist/releases.:type', $feed_type ],

6 changes: 0 additions & 6 deletions lib/MetaCPAN/Web/Controller/About.pm
Original file line number Diff line number Diff line change
@@ -38,12 +38,6 @@ sub meta_hack : Local : Args(0) {
my ( $self, $c ) = @_;
}

sub resources : Local : Args(0) {
my ( $self, $c ) = @_;
$c->res->redirect( '/about/contact', 301 );
$c->detach;
}

sub sponsors : Local : Args(0) {
my ( $self, $c ) = @_;
}
65 changes: 9 additions & 56 deletions lib/MetaCPAN/Web/Controller/Feed.pm
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ use HTML::Escape qw( escape_html );
use MetaCPAN::Web::RenderUtil qw( render_markdown );
use MetaCPAN::Web::Types qw( ArrayRef DateTime Enum HashRef Str Undef Uri );
use Params::ValidationCompiler qw( validation_for );
use Path::Tiny qw( path );
use XML::FeedPP (); ## no perlimports
use URI ();

@@ -59,61 +58,6 @@ sub recent : Private {
);
}

sub news_rss : Path('/news.rss') Args(0) {
my ( $self, $c ) = @_;
$c->detach( 'news', ['rss'] );
}

sub news_atom : Path('/news.atom') Args(0) {
my ( $self, $c ) = @_;
$c->detach( 'news', ['atom'] );
}

sub news : Private {
my ( $self, $c, $type ) = @_;

$c->add_surrogate_key('NEWS');
$c->browser_max_age('1h');
$c->cdn_max_age('1h');

my $file = $c->config->{home} . '/News.md';
my $news = path($file)->slurp_utf8;
$news =~ s/^\s+|\s+$//g;
my @entries;
foreach my $str ( split /^Title:\s*/m, $news ) {
next if $str =~ /^\s*$/;

my %e;
$e{name} = $str =~ s/\A(.+)$//m ? $1 : 'No title';

# Use the same processing as _Header2Label in
# Text::MultiMarkdown
my $a_name = lc $e{name};
$a_name =~ s/[^A-Za-z0-9:_.-]//g;
$a_name =~ s/^[^a-z]+//gi;

$str =~ s/\A\s*-+//g;
$e{date} = $str =~ s/^Date:\s*(.*)$//m ? $1 : '2014-01-01T00:00:00';
$e{link} = '/news';
$e{fragment} = $a_name;
$e{author} = 'METACPAN';
$str =~ s/^\s*|\s*$//g;

#$str =~ s{\[([^]]+)\]\(([^)]+)\)}{<a href="$2">$1</a>}g;
$e{abstract} = $str;
$e{abstract} = render_markdown($str);

push @entries, \%e;
}

$c->stash->{feed} = $self->build_feed(
format => $type,
entries => \@entries,
host => $c->config->{web_host},
title => 'Recent MetaCPAN News',
);
}

sub author_rss : Chained('/author/root') PathPart('activity.rss') Args(0) {
$_[1]->detach( 'author', ['rss'] );
}
@@ -304,6 +248,15 @@ sub end : Private {
my $feed = $c->stash->{feed};
$c->detach('/end')
if !$feed;

# This will only affect if `cdn_max_age` has been set.
# https://www.fastly.com/documentation/guides/concepts/edge-state/cache/stale/
# If it has then do revalidation in the background
$c->cdn_stale_while_revalidate('1d');

# And if there is still an error serve from cache
$c->cdn_stale_if_error('1y');

$c->res->content_type(
$feed->isa('XML::FeedPP::Atom')
? 'application/atom+xml; charset=UTF-8'
24 changes: 0 additions & 24 deletions lib/MetaCPAN/Web/Controller/News.pm

This file was deleted.

1 change: 1 addition & 0 deletions lib/MetaCPAN/Web/Controller/Pod.pm
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@
my $release = $c->stash->{release};

if ( $data->{directory} ) {
$c->cdn_max_age('1y');

Check warning on line 43 in lib/MetaCPAN/Web/Controller/Pod.pm

Codecov / codecov/patch

lib/MetaCPAN/Web/Controller/Pod.pm#L43

Added line #L43 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can end up being a redirect from a non-versioned path to a versioned path, and it doesn't have any surrogate keys so it won't get purged.

$c->res->redirect( $c->uri_for( '/source', @path ), 301 );
$c->detach;
}
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web/Controller/Release.pm
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ sub view : Private {

my $release = $data->{release};

$c->browser_max_age('1h');
$c->browser_max_age('1d');
$c->res->last_modified( $release->{date} );
$c->cdn_max_age('1y');
$c->add_dist_key( $release->{distribution} );
8 changes: 8 additions & 0 deletions lib/MetaCPAN/Web/Controller/Root.pm
Original file line number Diff line number Diff line change
@@ -113,6 +113,14 @@ Attempt to render a view, if needed.
sub end : ActionClass('RenderView') {
my ( $self, $c ) = @_;

# This will only affect if `cdn_max_age` has been set.
# https://www.fastly.com/documentation/guides/concepts/edge-state/cache/stale/
# If it has then do revalidation in the background
$c->cdn_stale_while_revalidate('1d');

# And if there is still an error serve from cache
$c->cdn_stale_if_error('1y');

# for normal errors, try to render the internal_error page rather
my @error = @{ $c->error };
if ( @error && !$c->debug ) {
4 changes: 4 additions & 0 deletions lib/MetaCPAN/Web/Controller/Search.pm
Original file line number Diff line number Diff line change
@@ -16,9 +16,13 @@ sub index : Path : Args(0) {
my $page = $req->page;
my $page_size = $req->get_page_size(20);

# Cache searches as this is where most traffic hits
$c->cdn_max_age('1d');

# Redirect back to main page if search query is empty irrespective of
# whether we're feeling lucky or not.
unless ( $req->param('q') ) {
$c->browser_max_age('1w');
$c->res->redirect('/');
$c->detach;
}
32 changes: 0 additions & 32 deletions root/news.tx

This file was deleted.

2 changes: 1 addition & 1 deletion t/controller/about.t
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ test_psgi app, sub {
surrogate_key =>
'ABOUT STATIC content_type=text/html content_type=text',
surrogate_control =>
'max-age=31556952, stale-if-error=2592000',
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);
}
26 changes: 5 additions & 21 deletions t/controller/feed.t
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ test_psgi app, sub {
surrogate_key =>
'RECENT DIST_UPDATES content_type=application/rss+xml content_type=application',
surrogate_control =>
'max-age=31556952, stale-if-error=2592000',
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);
}
@@ -73,7 +73,7 @@ test_psgi app, sub {
surrogate_key =>
'author=PERLER content_type=application/rss+xml content_type=application',
surrogate_control =>
'max-age=31556952, stale-if-error=2592000',
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);
}
@@ -90,24 +90,7 @@ test_psgi app, sub {
surrogate_key =>
'dist=MOOSE content_type=application/rss+xml content_type=application',
surrogate_control =>
'max-age=31556952, stale-if-error=2592000',
}
);
}
);
get_feed_ok(
$cb,
'/news.rss',
sub {
my ( $res, $tx ) = @_;
test_cache_headers(
$res,
{
cache_control => 'max-age=3600',
surrogate_key =>
'NEWS content_type=application/rss+xml content_type=application',
surrogate_control =>
'max-age=3600, stale-if-error=2592000',
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);
}
@@ -141,7 +124,8 @@ sub test_redirect {
{
cache_control => 'max-age=31556952',
surrogate_key => 'content_type=text/html content_type=text',
surrogate_control => 'max-age=31556952, stale-if-error=2592000',
surrogate_control =>
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);

3 changes: 2 additions & 1 deletion t/controller/home.t
Original file line number Diff line number Diff line change
@@ -15,7 +15,8 @@ test_psgi app, sub {
cache_control => 'max-age=3600',
surrogate_key =>
'HOMEPAGE content_type=text/html content_type=text',
surrogate_control => 'max-age=31556952, stale-if-error=2592000',
surrogate_control =>
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);

5 changes: 3 additions & 2 deletions t/controller/source.t
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ test_psgi app, sub {
cache_control => 'max-age=3600',
surrogate_key =>
'SOURCE dist=MOOSE author=ETHER content_type=text/html content_type=text',
surrogate_control => 'max-age=31556952, stale-if-error=2592000',
surrogate_control =>
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);

@@ -43,7 +44,7 @@ test_psgi app, sub {
surrogate_key =>
'SOURCE dist=MOOSE author=ETHER content_type=text/html content_type=text',
surrogate_control =>
'max-age=31556952, stale-if-error=2592000',
'max-age=31556952, stale-while-revalidate=86400, stale-if-error=2592000',
}
);