Skip to content

Instantly share code, notes, and snippets.

/warmup.diff Secret

Created August 17, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/14ad4599401f880aaaad to your computer and use it in GitHub Desktop.
Save anonymous/14ad4599401f880aaaad to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious.pm b/lib/Mojolicious.pm
index c2ffb91..500c373 100644
--- a/lib/Mojolicious.pm
+++ b/lib/Mojolicious.pm
@@ -188,7 +188,11 @@ sub plugin {
$self->plugins->register_plugin(shift, $self, @_);
}
-sub start { shift->commands->run(@_ ? @_ : @ARGV) }
+sub start {
+ my $self = shift;
+ $_->_warmup for $self->static, $self->renderer;
+ return $self->commands->run(@_ ? @_ : @ARGV);
+}
sub startup { }
diff --git a/lib/Mojolicious/Renderer.pm b/lib/Mojolicious/Renderer.pm
index 1e4ddaf..a659075 100644
--- a/lib/Mojolicious/Renderer.pm
+++ b/lib/Mojolicious/Renderer.pm
@@ -29,7 +29,6 @@ $HOME->parse(
$HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
my %TEMPLATES = map { $_ => slurp $HOME->rel_file($_) } @{$HOME->list_files};
-# For templates from DATA sections
my $LOADER = Mojo::Loader->new;
sub DESTROY { Mojo::Util::_teardown($_) for @{shift->{namespaces}} }
@@ -59,12 +58,7 @@ sub get_data_template {
my ($self, $options) = @_;
# Index DATA templates
- unless ($self->{index}) {
- my $index = $self->{index} = {};
- for my $class (reverse @{$self->classes}) {
- $index->{$_} = $class for keys %{$LOADER->data($class)};
- }
- }
+ $self->_warmup unless $self->{index};
# Find template
my $template = $self->template_name($options);
@@ -230,18 +224,7 @@ sub _extends {
sub _handlers {
my ($self, $file) = @_;
-
- unless ($self->{templates}) {
-
- # Templates
- s/\.(\w+)$// and push @{$self->{templates}{$_}}, $1
- for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths};
-
- # DATA templates
- s/\.(\w+)$// and push @{$self->{templates}{$_}}, $1
- for map { sort keys %{$LOADER->data($_)} } @{$self->classes};
- }
-
+ $self->_warmup unless $self->{templates};
return $self->{templates}{$file};
}
@@ -260,6 +243,25 @@ sub _render_template {
return undef;
}
+sub _warmup {
+ my $self = shift;
+
+ @$self{qw(index templates)} = ({}, {});
+
+ # Classes
+ for my $class (reverse @{$self->classes}) {
+ $self->{index}{$_} = $class for keys %{$LOADER->data($class)};
+ }
+
+ # Templates
+ s/\.(\w+)$// and push @{$self->{templates}{$_}}, $1
+ for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths};
+
+ # DATA templates
+ s/\.(\w+)$// and push @{$self->{templates}{$_}}, $1
+ for map { sort keys %{$LOADER->data($_)} } @{$self->classes};
+}
+
1;
=encoding utf8
diff --git a/lib/Mojolicious/Static.pm b/lib/Mojolicious/Static.pm
index 1a41fb5..f460583 100644
--- a/lib/Mojolicious/Static.pm
+++ b/lib/Mojolicious/Static.pm
@@ -19,7 +19,6 @@ my $MTIME = time;
my $HOME = Mojo::Home->new;
my $PUBLIC = $HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/public');
-# For files from DATA sections
my $LOADER = Mojo::Loader->new;
sub dispatch {
@@ -125,13 +124,7 @@ sub _get_data_file {
# Protect templates
return undef if $rel =~ /\.\w+\.\w+$/;
- # Index DATA files
- unless ($self->{index}) {
- my $index = $self->{index} = {};
- for my $class (reverse @{$self->classes}) {
- $index->{$_} = $class for keys %{$LOADER->data($class)};
- }
- }
+ $self->_warmup unless $self->{index};
# Find file
return undef
@@ -145,6 +138,14 @@ sub _get_file {
return -f $path && -r $path ? Mojo::Asset::File->new(path => $path) : undef;
}
+sub _warmup {
+ my $self = shift;
+ $self->{index} = {};
+ for my $class (reverse @{$self->classes}) {
+ $self->{index}{$_} = $class for keys %{$LOADER->data($class)};
+ }
+}
+
1;
=encoding utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment