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
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -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 ],

Expand Down
6 changes: 0 additions & 6 deletions lib/MetaCPAN/Web/Controller/About.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) = @_;
}
Expand Down
65 changes: 9 additions & 56 deletions lib/MetaCPAN/Web/Controller/Feed.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();

Expand Down Expand Up @@ -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'] );
}
Expand Down Expand Up @@ -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'
Expand Down
24 changes: 0 additions & 24 deletions lib/MetaCPAN/Web/Controller/News.pm

This file was deleted.

4 changes: 4 additions & 0 deletions lib/MetaCPAN/Web/Controller/Pod.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
my $release = $c->stash->{release};

if ( $data->{directory} ) {

# 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->cdn_max_age('1d');

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L46 was not covered by tests
$c->res->redirect( $c->uri_for( '/source', @path ), 301 );
$c->detach;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web/Controller/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} );
Expand Down
8 changes: 8 additions & 0 deletions lib/MetaCPAN/Web/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
4 changes: 4 additions & 0 deletions lib/MetaCPAN/Web/Controller/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 0 additions & 32 deletions root/news.tx

This file was deleted.

Loading
Loading