Skip to content

Commit

Permalink
let assets handle their mtime
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 30, 2014
1 parent e6466ae commit 8b66c45
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

5.35 2014-08-30
- Added mtime attribute to Mojo::Asset::Memory.
- Added mtime method to Mojo::Asset and Mojo::Asset::File.
- Improved monkey_patch to be able to name generated functions.

5.34 2014-08-29
Expand Down
7 changes: 7 additions & 0 deletions lib/Mojo/Asset.pm
Expand Up @@ -15,6 +15,7 @@ sub is_file {undef}
sub is_range { !!($_[0]->end_range || $_[0]->start_range) }

sub move_to { croak 'Method "move_to" not implemented by subclass' }
sub mtime { croak 'Method "mtime" not implemented by subclass' }
sub size { croak 'Method "size" not implemented by subclass' }
sub slurp { croak 'Method "slurp" not implemented by subclass' }

Expand Down Expand Up @@ -108,6 +109,12 @@ Check if asset has a L</"start_range"> or L</"end_range">.
Move asset data into a specific file. Meant to be overloaded in a subclass.
=head2 mtime
my $mtime = $asset->mtime;
Modification time of asset. Meant to be overloaded in a subclass.
=head2 size
my $size = $asset->size;
Expand Down
14 changes: 10 additions & 4 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -120,16 +120,16 @@ sub move_to {
return $self->path($to)->cleanup(0);
}

sub size {
return 0 unless defined(my $file = shift->path);
return -s $file;
}
sub mtime { _check(shift->path, 1) }
sub size { _check(shift->path, 0) }

sub slurp {
return '' unless defined(my $file = shift->path);
return Mojo::Util::slurp $file;
}

sub _check { $_[0] ? $_[1] ? (stat $_[0])[9] : -s $_[0] : 0 }

1;

=encoding utf8
Expand Down Expand Up @@ -233,6 +233,12 @@ True.
Move asset data into a specific file and disable L</"cleanup">.
=head2 mtime
my $mtime = $file->mtime;
Modification time of asset.
=head2 size
my $size = $file->size;
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -6,6 +6,7 @@ use Mojo::Util 'spurt';

has 'auto_upgrade';
has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 };
has mtime => sub {time};

sub add_chunk {
my ($self, $chunk) = @_;
Expand Down Expand Up @@ -114,6 +115,13 @@ Maximum size in bytes of data to keep in memory before automatically upgrading
to a L<Mojo::Asset::File> object, defaults to the value of the
C<MOJO_MAX_MEMORY_SIZE> environment variable or C<262144> (256KB).
=head2 mtime
my $mtime = $mem->mtime;
$mem = $mem->mtime(1408567500);
Modification time of asset, defaults to the current time.
=head1 METHODS
L<Mojo::Asset::Memory> inherits all methods from L<Mojo::Asset> and implements
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Static.pm
Expand Up @@ -98,7 +98,7 @@ sub serve_asset {
# Last-Modified and ETag
my $res = $c->res;
$res->code(200)->headers->accept_ranges('bytes');
my $mtime = $asset->is_file ? (stat $asset->path)[9] : $MTIME;
my $mtime = $asset->mtime;
my $options = {etag => md5_sum($mtime), last_modified => $mtime};
return $res->code(304) if $self->is_fresh($c, $options);

Expand Down Expand Up @@ -134,7 +134,7 @@ sub _get_data_file {
# Find file
return undef
unless defined(my $data = $LOADER->data($self->{index}{$rel}, $rel));
return Mojo::Asset::Memory->new->add_chunk($data);
return Mojo::Asset::Memory->new(mtime => $MTIME)->add_chunk($data);
}

sub _get_file {
Expand Down
8 changes: 8 additions & 0 deletions t/mojo/asset.t
Expand Up @@ -10,11 +10,14 @@ use Mojo::Asset::Memory;
# File asset
my $file = Mojo::Asset::File->new;
is $file->size, 0, 'file is empty';
is $file->mtime, 0, 'file is empty';
is $file->slurp, '', 'file is empty';
$file->add_chunk('abc');
is $file->contains('abc'), 0, '"abc" at position 0';
is $file->contains('bc'), 1, '"bc" at position 1';
is $file->contains('db'), -1, 'does not contain "db"';
is $file->size, 3, 'right size';
is $file->mtime, (stat $file->path)[9], 'right mtime';

# Cleanup
my $path = $file->path;
Expand All @@ -28,6 +31,10 @@ $mem->add_chunk('abc');
is $mem->contains('abc'), 0, '"abc" at position 0';
is $mem->contains('bc'), 1, '"bc" at position 1';
is $mem->contains('db'), -1, 'does not contain "db"';
is $mem->size, 3, 'right size';
ok $mem->mtime > (time - 100), 'right mtime';
my $mtime = $mem->mtime;
is $mem->mtime($mtime + 23)->mtime, $mtime + 23, 'right mtime';

# Empty file asset
$file = Mojo::Asset::File->new;
Expand Down Expand Up @@ -228,6 +235,7 @@ $file = Mojo::Asset::File->new(cleanup => 0)->add_chunk('test');
ok $file->is_file, 'stored in file';
is $file->slurp, 'test', 'right content';
is $file->size, 4, 'right size';
is $file->mtime, (stat $file->path)[9], 'right mtime';
is $file->contains('es'), 1, '"es" at position 1';
$path = $file->path;
undef $file;
Expand Down

0 comments on commit 8b66c45

Please sign in to comment.