瀏覽代碼

BILLY MAYS HERE

HAVE YOU EVER HAD A PAGE RENDER THAT TOOK OVER 250 MILLISECONDS?
WELL, WORRY NO MORE, BECAUSE THE BIG CITY TOILET WILL FLUSH THOSE
WORRIES RIGHT DOWN THE DRAIN!
Andy Baugh 5 年之前
父節點
當前提交
75771a8107
共有 3 個文件被更改,包括 79 次插入16 次删除
  1. 7 15
      cgi/pgupgrade.cgi
  2. 72 0
      lib/Troglodyne/CGI.pm
  3. 0 1
      templates/ui/pgupgrade.tmpl

+ 7 - 15
cgi/pgupgrade.cgi

@@ -1,32 +1,24 @@
 #!/usr/local/cpanel/3rdparty/bin/perl
-#WHMADDON:better_postgres:PostgreSQL Upgrade:troglophant.png
-#ACLS:all
-
 package Troglodyne::CGI::PgUpgrade;
 
 use strict;
 use warnings;
 
-use Cpanel::Template           ();
 use Cpanel::LoadModule::Custom ();
- 
+
 run() unless caller();
- 
+
 sub run {
     Cpanel::LoadModule::Custom::load_perl_module("Troglodyne::CGI");
-    my $args = Troglodyne::CGI::get_args();
-    my $tmpl = 'pgupgrade';
-    if( $args->{'version'} ) {
-        $tmpl = 'pginstall';
-    }
 
     print "Content-type: text/html\r\n\r\n";
-    Cpanel::Template::process_template(
+    Troglodyne::CGI::render_cached_or_process_template(
         'whostmgr',
         {
-            'template_file' => 'troglodyne/pgupgrade.tmpl',
-            'print'         => 1,
-        }
+            'troglodyne_do_static_render' => 1,
+            'template_file'               => "troglodyne/pgupgrade.tmpl",
+            'print'                       => 1,
+        },
     );
 
     exit;

+ 72 - 0
lib/Troglodyne/CGI.pm

@@ -9,4 +9,76 @@ sub get_args {
     return $args_hr;
 }
 
+# Yay. I now have ~250ms pageloads instead of 350+ms pageloads.
+sub render_cached_or_process_template {
+    my ( $service, $input_hr ) = @_;
+    if( $input_hr->{'troglodyne_do_static_render'} ) {
+
+        # Try to print from cache, as Cpanel::Template is slow AF
+        my ( $cached, $cache_file ) = cached( $service, $input_hr->{'template_file'} );
+        return if( $cached && render_from_cache( $cache_file ) );
+
+        # OK, so no cache. Let's fix that.
+        $input_hr->{'print'} = 0;
+        require Cpanel::Template;
+        my ( $success, $output_sr ) = Cpanel::Template::process_template( $service, $input_hr );
+        if( $success ) {
+            return if render_to_cache_and_print( $cache_file, $output_sr );
+        }
+    }
+
+    # Crap, everything failed. Just try to print it, sigh
+    require Cpanel::Template;
+    Cpanel::Template::process_template( $service, $input_hr );
+    return;
+}
+
+sub render_to_cache_and_print {
+    my ( $cache_file, $content_sr ) = @_;
+    local $@;
+    my $worked = eval {
+        open( my $fh, '>', $cache_file ) or die "Couldn't open cache file \"$cache_file\" for writing: $!";
+        print $fh $$content_sr;
+        print STDOUT $$content_sr;
+    };
+    if(my $err = $@) {
+
+        # Require bashes $@, so assign first
+        require Cpanel::Debug;
+        Cpanel::Debug::log_error($err);
+    }
+    return $worked;
+}
+
+sub render_from_cache {
+    my ( $cache_file ) = @_;
+    local $@;
+    my $worked = eval {
+        open( my $fh, '<', $cache_file ) or die "Couldn't open cache file \"$cache_file\" for reading: $!";
+        while( <$fh> ) { print $_; }
+    };
+    if(my $err = $@) {
+
+        # Require bashes $@, so assign first
+        require Cpanel::Debug;
+        Cpanel::Debug::log_error($err);
+    }
+    return $worked;
+}
+our $ULC = '/usr/local/cpanel';
+our %TMPL_DIRS_BY_SVC = (
+    'whostmgr' => 'whostmgr/docroot/templates',
+    'cpanel'   => 'base/frontend/paper_lantern',
+    'webmail'  => 'base/webmail/paper_lantern',
+);
+sub cached {
+    my ( $service, $tmpl_file ) = @_;
+    my $tmpl_path  = "$ULC/$TMPL_DIRS_BY_SVC{$service}/$tmpl_file";
+    my $cache_path = "$tmpl_path.cache";
+
+    # If cache mtime is older than template mtime, we are fine to use the cache.
+    my $cached = ( -s $cache_path && ( (stat(_))[9] > (stat($tmpl_path))[9] ) );
+    return ( $cached, $cache_path );
+}
+
 1;

+ 0 - 1
templates/ui/pgupgrade.tmpl

@@ -1,6 +1,5 @@
 [%
 USE Whostmgr;
-USE JSON;
 
 IF locale.get_html_dir_attr() == 'rtl';
 SET rtl_bootstrap = Whostmgr.find_file_url('/3rdparty/bootstrap-rtl/optimized/dist/css/bootstrap-rtl.min.css');