Browse Source

do cache invalidation better (not perfect obv)

George Baugh 5 tháng trước cách đây
mục cha
commit
037f2aa668

+ 1 - 0
lib/TCMS.pm

@@ -176,6 +176,7 @@ This is a "special" route as it needs to know about all the routes in order to d
 
 =cut
 
+# TODO anytime that the routing table changes, we need to invalidate the cache.
 sub robots ($query) {
     state $etag = "robots-" . time();
     my $routes = _routes();

+ 0 - 3
lib/Trog/Data/DUMMY.pm

@@ -66,9 +66,6 @@ sub delete ( $self, @posts ) {
     }
     $self->write( $example_posts, 1 );
 
-    # Gorilla cache invalidation
-    Path::Tiny::path('www/statics')->remove_tree;
-
     return 0;
 }
 

+ 0 - 3
lib/Trog/Data/FlatFile.pm

@@ -144,9 +144,6 @@ sub delete ( $self, @posts ) {
         Trog::SQLite::TagIndex::remove_post($update);
     }
 
-    # Gorilla cache invalidation
-    Path::Tiny::path('www/statics')->remove_tree;
-
     return 0;
 }
 

+ 0 - 3
lib/Trog/DataModule.pm

@@ -395,9 +395,6 @@ sub add ( $self, @posts ) {
     }
     $self->write( \@to_write );
 
-    # Gorilla cache invalidation
-    Path::Tiny::path('www/statics')->remove_tree;
-
     return 0;
 }
 

+ 8 - 18
lib/Trog/Renderer.pm

@@ -58,13 +58,19 @@ sub render ( $class, %options ) {
     return _yeet( $renderer, "Template not provided",                    %options ) unless $options{template};
 
     #TODO future - save the components too and then compose them?
-    my $skip_save = !$options{component} || !$options{data}{route} || $options{data}{has_query} || $options{data}{user} || ( $options{code} // 0 ) != 200 || Trog::Log::is_debug();
+    my $data = $options{data};
+    my $skip_save = $options{component} || $options{nocache} || !$data->{route} || $data->{has_query} || $data->{user} || ( $options{code} // 0 ) != 200 || Trog::Log::is_debug();
 
     my $ret;
     local $@;
     eval {
         $ret = $renderer->(%options);
-        save_render( $options{data}, $ret->[2], %{ $ret->[1] } ) unless $skip_save;
+        if (exists $data->{tpsgi} && !$skip_save) {
+            $data->{tpsgi}->add_post_close_callback(sub {
+                # We invalidate these in the individual routes themselves.
+                $data->{tpsgi}->save_render( $data->{route}, $rendertype, $ret->[2][0] );
+            });
+        }
         1;
     } or do {
         return _yeet( $renderer, $@, %options );
@@ -96,20 +102,4 @@ sub _yeet ( $renderer, $error, %options ) {
     return $ret;
 }
 
-sub save_render ( $vars, $body, %headers ) {
-    Path::Tiny::path( "www/statics/" . dirname( $vars->{route} ) )->mkpath;
-    my $file = "www/statics/$vars->{route}";
-
-    my $verb = -f $file ? 'Overwrite' : 'Write';
-    DEBUG("$verb static for $vars->{route}");
-    open( my $fh, '>', $file ) or die "Could not open $file for writing";
-    print $fh "HTTP/1.1 $vars->{code} OK\n";
-    foreach my $h ( keys(%headers) ) {
-        print $fh "$h:$headers{$h}\n" if $headers{$h};
-    }
-    print $fh "\n";
-    print $fh $body;
-    close $fh;
-}
-
 1;

+ 6 - 1
lib/Trog/Routes/HTML.pm

@@ -860,6 +860,12 @@ sub post_save ($qq) {
 
     $data->add($query) and die "Could not add post";
 
+    # Instruct tpsgi to invalidate the cached render.
+    $qq->{tpsgi}->add_post_close_callback(sub {
+        # We invalidate these in the individual routes themselves.
+        $qq->{tpsgi}->invalidate_render( $qq->{route}, 'html' );
+    });
+
     # Instruct tpsgi to restart the server after closing the request.
     $qq->{tpsgi}->signal_restart_parent();
 
@@ -1588,7 +1594,6 @@ sub totp_qr ($query) {
     );
 }
 
-# TODO make statics, abstract gzipped outputting & header handling
 sub rss_style ($query) {
     $query->{port}       = ":$query->{port}" if $query->{port};
     $query->{title}      = qq{<xsl:value-of select="rss/channel/title"/>};

+ 2 - 0
lib/Trog/Routes/JSON.pm

@@ -128,6 +128,8 @@ sub _render ( $code, $headers, %data ) {
         template    => 'bogus.tx',
         contenttype => 'application/json',
         headers     => $headers,
+        # TODO it may be appropriate to cache some of these methods.
+        nocache     => 1,
     );
 }