Skip to content

Commit

Permalink
added experimental auto_upgrade attribute to Mojo::Asset::Memory
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 10, 2011
1 parent 3c05aef commit d280d02
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.26 2011-11-10 00:00:00
- Added EXPERIMENTAL upgrade event to Mojo::Asset::Memory.
- Added EXPERIMENTAL upgrade event to Mojo::Transaction::HTTP.
- Added EXPERIMENTAL auto_upgrade attribute to Mojo::Asset::Memory.
- Improved Mojo::Content::Single and Mojo::Content::MultiPart parsers
to reuse events.
- Improved documentation.
Expand Down
13 changes: 12 additions & 1 deletion lib/Mojo/Asset/Memory.pm
Expand Up @@ -5,6 +5,7 @@ use Carp 'croak';
use IO::File;
use Mojo::Asset::File;

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

# "There's your giraffe, little girl.
Expand All @@ -19,7 +20,8 @@ sub new {
sub add_chunk {
my ($self, $chunk) = @_;
$self->{content} .= $chunk if defined $chunk;
return $self unless $self->size > $self->max_memory_size;
return $self
if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
$self->emit(upgrade => my $file = Mojo::Asset::File->new);
return $file->add_chunk($self->slurp);
}
Expand Down Expand Up @@ -102,6 +104,15 @@ Note that this event is EXPERIMENTAL and might change without warning!
L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and
implements the following new ones.
=head2 C<auto_upgrade>
my $upgrade = $mem->auto_upgrade;
$mem = $mem->auto_upgrade(1);
Try to detect if content size exceeds C<max_memory_size> limit and
automatically upgrade to a L<Mojo::Asset::File> object.
Note that this attribute is EXPERIMENTAL and might change without warning!
=head2 C<max_memory_size>
my $size = $mem->max_memory_size;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/Single.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojo::Content';
use Mojo::Asset::Memory;
use Mojo::Content::MultiPart;

has asset => sub { Mojo::Asset::Memory->new };
has asset => sub { Mojo::Asset::Memory->new(auto_upgrade => 1) };
has auto_upgrade => 1;

sub new {
Expand Down
14 changes: 13 additions & 1 deletion t/mojo/asset.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 46;
use Test::More tests => 50;

# "And now, in the spirit of the season: start shopping.
# And for every dollar of Krusty merchandise you buy,
Expand Down Expand Up @@ -121,3 +121,15 @@ $tmp->add_chunk('x');
$file->move_to($tmp->path);
is $file->slurp, 'bcd', 'right content';
undef $tmp;

# Upgrade
my $asset = Mojo::Asset::Memory->new(max_memory_size => 5, auto_upgrade => 1);
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok $asset->is_file, 'stored in file';
$asset = Mojo::Asset::Memory->new(max_memory_size => 5);
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';

0 comments on commit d280d02

Please sign in to comment.