@@ -9,6 +9,27 @@ use MetaCPAN::Web::Types qw( ArrayRef Str );
99
1010MetaCPAN::Web::Role::Fastly - Methods for fastly intergration
1111
12+ =head1 METHODS
13+
14+ The following:
15+
16+ =head2 $c->add_surrogate_key('foo');
17+
18+ =head2 $c->purge_surrogate_key('bar');
19+
20+ =head2 $c->cdn_cache_ttl(3600);
21+
22+ Are applied when:
23+
24+ =head2 $c->fastly_magic()
25+
26+ is run in the L<end> , however if
27+
28+ =head2 $c->cdn_never_cache(1)
29+
30+ Is set fastly is forced to NOT cache, no matter
31+ what other options have been set
32+
1233=cut
1334
1435# # Stuff for working with Fastly CDN
@@ -39,6 +60,21 @@ has '_surrogate_keys_to_purge' => (
3960 },
4061);
4162
63+ # How long should the CDN cache, irrespective of
64+ # other cache headers
65+ has ' cdn_cache_ttl' => (
66+ is => ' rw' ,
67+ isa => ' Int' ,
68+ default => sub {0 },
69+ );
70+
71+ # Make sure the CDN NEVER caches, ignore any other cdn_cache_ttl settings
72+ has ' cdn_never_cache' => (
73+ is => ' rw' ,
74+ isa => ' Bool' ,
75+ default => sub {0 },
76+ );
77+
4278sub _net_fastly {
4379 my $c = shift ;
4480
@@ -55,28 +91,24 @@ sub _net_fastly {
5591sub fastly_magic {
5692 my $c = shift ;
5793
58- # Surrogate key caching and purging
59- if ( $c -> has_surrogate_keys ) {
60-
61- # See http://www.fastly.com/blog/surrogate-keys-part-1/
62- $c -> res-> header( ' Surrogate-Key' => $c -> join_surrogate_keys(' ' ) );
63- }
64-
94+ # Some action must have triffered a purge
6595 if ( $c -> has_surrogate_keys_to_purge ) {
6696
6797 # Something changed, means we need to purge some keys
98+ my @tags = $c -> surrogate_keys_to_purge();
6899
69- my $net_fastly = $c -> _net_fastly();
70- return unless $net_fastly ;
71-
72- my $fsi = $c -> config-> {fastly_service_id };
100+ $c -> cdn_purge_now(
101+ {
102+ tags => \@tags ,
103+ }
104+ );
105+ }
73106
74- foreach my $purge_key ( $c -> surrogate_keys_to_purge() ) {
75- my $purge_string
76- = " https://metacpan.org/${fsi} /purge/${purge_key} " ;
107+ # Surrogate key caching and purging
108+ if ( $c -> has_surrogate_keys ) {
77109
78- $net_fastly -> purge( $purge_string );
79- }
110+ # See http://www.fastly.com/blog/surrogate-keys-part-1/
111+ $c -> res -> header( ' Surrogate-Key ' => $c -> join_surrogate_keys( ' ' ) );
80112 }
81113
82114 # Set the caching at CDN, seperate to what the user's browser does
@@ -89,6 +121,7 @@ sub fastly_magic {
89121 }
90122 elsif ( my $ttl = $c -> cdn_cache_ttl ) {
91123
124+ # TODO: https://www.fastly.com/blog/stale-while-revalidate/
92125 # Use this value
93126 $c -> res-> header( ' Surrogate-Control' => ' max-age=' . $ttl );
94127
@@ -101,19 +134,51 @@ sub fastly_magic {
101134 }
102135}
103136
104- # How long should the CDN cache, irrespective of
105- # other cache headers
106- has ' cdn_cache_ttl' => (
107- is => ' rw' ,
108- isa => ' Int' ,
109- default => sub {0 },
110- );
137+ =head2 cdn_purge_now
111138
112- # Make sure the CDN NEVER caches, ignore any other cdn_cache_ttl settings
113- has ' cdn_never_cache' => (
114- is => ' rw' ,
115- isa => ' Bool' ,
116- default => sub {0 },
117- );
139+ $c->cdn_purge_now({
140+ tags => [ 'foo', 'bar' ]
141+ urls => [ 'this', 'and/that' ],
142+ });
143+
144+ =cut
145+
146+ sub cdn_purge_now {
147+ my ( $c , $args ) = @_ ;
148+
149+ my $net_fastly = $c -> _net_fastly();
150+ return unless $net_fastly ;
151+
152+ my $fsi = $c -> config-> {fastly_service_id };
153+
154+ foreach my $tag ( @{ $args -> {tags } || [] } ) {
155+ my $purge_string = " https://metacpan.org/${fsi} /purge/${tag} " ;
156+ $net_fastly -> purge($purge_string );
157+ }
158+
159+ foreach my $url ( @{ $args -> {urls } || [] } ) {
160+ my $purge_string = " https://metacpan.org/${url} " ;
161+ $net_fastly -> purge($purge_string );
162+ }
163+ }
164+
165+ =head2 cdn_purge_all
166+
167+ $c->cdn_purge_all()
168+
169+ =cut
170+
171+ sub cdn_purge_all {
172+ my $c = shift ;
173+ my $net_fastly = $c -> _net_fastly();
174+
175+ die " No access" unless $net_fastly ;
176+
177+ my $fsi = $c -> config-> {fastly_service_id };
178+
179+ my $purge_string = " /service/${fsi} /purge_all" ;
180+
181+ $net_fastly -> purge($purge_string );
182+ }
118183
1191841;
0 commit comments